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!
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.
#!/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
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;
....
/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
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?
#!/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!
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?
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