* Bash Scripts
@ 2002-10-10 20:34 Paul Kraus
2002-10-10 21:13 ` Don Petrowski
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Paul Kraus @ 2002-10-10 20:34 UTC (permalink / raw)
To: linux-newbie
[-- Attachment #1: Type: text/plain, Size: 976 bytes --]
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
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Paul Kraus (pkraus@pelsupply.com).vcf --]
[-- Type: text/x-vcard; name="Paul Kraus (pkraus@pelsupply.com).vcf", Size: 592 bytes --]
BEGIN:VCARD
VERSION:2.1
N:Kraus;Paul
FN:Paul Kraus (pkraus@pelsupply.com)
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:pkraus@pelsupply.com
REV:20020416T182124Z
END:VCARD
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Bash Scripts
2002-10-10 20:34 Bash Scripts Paul Kraus
@ 2002-10-10 21:13 ` Don Petrowski
2002-10-10 21:16 ` Ray Olszewski
2002-10-10 21:33 ` Don Petrowski
2 siblings, 0 replies; 4+ messages in thread
From: Don Petrowski @ 2002-10-10 21:13 UTC (permalink / raw)
To: Paul Kraus; +Cc: linux-newbie
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.
On Thu, 2002-10-10 at 15:34, Paul Kraus wrote:
> 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
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Bash Scripts
2002-10-10 20:34 Bash Scripts Paul Kraus
2002-10-10 21:13 ` Don Petrowski
@ 2002-10-10 21:16 ` Ray Olszewski
2002-10-10 21:33 ` Don Petrowski
2 siblings, 0 replies; 4+ messages in thread
From: Ray Olszewski @ 2002-10-10 21:16 UTC (permalink / raw)
To: Paul Kraus, linux-newbie
At 04:34 PM 10/10/02 -0400, Paul Kraus wrote:
>[...]
>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) ...
>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).
>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 ray@comarre.com
-------------------------------------------------------------------------------
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Bash Scripts
2002-10-10 20:34 Bash Scripts Paul Kraus
2002-10-10 21:13 ` Don Petrowski
2002-10-10 21:16 ` Ray Olszewski
@ 2002-10-10 21:33 ` Don Petrowski
2 siblings, 0 replies; 4+ messages in thread
From: Don Petrowski @ 2002-10-10 21:33 UTC (permalink / raw)
To: Paul Kraus; +Cc: linux-newbie
[-- Attachment #1: Type: text/plain, Size: 952 bytes --]
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 tuxtracker@192.168.0.253:. >> /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.
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 240 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2002-10-10 21:33 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-10-10 20:34 Bash Scripts Paul Kraus
2002-10-10 21:13 ` Don Petrowski
2002-10-10 21:16 ` Ray Olszewski
2002-10-10 21:33 ` Don Petrowski
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox