Basic Shell script - Not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Basic Shell script - Not working
# 1  
Old 03-31-2017
Display Basic Shell script - Not working

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:
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.

What is happening here?

Last edited by Zam_1234; 03-31-2017 at 01:37 PM.. Reason: code edit
# 2  
Old 03-31-2017
Hello Zam_1234,

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.


Thanks, in advance,
Robin
# 3  
Old 03-31-2017
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
Code:
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.
Code:
{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 .."
Code:
{txce:benchmarks} ls
a  b  c  d  run.sh

# 4  
Old 03-31-2017
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.
# 5  
Old 03-31-2017
But why would it convert into a single element?

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?

Thanks in advance
# 6  
Old 03-31-2017
"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.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 03-31-2017
Here is the code which worked:
Code:
      1 # -- an example--
      2 export GEM5_DIR=/usr/local/gem5
      3 export BENCHMARK=./src/benchmark
      4 #export ARGUMENT=./data/input.program
      5 
      6 l1="32kB 64kB 128kB";
      7 l2="512kB 1MB 3MB";
      8 l1_assoc="2 4";
      9 l2_assoc="4 8 12"
     10 for l1_size in $l1
     11         do
     12         echo "Current L1_size: $l1_size"
     13         echo "--------------------------"
     14         for l1_assc in $l1_assoc
     15                 do
     16                 echo "Current L1_associativity: $l1_assc"
     17                 echo "-----------------------------------"
     18                 for l2_size in $l2
     19                         do
     20                         echo "Current L2_size: $l2_size"
     21                         echo "--------------------------"
     22                         for l2_assc in $l2_assoc
     23                                 do
     24                                 echo "-------------Current Settings:-------------"
     25                                 echo "L1_size: $l1_size \t L1_assoc: $l1_assc \t L2_size: $l2_size \t L2_assoc: $l2_assc \t"
     26                                 echo "*************************************************************************************"
     27                                 echo "Starting our Project"
     28                                 now=$(date +"%T")
     29                                 echo "Starting simulation at $now"
     30                                 mkdir ~/6304/429.mcf/m5out/$l1_size"_L1_"$l1_assc"_L1_assoc_"$l2_size"_L2size_"$l2_assc"_L2_assoc";
     31                                 time $GEM5_DIR/build/X86/gem5.opt  -d ~/6304/429.mcf/m5out/$l1_size"_L1_"$l1_assc"_L1_assoc_"$l2_size"_L2size_"$l2_assc"_L2_assoc" $GEM5_DIR/configs/example/se.py -I 5000  -n 1 --cpu-type=xf --caches --l1d_size="$l1_size" --l1i_size="$l1_size" --l1i_assoc="$l1_assc        " --l1d_assoc="$l1_assc"  --l2cache --l2_size="$l2_size" --l2_assoc="$l2_assc" --cpu-clock=1.2GHz --sys-voltage=1.2V --cacheline_size=64 -c $BENCHMARK -o "./data/inp.in";
     32                                 done
     33                         done
     34                 done
     35         done

This created folders with simulation for all those different combinations mentioned. But the difference may have been in the OS:
Code:
{6304:429.mcf} uname -a
Linux 6304 3.10.0-327.18.2.el7.x86_64 #1 SMP Thu May 12 11:03:55 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

~
~
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Basic Combination Shell Script

I need to have a script read a file that has a list of words in a single column like below:Black Blue Brown Orange Red Yellow Green White Purple Silver Grey Tan Then print to another file just all of the two-word possible combinations. Example: Black,Blue Anyone want to take a... (4 Replies)
Discussion started by: vespasian
4 Replies

2. Shell Programming and Scripting

Need a little help with my first shell script. Basic image resize script...

Hey everyone, just now joined because I didn't want to go onto Ubuntu forums and start asking about how to write shell scripts. Seems like this is a pretty active forum for exactly what I need. I'm trying to modify a shell script I found online, the end goal is to have it find all files in the... (9 Replies)
Discussion started by: mozzles
9 Replies

3. UNIX for Dummies Questions & Answers

Help with basic script - not working..

Here is the script I wrote... Pls. help. #!/bin/ksh for SERVER in NEWSNYD403P NEWSNYD404P SNEWSNYD601P SNEWSNYD602P SNEWSDNY603P SNEWSNYD604P do /usr/bin/scp "/home/sam/bash.tar root@$SERVER":/tmp done Error msg I get: ./scp_script.sh: syntax error at line 3:... (2 Replies)
Discussion started by: samnyc
2 Replies

4. Shell Programming and Scripting

Basic question on shell script execution

I have two shell scripts in the different directories listed below, /root/dev/dir1/test.sh /root/dev/dir2/master.sh I am executing the master.sh script from the test.sh like below and getting 'Permission denied' error. #! /bin/sh #test.sh path='/root/dev' $path/dir2/master.sh But it... (2 Replies)
Discussion started by: vel4ever
2 Replies

5. Shell Programming and Scripting

Help! Basic shell script advice

##### (2 Replies)
Discussion started by: AidoPotato
2 Replies

6. Shell Programming and Scripting

Basic shell script help

Im trying to make a script that simply adds a word to the last available line in a txt file without overwriting any previous lines. Ive googled this and there are great examples but no one explain what each function does, and i dont entirely understand how it works. Basically Im looking for... (7 Replies)
Discussion started by: kylecn
7 Replies

7. Shell Programming and Scripting

Basic Shell Script Help

Lets say I wanted to create a script that would show what people are doing on my machine using the w command and refresh in about 6 seconds. What would be the easiest way to do this? I pretty much want the script to loop until I stop it. I'm using the BASH shell by the way. Help is appreciated.... (1 Reply)
Discussion started by: c4391
1 Replies

8. Shell Programming and Scripting

shell script basic doubt

hi, I am new script learner, so my basic doubt is , how to store value of any command in a variable example $ ls | wc -l i want to stote the output of this in a variable c. so that i can use c in if else loop. and when do we use " ` " symbol in script.. can anyone also tell for... (5 Replies)
Discussion started by: hi2_t
5 Replies

9. Shell Programming and Scripting

Basic Shell script syntax help

Hi All, I am new to shell scripting. I have a variable which holds a numeric value.I have to check whether this variable holds a value between(0- 8),(8-17)(17-24).How do i write this syntax using if in shell scripting. Thanks Vignesh (2 Replies)
Discussion started by: vignesh53
2 Replies

10. Shell Programming and Scripting

need a quick basic shell script help

im trying to run the below if command ifconfig -a |grep 10.100.120.21 gives me below output inet addr:10.100.120.21 Bcast:10.100.120.255 Mask:255.255.255.0 i just want a basic shell which says if above exists then continue how would i do this? (6 Replies)
Discussion started by: eb222
6 Replies
Login or Register to Ask a Question