Hello,
This is basic (i think). I am trying to run a shell script which would go into each folder (folder names defined in the list) and after entering would run some commands, once done, come out of the folder and continue until the list ends.
Pretty basic and there are bunch of example online.
Here is my code:
1 BENCH="a b c d"
2
3 for l in "$BENCH"
4 do
5 echo "Going to $l Directory"
6 echo "$l"
7 cd $l
8 make clean
9 echo "Starting Simulation"
10 make all # This command takes hours to complete
11 echo "Simulation Done for $l"
12 cd ..
13
14 done
15 echo BYE
Now what is happening is, it takes the first on in the list, completes the make and prints out all the list name (in line 11). But does not necessarily continue with the other elements in the list.
I have a few to questions pose in response first:-
I'm assuming that the line numbers are not actually in your script. Is that right?
What output/errors do you get?
What OS and version are you using?
What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What output/errors do you get?
I suspect that at some point your script is in another directory, so your cd .. on line 12 is going wrong. You might be better to either remember the starting directory and explicity change back to that, or (if available) use pushd & popd to handle this for you.
If you can show us the normal output of a test run that doesn't cost you hours of waiting and also a run where you insert the line set -x before you set the BENCH variable, then that would be good too.
If the output from the make commands is going to be huge, push them out to a file if you want to keep them or just to /dev/null if not, just whilst we're testing the rest of it anyway. There are no glaring errors to me.
1: Yes line numbers are not included in the actual code.
2: I am not getting any obvious errors. after the "make all" command ends, I do get the print out of
Simulation done for a b c d
Bye
But it only completed the simulation for "a" instead of doing this from "a, b, c, d".
Since the make all command takes hours and generates some file, I am assuming I need to have some sort of control to explicitly say, wait until "make all" is done and then move on to the next one.
{txce:benchmarks} uname -a
Linux txce 2.6.32-642.11.1.el6.x86_64 #1 SMP Fri Nov 18 19:25:05 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
I am running the script from the this location and the list elements are all in this location. that is why I was doing "cd .."
While it is usually a good habit to quote variables to be expanded, HERE it is counter productive: it will expand to one single element 'a b c d', loop once only for this, and quit. You see that proven in the line cited.
Try dropping the double quotes.
I previously had another code, which was pretty similar (doing completely different tasks), it had same list likes and was able to loop through the elements. Is there a reason why this particular code would be doing this differently?
"Pretty similar" code doesn't necessarily result in "identical" behaviour. If OS, shell, and environment are the same, I'd be very surprised if identical loops would NOT execute identically.
If you want pople in here to have a look, post both code snippets, eventually with the necessary context information.
Please help me out, mayhap my eyesight escapes me - but I can't see double quotes around $l1 nor $l1 nor in any other loop construct, even when zooming in the browser to its extreme...?
I got you now RudiC.
My mistake. I thought when you said dropping the quotes, you were referring to drop the quotes on the List Definition (line 1 in the original code), and thats where I got confused.