标签:exec
Generally it can be used for opening and files in shall like open statement in Perl. The following examples illustrate the use of exec for manipulating file descriptors:
exec 3< inputfile # Opens inputfile with file descriptor 3 for reading. exec 4> outputfile # Opens outputfile with file descriptor 4 for writing. exec 5<&0 # Makes fd 5 a copy of fd 0 (standard input). exec 6>&p # Attach fd 6 to co-process.
请看如下代码 #!/bin/bash # # open_file.sh: print the contents of robots.txt shopt -s -o nounset declare LINE exec 3< robots.txt//打开文件读 while read LINE <&3 ; do printf "%s\n" "$LINE" done exit 0 请看代码2: 1 #!/bin/bash 2 # upperconv.sh 3 # Converts a specified input file to uppercase. 4 5 E_FILE_ACCESS=70 6 E_WRONG_ARGS=71 7 8 if [ ! -r "$1" ] # Is specified input file readable? 9 then 10 echo "Can't read from input file!" 11 echo "Usage: $0 input-file output-file" 12 exit $E_FILE_ACCESS 13 fi # Will exit with same error 14 #+ even if input file ($1) not specified. 15 16 if [ -z "$2" ] 17 then 18 echo "Need to specify output file." 19 echo "Usage: $0 input-file output-file" 20 exit $E_WRONG_ARGS 21 fi 22 23 24 exec 4<&0 25 exec < $1 # Will read from input file. 26 27 exec 7>&1 28 exec > $2 # Will write to output file. 29 # Assumes output file writable (add check?). 30 31 # ----------------------------------------------- 32 cat - | tr a-z A-Z # Uppercase conversion. 33 # ^^^^^ # Reads from stdin. 34 # ^^^^^^^^^^ # Writes to stdout. 35 # However, both stdin and stdout were redirected. 36 # ----------------------------------------------- 37 38 exec 1>&7 7>&- # Restore stout. 39 exec 0<&4 4<&- # Restore stdin. 40 41 # After restoration, the following line prints to stdout as expected. 42 echo "File \"$1\" written to \"$2\" as uppercase conversion." 43 44 exit 0 详情:http://www.softpanorama.org/Tools/exec.shtml 按位运算符 ~op1 反运算符 op1<<op2 左移运算符 op1>>op2 右移运算符 op1 & op2 与比较运算符 op1 ^ op2 异或运算符 op1 | op2 或运算符 eg: echo $[2<<4] echo $[2^4] $[ ] 表示形式告诉shell对方括号中的表达式求值 eg: echo $[3+9] 逻辑运算符 && 逻辑与运算 || 逻辑或运算符 echo $[1||1] l 赋值运算符 =,+=,-=,*=,/=,%=,&=,^=、|=,<<=,>>= let count = $count + $change let count += $change [wbm@wmblinux64 myshell]$ var=10 [wbm@wmblinux64 myshell]$ let var+=5 [wbm@wmblinux64 myshell]$ echo $var15 表达式替换 $[] 和 $(( )) 习惯使用$[],所有shell的求值都是用整数完成 $[]可以接受不同基数的数字 [base#n] n表示基数从2到36任意基数 [wbm@wmblinux64 myshell]$ echo $[10#8+191] 199 运算符的优先级(不确定的地方,多加括号) 文件状态测试 格式 test condition 或 [ condition ]使用方括号时,要注意在条件两边加上空格。 文件测试状态 :测试的结果可以用$?的值来判断,0表示成功,其他为失败 -d 目录 -s 文件长度大于0、非空-f 正规文件 -w 可写-L 符号链接 -u 文件有suid位设置-r 可读 -x 可执行-z 字符串为空
标签:exec
原文地址:http://blog.51cto.com/13375232/2054683