Registered User
Registered User

Reputation: 5361

running shell scripts in different directories

I have a directory named tinkering which has following subdirectories:

saraswati\ and\ durga\ pooja
64\ yogini\ pooja
52\ guruwar\ ke\ tap\ se\ unemployment\ finish
bajrang\ bali\ har\ lete\ ain\ devote\ dukh
bhoot\ bhagane\ ke\ tareke
bacho\ ko\ gussa\ ane\ ka\ karan
durga\ pooja
khatre\ ke\ nishan\ hanth\ mein
saraswati\ and\ durga\ pooja
seb\ chadhane\ se\ ma\ hinnamasta
bhoot\ bhagane\ ke\ tareke

Each of these sub directories has a script named script.sh.

I wrote a script on the terminal:

cd ~/tinkering/; 
cd saraswati\ and\ durga\ pooja/;  
./script.sh;    
cd ..;  
cd 64\ yogini\ pooja/;  
./script.sh;cd ../;  
cd 52\ guruwar\ ke\ tap\ se\ unemployment\ finish/;  
./script.sh;cd ../;  
cd bajrang\ bali\ har\ lete\ ain\ devote\ dukh/;  
./script.sh;cd ../;  
cd bhoot\ bhagane\ ke\ tareke/;  
./script.sh;cd ..;  
cd bacho\ ko\ gussa\ ane\ ka\ karan/;  
./script.sh;cd ..;  
cd durga\ pooja/;./script.sh;  
cd ..;  
cd khatre\ ke\ nishan\ hanth\ mein/;./script.sh;  
cd ..;cd saraswati\ and\ durga\ pooja/;  
./script.sh;cd ..;  
cd seb\ chadhane\ se\ ma\ hinnamasta/;  
./script.sh;cd ..;  
cd bhoot\ bhagane\ ke\ tareke/;  
./script.sh;cd ..;

But this script could not run. The purpose was rather than going to each subdirectory and typing ./script.sh I be able to automate this process. What mistake did I do in the code above?

EDIT Please note I wrote these commands on terminal separated by a semi colon while I was in parent directory tinkering all the subdirectories have a different script which is doing a different work I want to invoke all the shell scripts of sub directories from parent directory on terminal.

Upvotes: 0

Views: 6364

Answers (4)

Ashwin Kumar k
Ashwin Kumar k

Reputation: 139

I guess this might work .

for d in */; do
 cp scr.sh "$d"
 chmod +x scr.sh
 done

for subdir in */; do
  cd "$subdir"
  ./scr.sh
  cd ..
done

for d in */; do rm scr.sh "$d"; done

Upvotes: 0

flesk
flesk

Reputation: 7579

Like others have pointed out, "could not run" could mean a number of things. E.g. if you get a message saying Permission denied, you have to use chmod a+x script.sh if you want to invoke your script with ./script.sh.

If you're able to run your scripts with /some path with whitespace/script.sh, you could put this into a shell script under ~/tinkering/.

find -name script.sh -mindepth 2 -maxdepth 2 -exec sh {} \;

Upvotes: 2

Chris Morgan
Chris Morgan

Reputation: 90742

The script needs to be executable and have a shebang.

Put #!/bin/sh on at the start and run in the terminal chmod +x myscript.sh (for whatever you've called your script).

If you're trying to do all subdirectories, you could also do it more efficiently with a for loop (I see Roland has provided the answer for that so I'll omit it).

Upvotes: 0

Roland Illig
Roland Illig

Reputation: 41617

for subdir in */; do
  cd "$subdir"
  ./script.sh
  cd ..
done

Upvotes: 4

Related Questions