LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help With shell script (http://www.linuxquestions.org/questions/linux-newbie-8/help-with-shell-script-937702/)

pubudumj 04-02-2012 11:44 AM

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?

Sydney 04-02-2012 11:57 AM

I can try no warranty

Quote:

Originally Posted by pubudumj (Post 4642758)
#!/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!


pubudumj 04-02-2012 12:04 PM

Thanks mate for your kind reply...

Actually i want to know about this line,..

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

Sydney 04-02-2012 12:14 PM

Quote:

Originally Posted by pubudumj (Post 4642771)
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

pubudumj 04-02-2012 12:27 PM

Quote:

Originally Posted by Sydney (Post 4642777)
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

Sydney 04-02-2012 12:29 PM

Quote:

Originally Posted by pubudumj (Post 4642789)
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.

pubudumj 04-02-2012 12:32 PM

Quote:

Originally Posted by Sydney (Post 4642791)
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?

Sydney 04-02-2012 12:35 PM

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.

pubudumj 04-02-2012 12:37 PM

yeah Sydney,,anyway thanks for all the help..will meet again.Thanks again friend.Have a nice day

abarclay 04-02-2012 02:21 PM

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.

Sydney 04-02-2012 02:27 PM

Quote:

Originally Posted by abarclay (Post 4642862)
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,

pubudumj 04-02-2012 10:50 PM

Quote:

Originally Posted by abarclay (Post 4642862)
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 :)

chrism01 04-03-2012 12:06 AM

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


All times are GMT -5. The time now is 04:27 AM.