LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-20-2004, 12:44 PM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
Smile Question about Bash script for Linux


Question about Bash script for Linux

--------------------------------------------------------------------------------

1) When running a script and it is piped to /dev/null &, what is it mean ?
/usr/local/sbin/quotacheck1 > /dev/null &

2) When running a script and it is piped to /dev/null, what is it mean ?
killall checkin2.sh > /dev/null

3) When running a script and it is piped to /dev/null 2>&1 &, what is it mean ?
What is a 2 for ?
What is a &1 for ?
What is a & that goes after a 1 means ?
/etc/checkin2.sh > /dev/null 2>&1 &

Thank you for your help.
 
Old 04-20-2004, 12:52 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
/dev/null is like a black hole - everything you send there disappears.
>/dev/null means to redirect the output to go to /dev/null -- in other words not have coammnd out appear on the screen

The ampersand at the end of a line means to execute the line in a background process.

> /dev/null 2>&1 &

means: send standard output to the bit bucket (/dev/null), plus whatever would come out on stderr (2 - standard error output) make that go wherever stdout ( 1 - standard output ) is going. & at the end of the line = run in background. the &> is called a dup (for duplicate): creating a duplicate file descriptor, in this case, whatever goes to 2 ends up in 1

0 - stdin
1 - stdout
2 - stderr

Last edited by jim mcnamara; 04-20-2004 at 12:54 PM.
 
Old 04-20-2004, 12:53 PM   #3
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
1) The output just gets sent into oblivion. The ampersand sends the command into the background.
2) See above minus the backgrounding
3) The last ampersand backgrounds the task. 1=stdout (standard output stream) and 2=stderr (standard error stream). "2>&1" redirects messages from stderr to stdout.
 
Old 04-20-2004, 01:04 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Redirecting (the > symbol) makes all standard output from the program go to the specified file. Redirecting to /dev/null means to send the output to oblivion. So basically the script doesn't want any of the output from quotacheck1, etc. to be shown.

The & at the end of a line tells the shell to run the program in the background. Otherwise you would have to wait for the program to finish before you got a prompt back again.

The 2>&1 redirection means to send standard output and error output messages to the file (/dev/null = oblivion).
 
Old 04-20-2004, 01:10 PM   #5
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
Reply

Thank you jim mcnamara and david_ross for your help.

"2>&1" redirects messages from stderr to stdout.

Is it true that the standard in is by default the screen which is the terminal where the user type it in on a keyboard ?
Is it true that the standard out is by default the screen when 2>&1 is used?
Is it true that the standard error is by default the screen when 2>&1 is used?
 
Old 04-20-2004, 01:26 PM   #6
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Yes to all 3
 
Old 04-20-2004, 01:36 PM   #7
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
unix was written in C.

By default C opens three file descriptors - 0, 1, 2
and they are called stdin, stdout, stderr.

The same concept carries over to the command shell.

A lot of C langauge conventions permeate unix.
 
Old 04-20-2004, 01:47 PM   #8
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
Thank you david_ross and jim mcnamara for your help.
 
Old 04-21-2004, 11:41 AM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: Reply

Quote:
Originally posted by Linh
Is it true that the standard out is by default the screen when 2>&1 is used?
Is it true that the standard error is by default the screen when 2>&1 is used?
Yes, but both are also true when 2>&1 is not used.
 
Old 04-21-2004, 11:54 AM   #10
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
reply

Thank you HKo for the additional answer.
 
  


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
Bash script question deiussum Programming 6 08-14-2004 11:43 AM
bash script question djgerbavore Linux - Newbie 3 07-08-2004 03:39 PM
bash script question xscousr Programming 5 07-03-2003 05:04 PM
Bash script question J_Szucs Linux - General 4 05-29-2003 08:48 AM
A bash script question J_Szucs Programming 2 05-13-2003 02:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:04 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