标签存档: script

Linux Shell Scripting Tutorial–I/O重定向和文件描述符

I/O Redirection and file descriptors

As you know I/O redirectors are used to send output of command to file or to read input from file. Consider following example
如你所知,I/O重定向是用来将命令行的输出发送到文件或者从文件读取输入信息。
请参考如下的示例:
$ cat > myf
This is my file
^D
(press CTRL + D to save file)
Above command send output of cat command to myf file
上面这个命令将cat命令的输出发送到文件myf
$ cal
Above command prints calendar on screen, but if you wish to store this calendar to file then give command
上面这个命令用来在屏幕上打印日历,但是如果你希望将这个日历存储到文件,那么就用下面的命令
$ cal > mycal
The cal command send output to mycal file. This is called output redirection.
这个cal命令将输出发送到mycal文件。这叫做输出重定向
$ sort
10
-20
11
2

^D
-20
2
10
11
sort command takes input from keyboard and then sorts the number and prints (send) output to screen itself. If you wish to take input from file (for sort command) give command as follows:
sort命令从键盘读取输入,然后将这些数字进行排序并打印(发送)输出给屏幕自身。如果你希望从文件读取输入(作为sort命令的输入),就用下面的这个命令:
$ cat > nos
10
-20
11
2

^D
$ sort < nos
-20
2
10
11

First you created the file nos using cat command, then nos file given as input to sort command which prints sorted numbers. This is called input redirection.
首先,你要用cat命令来创建文件 nos ,然后将nos文件作为输入传给sort命令,sort命令将打印出这些数据,这叫做输入重定向

In Linux (And in C programming Language) your keyboard, screen etc are all treated as files. Following are name of such files
在Linux(和C语言中)你的键盘,屏幕等等都被看作是文件。 下面使这些文件的名称

Standard File File Descriptors number Use Example
stdin 0 as Standard input  Keyboard
stdout 1 as Standard output  Screen
stderr 2 as Standard error  Screen

By default in Linux every program has three files associated with it, (when we start our program these three files are automatically opened by your shell). The use of first two files (i.e. stdin and stdout) , are already seen by us. The last file stderr (numbered as 2) is used by our program to print error on screen. You can redirect the output from a file descriptor directly to file with following syntax
Syntax:
file-descriptor-number>filename

Examples: (Assemums the file bad_file_name111 does not exists)
$ rm bad_file_name111
rm: cannot remove `bad_file_name111': No such file or directory
Above command gives error as output, since you don't have file. Now if we try to redirect this error-output to file, it can not be send (redirect) to file, try as follows:
$ rm bad_file_name111 > er
Still it prints output on stderr as rm: cannot remove `bad_file_name111': No such file or directory, And if you see er file as $ cat er , this file is empty, since output is send to error device and you can not redirect it to copy this error-output to your file 'er'. To overcome this problem you have to use following command:
$ rm bad_file_name111 2>er
Note that no space are allowed between 2 and >, The 2>er directs the standard error output to file. 2 number is default number (file descriptors number) of stderr file. To clear your idea onsider another example by writing shell script as follows:

$ cat > demoscr
if [ $# -ne 2 ]
then
   echo "Error : Number are not supplied"
   echo "Usage : $0 number1 number2"
   exit 1
fi
ans=`expr $1 + $2`
echo "Sum is $ans"

Run it as follows:
$ chmod 755 demoscr
$ ./demoscr

Error : Number are not supplied
Usage : ./demoscr number1 number2

$ ./demoscr > er1
$ ./demoscr 5 7

Sum is 12

For first sample run , our script prints error message indicating that you have not given two number.

For second sample run, you have redirect output of script to file er1, since it's error we have to show it to user, It means we have to print our error message on stderr not on stdout. To overcome this problem replace above echo statements as follows
echo "Error : Number are not supplied" 1>&2
echo "Usage : $0 number1 number2" 1>&2
Now if you run it as follows:
$ ./demoscr > er1
Error : Number are not supplied
Usage : ./demoscr number1 number2

It will print error message on stderr and not on stdout. The 1>&2 at the end of echo statement, directs the standard output (stdout) to standard error (stderr) device.
Syntax:
from>&destination

 


Canonical URL by SEO No Duplicate WordPress Plugin