LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 07-01-2009, 06:19 PM   #1
KFC123
Member
 
Registered: May 2009
Posts: 47

Rep: Reputation: 15
Help with bash script


I am writing a simple script to do the following: mount a certain volume in, copy some files to that volume and then umount it


mount /dev/hda1 /mnt
# if error here, echo "error 1"
cp /home/jay/cifi /mnt -r
# if error here, echo "error 2"
umount /dev/hda1
# if error here, echo "error 3"

But for these three command, I would like to check each step is going well, no error. So how can I check that?
 
Old 07-01-2009, 06:32 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Look at the man page for "mount"---specifically the RETURN CODES near the end. Basically, you need to test the return code and branch based on the result.

Note:

1. You normally don't want to mount something at "/mnt". That would hide everything else in the directory.

2. What is this supposed to do?:
cp /home/jay/cifi /mnt -r
The normal syntax is:
cp [OPTION]... SOURCE... DIRECTORY

AND---please confirm if this is homework.
 
Old 07-01-2009, 06:33 PM   #3
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
You can check if the exit status of a previously executed command by checking if the var $? is equal to zero.

Something like:
if [ "$?" = "0" ] ; then
echo "success"
else
echo "failed"
fi

btw your cp command looks like it should be:
cp -r ... instead of the cp ... -r
 
Old 07-01-2009, 06:42 PM   #4
KFC123
Member
 
Registered: May 2009
Posts: 47

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pixellany View Post
Look at the man page for "mount"---specifically the RETURN CODES near the end. Basically, you need to test the return code and branch based on the result.

Note:

1. You normally don't want to mount something at "/mnt". That would hide everything else in the directory.
I just show an example, not really using /mnt

Quote:
2. What is this supposed to do?:
cp /home/jay/cifi /mnt -r
The normal syntax is:
cp [OPTION]... SOURCE... DIRECTORY
Thanks for remind me this, but it works and ... just wonder why it didn't even report a warning?

Quote:
AND---please confirm if this is homework.
Absolutely NOT. I am writing a script to backup a given directory to a disk and run it with cron constantly.
 
Old 07-01-2009, 06:56 PM   #5
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Quote:
Originally Posted by KFC123 View Post
I am writing a script to backup a given directory to a disk and run it with cron constantly.
You may want to look into rsync is you want to keep the backup files in sync rather then cp as you will have copies of old deleted or unused files in your backup. And it's lighter on you system as it will only copy new or changed files.
 
Old 07-01-2009, 07:01 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Thanks for remind me this, but it works and ... just wonder why it didn't even report a warning?
How about that!!


Quote:
AND---please confirm if this is homework.
Absolutely NOT.
OK

Looking at the other reply, there's another form to consider:

if $(mount something somewhere); then
do this
else
do that
fi
 
Old 07-01-2009, 07:07 PM   #7
KFC123
Member
 
Registered: May 2009
Posts: 47

Original Poster
Rep: Reputation: 15
Thanks all of you. It helps
 
Old 07-01-2009, 07:29 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by pixellany View Post
Looking at the other reply, there's another form to consider:

if $(mount something somewhere); then
do this
else
do that
fi
and the slightly simpler:
Code:
if mount something somewhere ; then
   do this
else
   do that
fi
and the even simpler:
Code:
mount something somewhere && do this || do that
eg:

Code:
mount /dev/hda1 /mnt && echo "mount hda1 done" || echo "error 1"
 
Old 07-02-2009, 07:45 AM   #9
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
Even shorter might be ...

Code:
mount /dev/hda1 /mnt || exit 1
cp /home/jay/cifi /mnt -r || exit 2
umount /mnt || exit 3
This acts as a mini if statement.

if_statement && do_this_when_true || do_this_when_false

If you want it to be more informative when an error occurs, try the following. Please note the spaces around the braces, and the semi-colon after each command.

Code:
mount /dev/hda1 /mnt || { echo Error mounting device.; exit 1; }
cp /home/jay/cifi /mnt -r || { echo Error copying files.; exit 2; }
umount /mnt || { echo Error unmounting device.; exit 3; }
 
  


Reply



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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 10:53 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 06:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:32 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
Open Source Consulting | Domain Registration