Csh / sh for loop - how?

I am trying to write a for loop that executes 2 scripts on FreeBSD. I don't care if it is written in sh or csh. I need something like:

for($i=11; $i<=24; $i++) { exec(tar xzf 'myfile-1.0.' . $i); // detect an error was returned by the script if ('./patch.sh') { echo "Patching to $i failed\n"; } } 

Does anyone know how to do this, please?

thanks

+7
source share
5 answers

csh does the loops in order, the problem is that you are using exec, which replaces the current program (which is the shell) with another, in the same process. Since others have provided sh versions, here is csh:

  #! / bin / csh
     set i = 11
     while ($ i & lt 25)
         tar xzf "myfile-1.0. $ i"

         # detect an error was returned by the script   
         if ({./patch.sh}) then     
             echo "Patching to $ i failed"   
         endif
         @ i = $ i + 1
     end

Not sure about ./patch.sh , are you checking its existence or running it? I run it here and check the result - true means that it returns zero. As an alternative:

  # detect an error was returned by the script   
         if (! {tar xzf "myfile-1.0. $ i"}) then     
             echo "Patching to $ i failed"   
         endif
+3
source

A typical way to do this in sh is:

 for i in $(seq 11 24); do tar xzf "myfile-1.0$i" || exit 1 done 

Please note that seq not standard. Depending on the availability of tools, you can try:

 jot 14 11 24 

or

 perl -E 'say for(11..24)' 

or

 yes '' | nl -ba | sed -n -e 11,24p -e 24q 

I made a few changes: I abort if tar fails and does not give an error message, since tar should issue an error message instead of a script.

+6
source

Wow! No BASH. And probably not Kornshell:

 i=11 while [ $i -le 24 ] do tar xzf myfile-1.0.$i i=`expr $i + 1` if ./patch.sh then echo "patching to $i failed" fi done 

It is written in a clean shell of Burn, as God intended.

Note that you must use the expr command to add 1 to $i . Burn's shell does not do math. Return outputs mean executing a command and outputting STDOUT from a command in $i .

Kornshell and BASH make this a lot easier, as they can do the math and make it more complicated for loops.

+5
source

I think you should just use bash. I don’t have it, so it can’t test it, but something about this should work:

 for ((I=11; I<=24; I++)) ; do tar xzf myfile-1.0.$I || echo Patching to $I failed done 

EDIT: just read the comments and find out if bash is in the default installation of FreeBSD. So this may not work - I'm not sure about the differences between (t) csh and bash.

-one
source

Well, what I just did is the following.

w

Download sh shell, and then for the loop it works like in linux.

 for i in 1 2 3 4 5 6 7 8 9; do dd if=/dev/zero of=/tmp/yourfilename$i.test bs=52428800 count=15; done 
-one
source

All Articles