Advanced Bash-Scripting Guide 读书笔记--Chapter 3 Special Characters

  • Comments #
    从#到行尾,表示注释。可以通过(‘”)转义。
  • Command separator ;
    表示命令结束,用来分割命令。一般是一行一个命令,但是要在一行中包含多个命令的话,需要把它加入其中。
echo hello; echo there
if [ -x "$filename" ]; then    #  Note the space after the semicolon.
  echo "File $filename exists."; cp $filename $filename.bak
else                
  echo "File $filename not found."; touch $filename
fi; echo "File test complete."
  • case 结束符 ;;
case "$variable" in
  abc)  echo "\$variable = abc" ;;
  xyz)  echo "\$variable = xyz" ;;
esac
  • test operator
(( var0 = var1<98?9:21 ))
#                

# if [ "$var1" -lt 98 ]
# then
#   var0=9
# else
#   var0=21
# fi
  • Variable substitution $
    变量前缀
var1=5
var2=23skidoo

echo $var1     # 5
echo $var2     # 23skidoo
  • process ID variable $$
    保存着进程id
  • exit status variable $?
    保存着上一个命令完成的状态,即exit X 中的X(X是一个数字)
  • redirection > &> >& >> < <>
    重定向
    scriptname >filename 把scriptname脚本运行的结果重定向到filename的文件,如果filename的文件已经存在,则覆盖
    command &>filename 把command的stdout和stderr重定向到filename的文件中
    command >&2 把command的stdout重定向到stderr
    scriptname >>filename 把scriptname脚本执行的内容追加到filename的文件中,如果文件不存在,则创建

  • word boundary \<, >
    正则表达式中的字符边界

grep '\<the\>' textfile
  • redirection from/to stdin or stdout -
(cd /source/directory && tar cf - . ) | (cd /dest/directory && tar xpvf -)
# Move entire file tree from one directory to another
# [courtesy Alan Cox <a.cox@swansea.ac.uk>, with a minor change]

# 1) cd /source/directory
#    Source directory, where the files to be moved are.
# 2) &&
#   "And-list": if the 'cd' operation successful,
#    then execute the next command.
# 3) tar cf - .
#    The 'c' option 'tar' archiving command creates a new archive,
#    the 'f' (file) option, followed by '-' designates the target file
#    as stdout, and do it in current directory tree ('.').
# 4) |
#    Piped to ...
# 5) ( ... )
#    a subshell
# 6) cd /dest/directory
#    Change to the destination directory.
# 7) &&
#   "And-list", as above
# 8) tar xpvf -
#    Unarchive ('x'), preserve ownership and file permissions ('p'),
#    and send verbose messages to stdout ('v'),
#    reading data from stdin ('f' followed by '-').
#
#    Note that 'x' is a command, and 'p', 'v', 'f' are options.
#
# Whew!

# More elegant than, but equivalent to:
#   cd source/directory
#   tar cf - . | (cd ../dest/directory; tar xpvf -)
#
#     Also having same effect:
# cp -a /source/directory/* /dest/directory
#     Or:
# cp -a /source/directory/* /source/directory/.[^.]* /dest/directory
#     If there are hidden files in /source/directory.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值