LinuxQuestions.org
Help answer threads with 0 replies.
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 04-02-2012, 11:44 AM   #1
pubudumj
LQ Newbie
 
Registered: Jan 2012
Posts: 16

Rep: Reputation: Disabled
Smile Help With shell script


#!/bin/bash

tar -cvf /dev/st0 /www /home 2>/dev/null


[ $? -eq 0 ] && status="Success!" || status="Failed!!!"


mail -s 'Backup status' vivek@nixcraft.co.in<<END_OF_EMAIL

The backup job finished.

End date: $(date)
Hostname : $(hostname)
Status : $status

END_OF_EMAIL


Could someone please explain me this script line by line?
 
Old 04-02-2012, 11:57 AM   #2
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
I can try no warranty

Quote:
Originally Posted by pubudumj View Post
#!/bin/bash

tar -cvf /dev/st0 /www /home 2>/dev/null # tar up the file to /home


[ $? -eq 0 ] && status="Success!" || status="Failed!!!" # if tar is not an error code of 1 set status to Success else Failed


mail -s 'Backup status' vivek@nixcraft.co.in<<END_OF_EMAIL # Send me an email

The backup job finished.

End date: $(date) # run the date command
Hostname : $(hostname) # run the hostname command
Status : $status # print status from line above

END_OF_EMAIL


Could someone please explain me this script line by line?
Email should look like this:
Code:
The backup job finished.

Mon Apr 2 11:58:20 CDT 2012
MYHOST.MYDOMAIN
Success!

Last edited by Sydney; 04-02-2012 at 11:59 AM.
 
Old 04-02-2012, 12:04 PM   #3
pubudumj
LQ Newbie
 
Registered: Jan 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thanks mate for your kind reply...

Actually i want to know about this line,..

[ $? -eq 0 ] && status="Success!" || status="Failed!!!"
 
Old 04-02-2012, 12:14 PM   #4
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
Quote:
Originally Posted by pubudumj View Post
Thanks mate for your kind reply...

Actually i want to know about this line,..

[ $? -eq 0 ] && status="Success!" || status="Failed!!!"
if you go to the terminal and run a command that gives you an error message and then
Code:
echo $?
it will return a 1. If the last command was successful is till return a 0. -eq is an evaluation for equals then you have the results if true the first thing || (or) is false the second
 
Old 04-02-2012, 12:27 PM   #5
pubudumj
LQ Newbie
 
Registered: Jan 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by Sydney View Post
if you go to the terminal and run a command that gives you an error message and then
Code:
echo $?
it will return a 1. If the last command was successful is till return a 0. -eq is an evaluation for equals then you have the results if true the first thing || (or) is false the second

Thanks sydney...Thank you very much..Very clear description.I understood them all Thanks again
 
Old 04-02-2012, 12:29 PM   #6
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
Quote:
Originally Posted by pubudumj View Post
Thanks sydney...Thank you very much..Very clear description.I understood them all Thanks again
LOL is till should be it will, sorry, but I am happy you found it clear.
 
1 members found this post helpful.
Old 04-02-2012, 12:32 PM   #7
pubudumj
LQ Newbie
 
Registered: Jan 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by Sydney View Post
LOL is till should be it will, sorry, but I am happy you found it clear.

Hey sydney im new to Linux..And im going to do RHCSA in this month..I think you are a person that have a good linux knowledge. Can i found you on Fb or twitter? Or your mail address?
 
Old 04-02-2012, 12:35 PM   #8
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
I have accepted your friends request. I have been studying for that test for a while and work with Red Hat every day. If you are new to Linux I would suggest either their classes or a ton of time on systems. I would be happy to help you, but this forum is probably your best bet on ways to find out stuff that is puzzling.
 
Old 04-02-2012, 12:37 PM   #9
pubudumj
LQ Newbie
 
Registered: Jan 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thumbs up

yeah Sydney,,anyway thanks for all the help..will meet again.Thanks again friend.Have a nice day
 
Old 04-02-2012, 02:21 PM   #10
abarclay
LQ Newbie
 
Registered: Aug 2003
Posts: 26

Rep: Reputation: 6
Just a couple of comments.

The line below doesn't backup the file to /home, it actually backs up both /www and /home to the tape drive. The stuff in /dev usually represent devices on the system. "st" means "Scsi Tape". The 0 means the first scsi tape.
Code:
tar -cvf /dev/st0 /www /home 2>/dev/null
The following line is, quite frankly, a mess. Why someone would write something convoluted like this, I'll never know.
Code:
[ $? -eq 0 ] && status="Success!" || status="Failed!!!"
Here it is, rewritten to be more clear.
Code:
returnCode=$?
if [ $returnCode -eq 0 ]
then
     status="Success!"
else
     status="Failed!"
fi
Code:
mail -s 'Backup status' vivek@nixcraft.co.in<<END_OF_EMAIL
The line above invokes the mail system to send email to vivek@nixcraft.co.in with subject line 'Backup status'.
Normally, the mail system will read the contents of the email from stdin (the keyboard). If you wanted to email he contents of a file (/tmp/mailbody.txt), you could have used this:
Code:
mail -s 'Backup status' vivek@nixcraft.co.in </tmp/mailbody.txt
The "<<" is a special item called a "here document". It means, "I want you to redirect the input of this command from a file - Oh, and by the way, here is the content of the file."
The "END_OF_MAIL" is just a string that defines the start and end of the 'file' contents to be redirected into the command.
This is very useful as it obviates the need to create an intermediate file.

Last edited by abarclay; 04-02-2012 at 02:27 PM.
 
1 members found this post helpful.
Old 04-02-2012, 02:27 PM   #11
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
Quote:
Originally Posted by abarclay View Post
Just a couple of comments.

# The line below doesn't backup the file to /home, it actually backs up both /www and /home to the tape drive. The stuff in /dev usually represent devices on the system. "st" means "Scsi Tape". The 0 means the first scsi tape.
tar -cvf /dev/st0 /www /home 2>/dev/null



[ $? -eq 0 ] && status="Success!" || status="Failed!!!"
# the line above is, quite frankly, a mess. Why someone would write something convoluted like this, I'll never know.
# Here it is, rewritten to be more clear.
Code:
returnCode=$?
if [ $returnCode -eq 0 ]
then
     status="Success!"
else
     status="Failed!"
fi

mail -s 'Backup status' vivek@nixcraft.co.in<<END_OF_EMAIL
# the line above invokes the mail system to send email to vivek@nixcraft.co.in with subject line 'Backup status'.
# Normally, the mail system will read the contents of the email from stdin (the keyboard). If you wanted to email
# the contents of a file (/tmp/mailbody.txt), you could have used this:
mail -s 'Backup status' vivek@nixcraft.co.in </tmp/mailbody.txt

# The "<<" is a special item called a "here document". It means, "I want you to redirect the input of this command from a file.
# Oh, and by the way, here is the content of the file."
# The "END_OF_MAIL" is just a string that defines the start and end of the 'file' contents to be redirected into the command.
# This is very useful as it obviates the need to create an intermediate file.
Yep, I totally glazed over the dev path in that line. This is a much better explanation. Thanks,
 
Old 04-02-2012, 10:50 PM   #12
pubudumj
LQ Newbie
 
Registered: Jan 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by abarclay View Post
Just a couple of comments.

The line below doesn't backup the file to /home, it actually backs up both /www and /home to the tape drive. The stuff in /dev usually represent devices on the system. "st" means "Scsi Tape". The 0 means the first scsi tape.
Code:
tar -cvf /dev/st0 /www /home 2>/dev/null
The following line is, quite frankly, a mess. Why someone would write something convoluted like this, I'll never know.
Code:
[ $? -eq 0 ] && status="Success!" || status="Failed!!!"
Here it is, rewritten to be more clear.
Code:
returnCode=$?
if [ $returnCode -eq 0 ]
then
     status="Success!"
else
     status="Failed!"
fi
Code:
mail -s 'Backup status' vivek@nixcraft.co.in<<END_OF_EMAIL
The line above invokes the mail system to send email to vivek@nixcraft.co.in with subject line 'Backup status'.
Normally, the mail system will read the contents of the email from stdin (the keyboard). If you wanted to email he contents of a file (/tmp/mailbody.txt), you could have used this:
Code:
mail -s 'Backup status' vivek@nixcraft.co.in </tmp/mailbody.txt
The "<<" is a special item called a "here document". It means, "I want you to redirect the input of this command from a file - Oh, and by the way, here is the content of the file."
The "END_OF_MAIL" is just a string that defines the start and end of the 'file' contents to be redirected into the command.
This is very useful as it obviates the need to create an intermediate file.
Wow what a nice explanation...THanks alot mate.Anyway could you please explain me this little thing?

2>/dev/null
 
Old 04-03-2012, 12:06 AM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Code:
2>/dev/null
means send channel 2 ie stderr to virtual device /dev/null; essentially it s a blackhole ie you're just throwing away/ignoring any error msgs
 
  


Reply

Tags
bash scripting, shell scripting



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
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
Shell script calling shell script - List of all nikunjbadjatya Programming 7 04-13-2011 06:27 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

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

All times are GMT -5. The time now is 03:30 AM.

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