Simple BASH script?

Hi guys, I'm new to the forum so forgive me if I'm sounding ... daft.

I currently work in a Tech Support role. Every day we have to generate data by running around 10 .sh scripts. I was thinking instead of having to ./filename 10 times is it possible to right a new script that will run these for me on the relevant order? I'm assuming one of you guys could do this in 2 seconds, but would anyone mind pointing me in the right direction? E.g a template of what the script should look like?

Obviously I still think myself to be a novice when it comes to UNIX, but anything to make the little jobs easier would be great!

Cheers
Jamie

Put the following in a file call runall.sh then do

chmod +x runall.sh

to execute use

./runall.sh

#!/bin/sh
for d in    /path...../first-file.sh \
             /path..../second-file.sh \
             /path..../third-file.sh \
             /path..../fourth-file.sh
do
     $d
     RC=$?
     if test "$RC" != "0"
     then
          exit $RC
     fi
done

this will run the scripts and exit if there is an error returned.

Thanks porter, appreciated!

What does the +x specify after the chmod, isn't this normally numbers to specify read, write, execute permissions? Such as 660 or similar?

Could you have it so if an error occurred it would ask if you wanted to continue?

Thanks again!

"chmod +x .... " sets the "executeable" flags which tells the operating system this is a runable program.

Yes you could,

if test "$RC" != "0"
then
     echo "continue? [y]/n"
     read N
     case "$N" in 
        Y* | y* ) 
              ;;
        * )
              exit $RC
              ;;
      esac
fi

So something along the lines of;

#!/bin/sh
for d in    /path...../first-file.sh \
             /path..../second-file.sh \
             /path..../third-file.sh \
             /path..../fourth-file.sh \
if test "$RC" != "0"
then
     echo "continue? [y]/n"
     read N
     case "$N" in 
        Y* | y* ) 
              ;;
        * )
              exit $RC
              ;;
      esac
fi
done

Do the .sh scripts not need to be told to "./"?

Thanks porter!

Jamie

Depends what is on the path, in UNIX (unlike windows) you normally don't execute programs from the current directory.

If the directory name is not in the command name then the operating system will look for programs in directories listed in the PATH variable.

The "./" basically tells the OS what directory "runall.sh" is in. Similarly you should replace the "/path...." to what ever directory your scripts are in.

If I had 4 scripts in;
/usr/local/production/temp/

And another 4 in;
/usr/local/production/temp/newfolder

I could use;

#!/bin/sh
for d in    /usr/local/production/temp/first-file.sh \
             /usr/local/production/temp/second-file.sh \
             /usr/local/production/temp/third-file.sh \
             /usr/local/production/temp/fourth-file.sh \
             /usr/local/production/temp/newfolder/first-file.sh \
             /usr/local/production/temp/newfolder/second-file.sh \
             /usr/local/production/temp/newfolder/third-file.sh \
             /usr/local/production/temp/newfolder/fourth-file.sh \
if test "$RC" != "0"
then
     echo "continue? [y]/n"
     read N
     case "$N" in 
        Y* | y* ) 
              ;;
        * )
              exit $RC
              ;;
      esac
fi
done

Where I currently work we generally go to the directory the .sh script is located and simply;

./script.sh
....
             /usr/local/production/temp/newfolder/third-file.sh \
             /usr/local/production/temp/newfolder/fourth-file.sh
do
     $d
     RC=$?
     if test "$RC" != "0"
     then
          echo "continue? [y]/n"
.....

You missed
(a) don't put \ on the last item of the list
(b) the "do"
(c) the running of the command itself

:slight_smile:

So creating a file called gendata.sh, chmod +x and looking something like;

#!/bin/sh
for d in    /usr/local/production/temp/first-file.sh \
             /usr/local/production/temp/second-file.sh \
             /usr/local/production/temp/third-file.sh \
             /usr/local/production/temp/fourth-file.sh \
             /usr/local/production/temp/newfolder/first-file.sh \
             /usr/local/production/temp/newfolder/second-file.sh \
             /usr/local/production/temp/newfolder/third-file.sh \
             /usr/local/production/temp/newfolder/fourth-file.sh
do
     $d
     if test "$RC" != "0"
     then
          echo "continue? [y]/n"
     read N
     case "$N" in 
        Y* | y* ) 
              ;;
        * )
              exit $RC
              ;;
      esac
fi
done

Should run all the mentioned .sh files and if any errors occur prompt for user input?

Thanks Porter :b:

Jamie

One more line missing

after the running of the command "$d"
you need

RC=$?

before "if test "$RC" ...."

#!/bin/sh
for d in    /usr/local/production/temp/first-file.sh \
             /usr/local/production/temp/second-file.sh \
             /usr/local/production/temp/third-file.sh \
             /usr/local/production/temp/fourth-file.sh \
             /usr/local/production/temp/newfolder/first-file.sh \
             /usr/local/production/temp/newfolder/second-file.sh \
             /usr/local/production/temp/newfolder/third-file.sh \
             /usr/local/production/temp/newfolder/fourth-file.sh
do
     $d     
     RC=$?
     if test "$RC" != "0"
     then
          echo "continue? [y]/n"
     read N
     case "$N" in 
        Y* | y* ) 
              ;;
        * )
              exit $RC
              ;;
      esac
fi
done

chmod +x

Look good to you?

Thanks for all your help Porter, much appreciated!

Give it a whirl. Well done.

Cheers Porter! Thanks for you help!

Last question, if I wanted to make an "Options page" where the user had the choice of 1 or 2.

Depending on there choice would decide on which scripts are processed and which are not.

So the user would ./script.sh and have the choice of option 1 or 2. If they choose 1 it would process script 1,2 and 3 or if they choose 2 it would process 4,5 and 6. Is this possible too?

Thanks again!
Jamie

You can make a shell script as elaborate as you want within the limitations of the "language" ... :slight_smile:

This chap is doing some menus.... http://www.unix.com/shell-programming-scripting/43542-reference-variable.html

select Scripts in script1 script2;do
        case $script in
                script1) ;break;;
                script2) ;break;;
                *)printf "Invalid option\n";break;;
        esac
done

So, I'm looking for something along the lines of above? How would I be able to link each choice with the specific part of the script e.g;

Choice 1 would run;

#!/bin/sh
for d in    /usr/local/production/temp/newfolder/first-file.sh \
             /usr/local/production/temp/newfolder/first-file.sh \
             /usr/local/production/temp/newfolder/second-file.sh \
             /usr/local/production/temp/newfolder/third-file.sh \
             /usr/local/production/temp/newfolder/fourth-file.sh
do
     $d     
     RC=$?
     if test "$RC" != "0"
     then
          echo "continue? [y]/n"
     read N
     case "$N" in 
        Y* | y* ) 
              ;;
        * )
              exit $RC
              ;;
      esac
fi
done

chmod +x

Choice 2 would run;

#!/bin/sh
for d in    /usr/local/production/temp/first-file.sh \
             /usr/local/production/temp/second-file.sh \
             /usr/local/production/temp/third-file.sh \
             /usr/local/production/temp/fourth-file.sh 
do
     $d     
     RC=$?
     if test "$RC" != "0"
     then
          echo "continue? [y]/n"
     read N
     case "$N" in 
        Y* | y* ) 
              ;;
        * )
              exit $RC
              ;;
      esac
fi
done

chmod +x

I suggest putting the different choices in their own scripts and then you have something you can use immediately.

Then work on a separate menu program that calls the appropriate script.

That is what I plan on doing for the time being until I manage to get a menu working. Was just asking for the future really.

Thanks for your help Porter!
Jamie