Bash recursive scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash recursive scripting
# 1  
Old 12-12-2007
Bash recursive scripting

How does one make a recursive script in the bash shell? For instance, if I wanted a script that stepped through the directory structure starting with . and going to the deepest level, and ran make in each folder in the tree.

Yeah, I know a makefile could be setup to do this, but it's not allowed. No, I don't understand it either. Smilie
# 2  
Old 12-12-2007
Quote:
Originally Posted by James McMurray
but it's not allowed. No, I don't understand it either. Smilie
Probably because it's homework? Smilie

Seriously, bash is just a programming language like others and recursion can be done two ways....

1. starting child processes

2. calling functions

the function calling approach is often the best because it does not keep having to spawn new processes.

However variables then become and issue, with child processes they can only inherit variables, functions can change variables.

Bash also supports local variables in functions which are not global.
# 3  
Old 12-12-2007
Quote:
Originally Posted by porter
Probably because it's homework? Smilie
Nah, it's the government sector, which sometimes feels as pointless as homework, but without the "I'll escape here one day" feeling. Smilie

The system is setup so that some things have to be done in csh, some in bash, some as root, some absolutely never allowed as root, some that are best done rlogged into one machine, but that machine can never be used for actually running the sim, etc., etc. It's a monstrous conglomeration of homemade tools and outdated hardware.
# 4  
Old 12-12-2007
Quote:
Originally Posted by James McMurray
It's a monstrous conglomeration of homemade tools and outdated hardware.
It could be worse, you might have homemade hardware and outdated tools.
# 5  
Old 12-12-2007
Quote:
Originally Posted by porter
However variables then become and issue, with child processes they can only inherit variables, functions can change variables.
This is why all your variables (with carefully selected exceptions) should be local. From the iron rules of software engineering:

Thou shalt maketh your variables local and declareth them for everything else will be an abomination in the eyes of every compiler and every interpreter you might or might not run across.

bakunin
# 6  
Old 12-12-2007
Quote:
Originally Posted by bakunin
Thou shalt maketh your variables local and declareth them for everything else will be an abomination in the eyes of every compiler and every interpreter you might or might not run across.
Exceptingeth a thell which doeth not thupport local, or PL/M compiler which every variable ith global unleth you declareth the prothedure reentrant, or RPG on your A-eth-400. And-lo, they altho remembered Bathic ath well.

Last edited by porter; 12-12-2007 at 07:56 PM..
# 7  
Old 12-13-2007
Quote:
Originally Posted by porter
It could be worse, you might have homemade hardware and outdated tools.
We've got that too. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Bash Scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Try running 'phone4 xyz' and see what happens. Modify your program so that if no matching name is found, an... (1 Reply)
Discussion started by: OmgHaxor
1 Replies

2. Shell Programming and Scripting

Recursive File Renaming & Natural Sorting (Bash)

NB! I have already started a thread on this subject on ComputerHope (with the thread title "Recursive File Renaming & Logical Sorting"). However, on ComputerHope they are perhaps more specialized in Windows Command Prompt, and not that much in shell scripts for Bash (I guess). I have a bulk... (1 Reply)
Discussion started by: JewzeyfGhewbelz
1 Replies

3. Shell Programming and Scripting

bash scripting

same script: 1- i am using grep to find a string called: tinker panic 0 in a file /etc/ntp.conf if the string is not there, i want to add the strings in /etc/ntp.conf file in the first line of the file. if not do nothing or exit. 2- also i want to add # in front of the following lines in... (0 Replies)
Discussion started by: lamoul
0 Replies

4. Shell Programming and Scripting

bash scripting help

Hi Guys i have a <script?> that spits out the location of each printer using snpget here is the code for i in `sed -n '/Start Printer/,/End Printer/p' /hosts/blah/etc/dhcp/hosts.conf | awk '!/^#/ {print $2}' | egrep -v \... (2 Replies)
Discussion started by: ab52
2 Replies

5. Shell Programming and Scripting

bash scripting help

have this code but when i run it i get this error ./pulse: line 2: and here is the code #!/bin/bash if ; then pulseaudio -k; fi what am i doing wrong thanks Adam (5 Replies)
Discussion started by: ab52
5 Replies

6. Homework & Coursework Questions

bash,scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: i have to do this but i am confused, Create a file containing the bash functions which perform the... (1 Reply)
Discussion started by: CRAZYLITTLELOU
1 Replies

7. Shell Programming and Scripting

bash scripting help

hi all i'm trying to get a script working upon connection with pppd According to docu this happens ina clean environment with a couple of variables set, namely $1,$2,... To be able to execute the statements i included a path statement but i think i'm running into trouble with the variables -... (6 Replies)
Discussion started by: jimjones
6 Replies

8. UNIX for Dummies Questions & Answers

Should I do a bash scripting course?!

Hello, I'm confused (oh, yes). I'm running Linux at work. When I type 'echo $SHELL' I am told that I'm running tcsh. In /bin I note that both tcsh and bash are listed. Question 1: Can I swap to run bash rather than tcsh and, if so, how will this affect my system? Is there any advantage to... (6 Replies)
Discussion started by: macpete
6 Replies

9. Shell Programming and Scripting

Bash Scripting

Hello there peeps: There is a little piece of bash shell scripting problem i have, which i was hoping you could help me with. #!/bin/bash stored_word() { case $(( $$ % 8 )) in 0 ) echo "energy";; 1 ) echo "touch";; 2 ) echo "climbing";; 3 ) echo... (3 Replies)
Discussion started by: keyvan
3 Replies
Login or Register to Ask a Question