LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 03-09-2005, 11:02 AM   #1
AUSanders79
Member
 
Registered: Aug 2003
Location: Alabama
Distribution: Slack 10.1/FreeBSD 5.4
Posts: 37

Rep: Reputation: 15
Command Line Options


ok guys, I consider myself pretty self sustaining when it comes to linux. But here's a question I've had for quite some time and never really knew how to google this.

Logrotate rotates your log files according to your specs, and then when it's done, it will usually HUP syslog daemon to get it to check and reopen it's log file. Here's the command I've seen on most instances of logrotate that does this:

/bin/kill -HUP `cat /var/run/syslogd.pid 2>/dev/null` 2>/dev/null || true

My question is this. What does the 2> do? I know what > does as far as redirecting the output to /dev/null, but what is the 2??? I've seen this before in other scripts and such, and I think I have even seen a 1 before. Like someone runs a command "cat myfile.txt 1" or something similar, so I just want to know what these numbers mean. Oh yeah, and what is the || true at the end? Isn't that supposed to be saying "or true"? Thanks for the help!!
 
Old 03-09-2005, 11:07 AM   #2
susefan
Member
 
Registered: Feb 2005
Location: California, USA
Distribution: Novell SuSE
Posts: 39

Rep: Reputation: 15
File descriptor 2 is standard error output.

There are three file descriptors normally attached
to a command,
stdin (standard in) fd=0
stdout (standard out) fd=1
stderr (standard error) fd=2

Sometimes you will see this:

command 2>&1 >command.log

It means, run the command, send stderr to the address of
stdout, send stdout to the file, command.log.
So command.log will contain both the standard output and the
standard error messages.

Hope this helps.
 
Old 03-09-2005, 11:07 AM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
2>/dev/null sends errors to /dev/null. That means you don't want to see any error messages.

Output redirection:
0 = stdin input device such as keyboard
1 = stdout output device usually the console
2 = stderr error message usually displayed at the console
Example: ls nofile > test 2>&1 This sends the error message to a file called test.
Example: ls | tee test This sends output to the screen and to a file called test.
 
Old 03-09-2005, 11:10 AM   #4
AUSanders79
Member
 
Registered: Aug 2003
Location: Alabama
Distribution: Slack 10.1/FreeBSD 5.4
Posts: 37

Original Poster
Rep: Reputation: 15
That's it! Perfect!! Thanks so much!!

Now what about the "|| true" part of the command?

Last edited by AUSanders79; 03-09-2005 at 11:12 AM.
 
Old 03-09-2005, 11:11 AM   #5
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
Read this:
http://www.tldp.org/LDP/abs/html/io-redirection.html

Quote:
Example: ls nofile > test 2>&1 This sends the error message to a file called test.
Actually this will redirect the output and the error mesages to a file called test.
This would send the error message to a file called test:
ls nofile 2>test

Last edited by perfect_circle; 03-09-2005 at 11:48 AM.
 
Old 03-09-2005, 11:28 AM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Quote:
Actually this will redirect the output and the error mesages to a file called test.
I like to use that example as I'm sure to get an error. Also, confuses the crap out of everyone.
But you're right, normally you would get a standard output and an error message.
 
Old 03-09-2005, 11:53 AM   #7
DaFrawg
LQ Newbie
 
Registered: Jun 2004
Location: The Netherlands
Distribution: Slackware 10.1, Debian Sarge
Posts: 28

Rep: Reputation: 15
Quote:
Originally posted by AUSanders79 Now what about the "|| true" part of the command?
|| stands for Logic OR in many programming and scripting languages, including the language bash uses. In scripts it's mostly used to handle errors*. In this case, bash would do the following when it encounters
Code:
/bin/kill -HUP `cat /var/run/syslogd.pid 2>/dev/null` 2>/dev/null || true
1) Do cat with the 'option' /var/run/syslogd.pid and redirect stderr to /dev/null. This will send the process id of syslogd to stdout.
2) Do /bin/kill with the option -HUP plus what was sent to stdout by the last command.
3) Think about ||. If the last expression resulted in a nonzero value, stop evaluation and return 1. Otherwise, continue evaluation and return 0 when the next expression is 0 and 1 when it's nonzero.
4) Do true. This just returns 1.

The function of the script is to send a HUP signal to syslogd and make sure the user gets no errors at all. File descriptor 2 is redirected to /dev/null and the return value of the expression is always 1.

* If people don't believe me, than think about why there are so much occurences of "or die(...)" in PHP scripts.
 
Old 03-09-2005, 03:59 PM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Actually, true returns a '0' since this is what the bash shell uses for 'success'.
The man page for 'true' states that 'success' is returned rather than the number.

However from the stand point of success or failure, DaFrawg's analysis is excellent.
 
Old 03-10-2005, 10:24 AM   #9
DaFrawg
LQ Newbie
 
Registered: Jun 2004
Location: The Netherlands
Distribution: Slackware 10.1, Debian Sarge
Posts: 28

Rep: Reputation: 15
Sorry, my mistake, thanks for correcting it
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Help with command line options(ksh) SeT Programming 2 11-29-2004 06:54 PM
gcj command line options m3rc_2003 Programming 0 12-03-2003 05:40 AM
kmail command line options randyasu Linux - Software 10 09-30-2003 03:02 AM
WINE and command line options Power User Linux - Software 2 09-14-2003 06:16 PM
Command Line Options in C++ crichards Programming 5 03-08-2003 03:42 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration