Search code examples
bashshellshcsh

csh/sh for loop - how to?


i'm trying to write a for loop that executes 2 scripts on FreeBSD. I don't care if it's written in sh or csh. I want 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


Solution

  • csh does loops fine, the problem is that you are using exec, which replaces the current program (which is the shell) with a different one, in the same process. Since others have supplied sh versions, here is a csh one:

        #!/bin/csh
        set i = 11
        while ($i < 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 the ./patch.sh are you testing for its existence or running it? I am running it here, and testing the result - true means it returned zero. Alternatively:

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