LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-11-2016, 08:18 AM   #1
pawimis
LQ Newbie
 
Registered: Apr 2016
Posts: 3

Rep: Reputation: Disabled
Task with recursion in bash


Hello.
I am working on following task :
,,Write a script modify with the following syntax:
modify [-r] [-l|-u] <dir/file names...>
modify [-r] <sed pattern> <dir/file names...>
modify [-h]
which will modify file names. The script is dedicated to lowerizing (-l)
file names, uppercasing (-u) file names or internally calling sed
command with the given sed pattern which will operate on file names.
Changes may be done either with recursion (-r) or without it. "

And I have question about recursion. I don't know how to understand it in this case. Option without recursion should just change file names in given directory. Option with recursion should change every folder name and file in given directory or in case that given file name is folder should it enter it and change all files with given pattern ?
Thanks in advance
 
Old 04-11-2016, 08:50 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
That sounds suspiciously like homework to me.

However: it also sounds like "an abusement of 'bash.'" Given that you can write "a shell script" in any language, I never recommend using Bash to do "serious" programming, especially not stuff that involves recursion. At the end of the day, Bash is only a command-executor, with just enough scripting capability to allow rudimentary looping and decision-making ... to govern exactly what commands are executed and with what parameters. In my experience, you can wind up spending far more time jimmying Bash to do a task that it really wasn't designed to do, than to avail yourself of the Bash feature that was designed for this purpose: #!shebang.

Simply tell Bash to hand over the keys to "the real programming language of your choice," and let Bash sit back and watch it go. The user will be none the wiser.

Last edited by sundialsvcs; 04-11-2016 at 08:52 AM.
 
1 members found this post helpful.
Old 04-11-2016, 09:22 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,666

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by pawimis View Post
Hello.
I am working on following task :
,,Write a script modify with the following syntax:
modify [-r] [-l|-u] <dir/file names...>
modify [-r] <sed pattern> <dir/file names...>
modify [-h]
which will modify file names. The script is dedicated to lowerizing (-l)
file names, uppercasing (-u) file names or internally calling sed
command with the given sed pattern which will operate on file names.
Changes may be done either with recursion (-r) or without it. "

And I have question about recursion. I don't know how to understand it in this case. Option without recursion should just change file names in given directory. Option with recursion should change every folder name and file in given directory or in case that given file name is folder should it enter it and change all files with given pattern ?
Well, since you're working on the task, why don't you show us what YOU have done/written/tried so far on your own???

Read the "Question Guidelines" and the LQ rules. We will be happy to help you if you're stuck, but we WILL NOT write your scripts or do your homework for you. Also, we can't answer your question about the use of recursion, because we don't know what the question parameters are. The script could be written either way.
 
Old 04-11-2016, 09:42 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
> Option with recursion should change every folder name and file in given directory or in case that given file name is folder should it enter it and change all files with given pattern ?

The first one. Try ls -lR /tmp for an example.

Edit: let's note that your program usually doesn't get any 'pattern', it is resolved by shell -- unless there is no matching file. Try this:
Code:
/bin/echo *nosuchfile* # pattern is passed to 'echo'
/bin/echo /bin/e*      # filenames are passed to 'echo'

Last edited by NevemTeve; 04-11-2016 at 09:46 AM.
 
Old 04-11-2016, 10:36 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
I agree that usually the best way to handle this would be to use a higher level language, however, if learning bash scripting this would be a good way to become familiar with
virtually all of its abilities

As TB0ne has said, we will not write the script for you, but if you show us what you have done and where you are stuck, we would be more than lad to assist
I must say that I am not fond of the sed option as there appears to be no way to indicate to use this option (perhaps like having a -s). Some will say that the pattern might be obvious, but the pattern
might also be something as simple as a string with no meta-characters. This would then be difficult to distinguish between a file / directory name and a pattern
 
Old 04-13-2016, 11:11 AM   #6
pawimis
LQ Newbie
 
Registered: Apr 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
I did not want you to write it for me . Just little help with understanding what does recursion mean in this task case Anyway I solved problem with recursion and than another appeared so if you can help me a bit (now I gave some code) please reply in this link: http://www.linuxquestions.org/questi...nt-4175577362/ This thread can be closed. Thank you for replies
 
Old 04-18-2016, 11:27 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
So if you have solved your problem, please display it so future newbies can learn from you

Also, please mark as SOLVED.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash: recursion = bad? bdoremus Programming 18 05-06-2010 12:54 PM
New to Bash | Need help with recursion. VauxhallVXR Programming 2 05-30-2009 05:47 PM
using recursion in bash script drkstr Linux - Software 4 07-09-2006 08:48 PM
Bash Local Variable Recursion With Array jshivers Programming 0 06-16-2006 04:31 PM
Writing bash script with recursion.. ray5_83 Programming 4 08-04-2004 05:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration