Sunday, May 18, 2014

Unix Shell Redirection(2)

1. exec to change the file descriptor
./script_2:
 #! /bin/bash  

 exec 5>&1 #save the current standard output to fd 5  
 exec 6>&2 #save the current standard error to fd 6  

 #redirect standard output to ./text_output  
 #redirect standard error to ./text_error  
 exec 1>./text_output  
 exec 2>./text_error  

 if [ echo "h" ] #error here, output to standard error(text_error)  
 then :  
 fi  

 echo "Hello world!"  
 # output to standard output(text_output)  

 exec 1>&5 #restore standard output to terminal  
 exec 2>&6 #restore standard error to terminal  

 if [ echo "h" ]  
 then :  
 fi  
 #now output error information to terminal  
 #output(terminal): ./script_2: line 21: [: echo: unary operator expected

 echo "Hello world"  
 #now output standard output to terminal  
 #output(terminal): Hello world

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_2  
 ./script_2: line 21: [: echo: unary operator expected  
 Hello world  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat ./text_output  
 Hello world!  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat ./text_error  
 ./script_2: line 11: [: echo: unary operator expected  
===================================
script_2:
 #! /bin/bash  

 exec 5>&1  
 exec 6>&2  

 #">>" make shell append string to the file while doing the redirection  
 exec 1>>./text_output  
 exec 2>>./text_error  

 if [ echo "h" ] #error here, append error to text_error  
 then :  
 fi  
 echo "Hello world from script!" #append string to text_output  

 exec 1>&5  
 exec 2>&6  

text_output: already exists, and contain "Hello world!"
text_error: already exists, and contain "Hello world!"

terminal:
It append string to the end of both files.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_2  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat ./text_error  
 Hello world!  
 ./script_2: line 14: [: echo: unary operator expected  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat ./text_output  
 Hello world!  
 Hello world from script!  
====================================
text_output already exists and contain some string there.

script_2:
 #! /bin/bash  
 exec 5>&1  
 #">" will truncate the "text_output" file and make it empty here  
 exec 1>./text_output  
 echo Hello world!  
 echo Amazing world!  

 cat ./text_output  
 #text_output content:  
 #Hello world!  
 #Amazing world!  
 #  
 #text_output's original content is gone.
 #This indicates that "truncate" operation only happens at the beginning  
 #of this script's process, for all remaining print out, it will be appended   
 #to the end of text_output file  
 #If using "exec 1>>..", then it will never truncate, just append all the time.  
 # and text_output's original content is still there.
 exec 1>&5  

2. exec to execute command
./script_1:
 #! /bin/bash  
 echo "script_1 is running!"  
 echo $1 $2  


 ./script_2:
 #! /bin/bash  
 echo "script_2 is running"  
 exec ./script_1 Hello world  
 echo "back to script_2!"  

terminal:
Note: the last echo command in script_2 is never run.
Because exec is the one-way command, it will launch the ./script_1, and then it never come back.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_2  
 script_2 is running  
 script_1 is running!  
 Hello world  

No comments:

Post a Comment