Function of Stdout and Sterr


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Function of Stdout and Sterr
# 1  
Old 09-03-2012
Function of Stdout and Sterr

Hi guys,
my doubs are around of this, stdout and sterr, i don't understand, why, when and the tipically situation where it's usseful.
For example:
Code:
#!/bin/bash
# usugrup: datos y grupos de un usuario
#
USUARIO=$1
id $USUARIO 1>/dev/null 2>&1    .....HERE
ERROR=$?
if [ $ERROR -ne 0 ]
then
  echo "El usuario " $USUARIO "no existe"
  exit

if anyone can help me i'd like that explain to me step by step, please.Smilie

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by radoulov; 09-04-2012 at 09:48 AM..
# 2  
Old 09-03-2012
The commandid valid_userwrites the user ID and group ID information for valid_user to stout (i.e., file descriptor 1). The 1>/dev/nullthrows away that information. The commandid invalid_userwrites a diagnostic message to stderr (i.e., file descriptor 2) saying that invalid_user is not known. The2>&1 redirects the output written to file descriptor 2 to go to the same place as data written to file descriptor 1. So, for both valid_user and invalid_user, the output from the id command will be thrown away.

When id successfully completes (after writing data for valid_user), it terminates with exit code 0. When id completes unsuccessfully (after writing a diagnostic message for invalid_user), it terminates with a non-zero exit code. Immediately after id completes, the value of$?is the exit status of the id command. TheERROR=$?saves id's exit code in $ERROR.

The following if statement in your script writes a note to stdout (instead of stderr) saying the user doesn't exist if id reported an error and returned a non-zero exit status.

Last edited by Don Cragun; 09-03-2012 at 05:38 PM.. Reason: Fixed a typo.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-03-2012
stdout is the file that provides buffered process output for normal data. This means that output going to stdout does not necessarily appear right away.

stderr is unbuffered for error or warning information. Information going here does appear right away.

By convention 0 stdin 1 = stdout 2 = stderr. All processes that have a controlling tty (screen and keyboard: or 0, 1, and 2 redirected to or from some file. When you run a script it becomes stdin, for example.).

If everyone follows the convention then you get errors in one output stream and good stuff separated. >/dev/null 2>&1 means ALL output goes to /dev/null.
The >/dev/null part assigns stdout (1) to the bit bucket, 2>&1 assigns stderr (2) to whatever 1 (stdout) is already "aimed" at. It can be any valid file. /dev/tty is a file and is the controlling terminal.
This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 09-04-2012
Sir? the response was great, Now I can understand much better the things, Thanks a lot.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

4. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

5. Shell Programming and Scripting

Stdout in file

I cannot figure out what is wrong.... I have 3 files with IP addresses: file1 134.123.3.236 file2 134.123.3.235 file3 134.123.5.237 I type "prob1 Oops x2x3x4". Then my code creates file with name Oops and first line x2x3x4. Moreover, my code generate IP and it gives to file Oops as a second... (1 Reply)
Discussion started by: Manu1234567
1 Replies

6. UNIX for Dummies Questions & Answers

STDout

Hi, I have a program set to read in a text file, change certain characters and then print the altered version to the screen but does anyone know how to save the new version as another text file? And, if possible, how to specify the file name, and perhaps location? Thanks! (2 Replies)
Discussion started by: PerlNutt
2 Replies

7. Programming

How to step in one function after the function be executed in gdb?

In gdb, I can call one function with command "call", but how can I step in the function? I don't want to restart the program, but the function had been executed, gdb will execute next statement, and I don't know how to recall the function. (4 Replies)
Discussion started by: 915086731
4 Replies

8. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

9. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

10. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies
Login or Register to Ask a Question