Bash Scripts
I have a script that mounts an samba shared drive, copies the contents if newer to the Linux box, and then umounts the share.
Sample script
---
mount -t smbfs -o username=x,password=y //machine/share /mnt/network cp --recursive --preserve --verbose --parents --update /mnt/network/*
/backup/machine \ > log1.txt 2>log2.txt
umount /mnt/network
------------
End script
A couple of easy questions I can't seem to find answers to.
1. How can I pass in parameters from the command line to use as variables.
myscript value1 value2
2. How can I have it dump a message to a log file if something fails and then cease running the script.
if mount fail append to end of log1.txt "mounting failed",
terminate script.
I was under the impression that you could have a script terminate if a command failed by having && separate the commands but this just generates an error.
Paul Kraus
Network Administrator
PEL Supply Company
216.267.5775 Voice
216-267-6176 Fax
www.pelsupply.com
BEGIN:VCARD
VERSION:2.1
N:Kraus;Paul
FN:Paul Kraus ([email protected])
ORG:PEL Supply Company
TITLE:Network Administrator
TEL;WORK;VOICE:(216) 267-5775
TEL;CELL;VOICE:(216) 410-5526
TEL;WORK;FAX:(216) 267-6176
ADR;WORK:;;4666 Manufacturing Road;Cleveland;Ohio;44135;United States of America LABEL;WORK;ENCODING=QUOTED-PRINTABLE:4666 Manufacturing Road=0D=0ACleveland, Ohio 44135=0D=0AUnited States of Ame= rica
ADR;HOME:;;;;;;United States
LABEL;HOME:United States
URL;WORK:http://www.pelsupply.com
EMAIL;PREF;INTERNET:[email protected]
REV:20020416T182124Z
END:VCARD
1. the first value passed from the command line would be referenced as $1 the second as $2
2. run command as part of an if statement. if the command execute successfully the error result will be 0 and follow the then branch other wise the else.
Quoted 26 lines · Paul Kraus
I have a script that mounts an samba shared drive, copies the contents if newer to the Linux box, and then umounts the share.
Sample script
---
mount -t smbfs -o username=x,password=y //machine/share/mnt/networkcp --recursive --preserve --verbose --parents --update/mnt/network/*/backup/machine \ > log1.txt 2>log2.txtumount
/mnt/network
------------
End scriptA couple of easy questions I can't seem to find answers to.
1. How can I pass in parameters from the command line to use as variables.
myscript value1 value2
2. How can I have it dump a message to a log file if something fails and then cease running the script.
if mount fail append to end of log1.txt "mounting failed",
terminate script.I was under the impression that you could have a script terminate if a command failed by having && separate the commands but this just generates an error.
Quoted 6 lines
Paul Kraus
Network Administrator
PEL Supply Company
216.267.5775 Voice
216-267-6176 Fax
www.pelsupply.com
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Quoted 2 lines · Paul Kraus
[...]
A couple of easy questions I can't seem to find answers to.
Assuming we are talking about standard shell scripts (in practice, ones that run using bash) ...
Quoted 3 lines
1. How can I pass in parameters from the command line to use as variables.
myscript value1 value2
These command-line arguments are accessed as $1 and $2 (and so forth).
Quoted 8 lines
2. How can I have it dump a message to a log file if something fails and then cease running the script.
if mount fail append to end of log1.txt "mounting failed",
terminate script.I was under the impression that you could have a script terminate if a command failed by having && separate the commands but this just generates an error.
If it generates an error, you have the details wrong, because this is the correct (or at least the common) way to to do it. It is basically a programmers's trick ... it works because && is the logical AND operator, and, in bash (and most Unix/Linux interpreters), it is evaluated left to right. So ... first it evaluates whatever is on the left side of &&. If that part is TRUE, then it evaluates the right side, but if it is FALSE it does not evaluate the right side (since it knows without doing so that the combined boolean expression evaluates to FALSE).
If the two parts are commands, then it executes the one on the left. If execution returns a TRUE value (basically, this means that it does not return an error), it executes the rifht-hand command, which might be
echo "something or other"
or
exit 0
If you look in your init-script directory (I forget where RH keeps this), you'll find a ton of examples of how to do this.
BTW, you can find all this stuff in the man page for bash. It is about the most difficult man page I've ever tried to read, though (the information needed about bash is just too long for the man-page mechanism to handle), so you'd do better to find a tutorial (use the usual search strategies) or a book ... or look at the many examples on your system.
--
-------------------------------------------"Never tell me the odds!"--------
Ray Olszewski -- Han Solo
Palo Alto, California, USA [email protected]
-------------------------------------------------------------------------------
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Here is an example of a simple backup script I use to backup some files and copy them to another machine to be put on tape.
echo $HOSTNAME > /var/log/backup.log
echo "Backup Started: " >> /var/log/backup.log
date >> /var/log/backup.log
tar --create --verbose --bzip2 --file /root/tuxtracker.tar.bz2 --files-from /etc/backup-list --exclude-from /etc/backup-exclude >> /var/log/backup.log 2>&1 echo "Backup Stoped: " >> /var/log/backup.log
date >> /var/log/backup.log
echo "Transfer to Annex Started: " >> /var/log/backup.log
date >> /var/log/backup.log
if scp -q /root/tuxtracker.tar.bz2 [email protected]:. >> /var/log/backup.log 2>&1
then
echo "Transfer to Annex Finished Successfully: " >> /var/log/backup.log
date >> /var/log/backup.log
else
echo "Transfer to Annex was Interrupted: " >> /var/log/backup.log
date >> /var/log/backup.log
fi
The first line creates a log file and all other lines append to it.