Sunday, May 18, 2014

Unix Shell Redirection(1)

1. no clobber:
./script_2:
 #! /bin/bash  

 echo "Hello world" > text  
 set -C  
 echo "Amazing world!" >| text  
 echo "Hello world!" > text  

terminal:
set -C will force the shell check if the output file is already existed, if yes, it will not let user to output content in there. >| will explicitly "ignore" such check and output content inside.
So, first echo command, truncate the file text and output "Hello world" there.
Second echo command, still truncate the file text and output string there.
Third echo command failed, since "set -C" already enable the redirection check, and it doesn't use the ">!" to avoid the check.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_2  
 ./script_2: line 7: text: cannot overwrite existing file  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat ./text  
 Amazing world!  

2. Provide input with "<<" "<<-"
terminal:
2nd command "<<-" will make shell ignore all tab characters in the input.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat<<EOF  
 Hello world!  
 This is a multi-line input!  
 EOF  
 Hello world!  
 This is a multi-line input!  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat<<-EOF  
 Hello world!  
 This is a multi-line input!  
 EOF  
 Hello world!  
 This is a multi-line input!  

3. "<" ">" "<>" to open file
text:
 My world!  
 Your world!  
 Hello world!  

script_2:
 #! /bin/bash  

 #"<>" means the file after it can be used to be write or read,   
 #and it really depends on implementation of the command. for grep,   
 #it will just use it for "read"  
 grep world! <>text  

 #"<" means the file after it could only be used to read, but  
 #based on implementation of "echo command", this is useless  
 echo "Amazing world!" <text #output "Amazing world!" to standard output  
 #text doesn't chagne here.  

 echo "Amazing world!" <>text #output "Amazing world!" to standard output  
 #text doesn't change here.  

 #">" means the file after it could only be used to write, and it  
 #will truncate the file if it is already existed.  
 echo "Amazing world!" >text #clear up the text file, and output "Amazing world!"  

 cat ./text  
 #output "Amazing world!"  

 #">>" make output "appended" to the file, instead of removing original content  
 echo "Amazing world!" >>text  
 cat ./text  
 #output:  
 #Amazing world!  
 #Amazing world!  

4. File descriptor manipulation
 #! /bin/bash  

 #unix shell file descriptor:  
 #0: standard input  
 #1: standard output  
 #2: standard error output  

 # "0<text" means making the text file as source of standard input  
 # In real life, "0" can be ignored  
 read var 0<text  
 echo $var #output "Hello world!"  

 # "1>text" means making the text file as the standard output  
 # In real life, "1" can be ignored  
 echo Hello world 1>text  
 # output nothing  

 # "2>text" means making the text file as the standard error output  
 # But following statement just output the "Hello world!" at the   
 # standard output: terminal. It doesn't output error information  
 # so "2>text" is useless here.  
 echo "Hello world!" 2>text  
 #output "Hello world!"  

 echo Hello world 1>text | echo amazing world >&1  
 #output: "amazing world"  
 #this indicates that: "1>text" only applies on first command, after  
 #going through the pipeline, file descriptor 1 still represents the  
 #terminal  

========================================
script_2:
 #! /bin/bash  

 if [ echo "H" ]  
 then  
   :  
 fi  
 echo $?  

terminal:
1) First "script_2" command is trying to "truncate" file ./text, and then redirect the standard error to ./text. So ./text contain one line of error information. "0" at the terminal belongs to information of standard output.
2) Second  "script_2" command is trying to "append" standard error information to file ./text, so ./text just contain 2 lines of error information here.
3) Third "script_2" command firstly redirect the standard error information to ./text(2>./text), and redirect standard output information to standard error, which is ./text now.(1>&2). ./text contains both information from standard output and standard error.
4) Fourth "script_2" command firstly redirect the standard output to standard error(terminal) now, then redirect standard error to ./text. And the result is: standard output is terminal, standard error is ./text. This indicates that: order of redirection is very important!
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_2 2>./text  
 0  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat ./text  
 ./script_2: line 3: [: echo: unary operator expected  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_2 2>>./text  
 0  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat ./text  
 ./script_2: line 3: [: echo: unary operator expected  
 ./script_2: line 3: [: echo: unary operator expected  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_2 2>./text 1>&2  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat ./text  
 ./script_2: line 3: [: echo: unary operator expected  
 0  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_2 1>&2 2>./text  
 0  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat ./text  
 ./script_2: line 3: [: echo: unary operator expected  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$   

No comments:

Post a Comment