Execute script in series of folders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execute script in series of folders
# 1  
Old 04-18-2014
Execute script in series of folders

I'm trying to get a script to iterate of an array in bash run a series of commands in those folders.

Code:
#Folder names
folders=(folder1 folder2 folder3)

#Common path for folders
fold_path="/Users/Rich/Projects/"

# For each element array copy script to folder and execute
for f in "${folders[@]}"
do
cp $desktop_script $fold_path/$f
chmod +x $fold_path/$f/$desktop_script
exec $fold_path/$f/$desktop_script
done

This part of the code seems to be working ok, the script is being copied across and made executable. I think the issue is with the secondary script.

This script gets the name of the directory it is in and adds that name to the commands it executes, specific for each folder by name
Code:
path=${PWD##*/}
 rm -rf .git
git remote rm origin
git remote add origin https://github.com/myusername/"${PWD##*/}".git
git push --set-upstream origin master

The error I keep getting is that the folder name, which is is evaluating fine returns:
Code:
./gitter.sh: line 2: folder1: command not found


Last edited by 3therk1ll; 04-18-2014 at 07:57 AM.. Reason: corrected syntax
# 2  
Old 04-18-2014
Quote:
Originally Posted by 3therk1ll
Code:
for f in "${folders[@]}"
do
cp $desktop_script $fold_path/$f
chmod +x $fold_path/$f/$desktop_script
exec $fold_path/$f/$desktop_script
done

This part of the code seems to be working ok ...
That for-loop will never loop. The exec will replace the shell with its command argument before the first iteration completes.

Do you actually need a copy of the script in each target directory? Why not simply cd in a subshell?
Code:
for f in "${folders[@]}"
do
    (cd "$fold_path/$f"; "$desktop_script")
done

If it matters, the subshell isn't strictly required.

Regards,
Alister

Last edited by alister; 04-18-2014 at 09:25 AM..
# 3  
Old 04-18-2014
Yeah I had tried the cd command before but it kept saying that the directory didn't exist, I think to do with changes in sub-shell paths(?).
Tried it with brackets and it worked.
Cheers dude
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trying to loop through folders and execute an existing python script.

I am trying to loop through lots and lots of folders and use the names of the folders to run a Python script which has parameters. E.g. -- setup_refs -n John -f England/London/Hackney/John -c con/con.cnf Normally to run `setup_refs` once from command line it's: `python setup_refs.py -n John... (3 Replies)
Discussion started by: Mr_Keystrokes
3 Replies

2. Shell Programming and Scripting

Execute multiple files in multiple folders and also output on same folder

How to execute multiple files in multiple folders and also output to be generated in the same folder? Hello Team, I have a path like Sanity_test/*/* and it has around 100+ folders inside with files. I would like to run/execute those files and output of execution to be placed on same /... (1 Reply)
Discussion started by: pushpabuzz
1 Replies

3. Shell Programming and Scripting

Move multiple files 4rm Source to different target folders based on a series num in the file content

Dear Experts my scenario is as follows... I have one source folder "Source" and 2 target folders "Target_123456" & "Target_789101". I have 2 series of files. 123456 series and 789101 series. Each series has got 3 types of fiels "Debit", "Refund", "Claims". All files are getting... (17 Replies)
Discussion started by: phani333
17 Replies

4. UNIX for Dummies Questions & Answers

Form a pipeline script with series of steps

Hello all, This maybe a dumb question, I have a series of awk command liners, perl and C executables that I usually run in a particular order to get an output. I need 2 files as input and then , a series of outputs are produced which pipe into the next step etc. I am usually able to paste... (3 Replies)
Discussion started by: senhia83
3 Replies

5. Shell Programming and Scripting

Script for moving series of sub folders

Hello, I'm new to this forum. Did a search but I didn't quite find what I was looking for. This is probably a fairly easy request but I'm just not sure how to accomplish this. I have a folder structure that looks something like this:/names/company1/archive /names/company1/newarchive ... (4 Replies)
Discussion started by: vipertech
4 Replies

6. Shell Programming and Scripting

Running a script over a series of files

Hi, I want to run a script over a series of files with the names : Sample_1.sorted.bam Sample_2.sorted.bam Sample_3.sorted.bam How can I specify it in my script. Thanks a lot in advance. (3 Replies)
Discussion started by: Homa
3 Replies

7. Shell Programming and Scripting

Shell script to generate Fibonacci series using recursion

I am facing problem with Shell script to generate Fibonacci series using recursion i.e. recursive function. Here is my script: #!/bin/sh fibo() { no=$1 if ; then return 0 elif ; then return 1 else a1=`expr $no - 1` fibo $a1 ... (10 Replies)
Discussion started by: Tapas Bose
10 Replies

8. UNIX for Dummies Questions & Answers

For Loop to execute a command on a series of files

Hello, I have a set of input data that I split into batches using the following command: split -l 4000000 MyInput.in Split_a Once I get the split files, I run a certain command on the split files that spews an output. So far I have been doing it without a for loop. How can I translate the... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

9. Shell Programming and Scripting

Need a script find the folders and execute a code

I need a script which can search for two files in location /share/point/ which has year and month stamps example 1) Extract200806.txt 2)file_new200805.csv If these files exists then it should run a code named abc.sas (3 Replies)
Discussion started by: reachsarath
3 Replies

10. Shell Programming and Scripting

Can we execute series of commands from a single script?

Hi, I am trying to call a certain command from within a shell script, but everytime it executes the script, only the first command runs and it comes out of the control, how do i do it? code : ```````` #!/bin/sh # # #i=1 #while #do # i=`expr $i + 1` #done StoreXML -project xnat -l... (2 Replies)
Discussion started by: virendra maloo
2 Replies
Login or Register to Ask a Question