LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell scripts (https://www.linuxquestions.org/questions/linux-newbie-8/shell-scripts-279428/)

newbie_m 01-18-2005 04:14 PM

shell scripts
 
Hi,

I would appreciate help with the following. I am writing a shell script
that call several different perl files.

To be exact let us say that I am calling four perl files. We will call
them a.pl, b.pl, c.pl, and d.pl.

I want to run them in the following order: a.pl, b.pl, c.pl, d.pl.

If both a.pl and b.pl do not run properly I don't want to run c.pl, or d.pl.

If both a.pl and b.pl run okay but c.pl does not run properly I don't want
to run d.pl.

Each time ther is an error I want to report it in a log file.

I would like to know the best way of doing this in a shell script.
Also, is there something better I can do instead of just writing an
error to the log file.
One of the perl files that I am calling is loading a database.
If the loading doesn't work properly I would like to roll back instead
of having an improper loading.

thank in advance!

student04 01-18-2005 05:38 PM

I have a couple of links that i found useful in writing my shell scripts for bash:

Beginner's guide:
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html

Advanced guide (more indepth than the one above):
http://www.tldp.org/LDP/abs/html/index.html

And another:
http://quong.best.vwh.net/shellin20/

And something i found very quick and easy for variables in bash:
http://www.linuxvoodoo.com/resources...eclareref.html

Shell scripts basically run commands from the console except you can put them into a script and manipulate multiple commands, set variables, make loops (for repeated commands that would otherwise be tedious), read and write to files easily, etc.

For example the command
Code:

$ echo "My name is Alex" >> ~/tmp/name.txt
would do the same thing as
Code:

#!/bin/bash
# This is the script
echo "My name is Alex" >> ~/tmp/name.txt

Now you can modify that and use command line arguments like
Code:

#!/bin/bash
# "$1" is the first command line argument indicating
# the name of the file to store this text in the folder ~/tmp/
echo "My name is Alex" >> ~/tmp/$1

It would be run like this:
$ sh thescript.sh filename.txt
or
$ ./thescript.sh filename.txt

chrism01 01-18-2005 06:44 PM

Try this:

Code:

#!/bin/bash
for file in `ls -1 *.pl`
do
    ./$file
    if [[ $? -ne 0 ]]
    then
        echo "$file failed"
        break
    fi
done

For a DB rollback, you'll have to do that inside the perl prog.

Here's an email submit fn you could use:
Code:

function mail_msg()
{
    # Get params & set local vars
    MSG=$1
    SUBJECT=$2
    MAILTO='abc@some.net.somewhere'
    NODE=`uname -n`

    echo "$MSG" | mail -s "$NODE:$SUBJECT" $MAILTO
    if [[ $? -ne 0 ]]
    then
        exit_with_error "Failed to email $NODE:$SUBJECT $MSG $MAILTO"
    fi
}
function exit_with_error()
{
    echo -e "$0 exiting with error. Msg was:\n$1"
    exit 1
}

call it like this
Code:

    mail_msg "$0: Unable to <your msg here> " "$0: Failed !"
HTH

Blinker_Fluid 01-18-2005 08:12 PM

Maybe I'm just being a little too simplistic on this...
Couldn't you just do this:
a.pl && b.pl && c.pl && d.pl || mail -s "the script failed" yourname@your.com < /dev/null

Check this out:
http://www.freeos.com/guides/lsst/ch04sec3.html


All times are GMT -5. The time now is 04:27 PM.