1 a=123
2 ( a=321; )
3
4 echo "a = $a" # a = 123
5 # 在圆括号中a变量, 更像是一个局部变量.
----------------------------------------------------------------------------
1 a=123
2 { a=321; }
3 echo "a = $a" # a = 321 (说明在代码块中对变量a所作的修改, 影响了外边的变量)
4
5 # 感谢, S.C.
----------------------------------------------------------------------------
在大括号中, 不允许有空白, 除非这个空白被引用或转义.
echo {file1,file2}\ :{\ A," B",' C'}
file1 : A file1 : B file1 : C file2 : A file2 : B file2 : C
----------------------------------------------------------------------------
The output of a command or commands may be piped to a script.
#!/bin/bash
# uppercase.sh : Changes input to uppercase.
tr 'a-z' 'A-Z'
# Letter ranges must be quoted
#+ to prevent filename generation from single-letter filenames.
exit 0
Now, let us pipe the output of ls -l to this script.
bash$ ls -l | ./uppercase.sh
-RW-RW-R-- 1 BOZO BOZO 109 APR 7 19:49 1.TXT
-RW-RW-R-- 1 BOZO BOZO 109 APR 14 16:48 2.TXT
-RW-R--R-- 1 BOZO BOZO 725 APR 20 20:56 DATA-FILE
----------------------------------------------------------------------------
与(逻辑)
1 if [ $condition1 ] && [ $condition2 ]
2 # 与 if [ $condition1 -a $condition2 ] 相同
3 # 如果condition1和condition2都为true, 那结果就为true.
4
5 if [[ $condition1 && $condition2 ]] # 也可以.
6 # 注意: &&不允许出现在[ ... ]结构中.
----------------------------------------------------------------------------
1 (cd /source/directory && tar cf - . ) | (cd /dest/directory && tar xpvf -)
2 # 从一个目录移动整个目录树到另一个目录
23 # 更优雅的写法应该是:
24 # cd source/directory
25 # tar cf - . | (cd ../dest/directory; tar xpvf -)
26 #
27 # 当然也可以这么写:
28 # cp -a /source/directory/* /dest/directory
29 # 或者:
30 # cp -a /source/directory/* /source/directory/.[^.]* /dest/directory
31 # 如果在/source/directory中有隐藏文件的话.
----------------------------------------------------------------------------
如果变量以-开头进行命名, 可能也会引起问题.
1 var="-n"
2 echo $var
3 # 具有"echo -n"的效果了,这样什么都不会输出的.
阅读(829) | 评论(0) | 转发(0) |