LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-18-2005, 05:14 PM   #1
newbie_m
LQ Newbie
 
Registered: Dec 2004
Posts: 6

Rep: Reputation: 0
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!
 
Old 01-18-2005, 06:38 PM   #2
student04
Member
 
Registered: Jan 2004
Location: Georgia
Distribution: OS X, CentOS
Posts: 669

Rep: Reputation: 34
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

Last edited by student04; 01-18-2005 at 06:40 PM.
 
Old 01-18-2005, 07:44 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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

Last edited by chrism01; 01-18-2005 at 07:50 PM.
 
Old 01-18-2005, 09:12 PM   #4
Blinker_Fluid
Member
 
Registered: Jul 2003
Location: Clinging to my guns and religion.
Posts: 683

Rep: Reputation: 63
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with shell scripts thorney Linux - Newbie 4 11-28-2005 12:18 AM
Shell Scripts benwy Linux - Software 1 06-09-2003 03:58 AM
Shell scripts j0hn0n1 Linux - Software 1 01-03-2003 04:11 PM
shell scripts nautilus_1987 Linux - General 3 08-30-2002 04:12 AM
shell scripts gui10 Programming 10 10-28-2001 03:46 AM


All times are GMT -5. The time now is 12:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration