Page 1 of 1

Bash Scripting - I/O Redirection redirecting i/o in bash scripts Rate Topic: -----

#1 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Post icon  Posted 28 November 2009 - 01:16 PM

This time, we are going to explain how to redirect standard input/output. OK, in case you need more explanation about I/O, read the following paragraph:

There are always three default files open, stdin (the keyboard), stdout (the screen), and stderr (error messages output to the screen). These, and any other open files, can be redirected. Redirection simply means capturing output from a file, command, program, script, or even code block within a script and sending it as input to another file, command, program, or script.
Each open file gets assigned a file descriptor. The file descriptors for stdin, stdout, and stderr are 0, 1, and 2, respectively. For opening additional files, there remain descriptors 3 to 9. It is sometimes useful to assign one of these additional file descriptors to stdin, stdout, or stderr as a temporary duplicate link. This simplifies restoration to normal after complex redirection and reshuffling.

OK, now the syntax:
#this one redirects the output of command to the file "mylog"
#note that if the file doesn't exist, it is created
#and if it exists, it's previous contents will be overwritten
command > mylog

#to avoid overwriting, use this
command >> mylog

#redirect stderr to file when executing command
command 2> mylog

#this one is called a pipe
#it redirects command1's output to command2's input
command1 | command2

#this one creates an empty file or truncates if it exists
#the result is an empty file
> filename

#this one redirects descriptor M to descriptor N

#for example, this one redirects stderr to stdout
a_bad_command 2>&1

#open "file" for input/output and assign fd 3 to it.
exec 3<> file

#close output to fd 3
exec 3>&-

Don't panic! you think it's too much but it isn't... look at this script:

exec 3> log
ping -c 3 >&3
exec 3>&-

exit 0
This simply creates the file "log" and assigns fd 3 to it for output (to the file) then does the ping and sends it's output to fd 3, then closes output to fd 3... wasn't that obvious ? :D

Understanding redirections needs a bit of practice... so here's a script I wrote which reconnects the ppp connection in case it is disconnected:
#redirect ping's error message to stdout then pipe it to egrep and count the matches lines
#then assign the number of matches to variable status
status=$(ping -c 3 2>&1 | egrep -c "\<unknown\>|\<unreachable\>")

#if it  is zero then we are connected to internet already, so exit with sucess
if [ $status -eq 0 ]; then
	echo "Connected"
	exit 0
#if execution reaches here, then we are not connected to internet
echo "reconnecting..."

#close all instances of ppp
#redrect both stderr and stdout to a log file
poff -a &> /home/kian/bin/recon-log
pon dsl-provider &> /home/kian/bin/recon-log

Redirection is quite useful as you see in the above script, just use them practically until you understand the concept :) Hope this tutorial was useful for you!

Is This A Good Question/Topic? 0
  • +

Page 1 of 1