Useful Bash examples

GOOD Practices:

     1.   Since bash 3.2, regex should always be unquoted.

use a variable to store your regex, e.g. re='^\*( >| *Applying |.*\.diff|.*\.patch)'; [[ $var =~ $re ]]


      2. Whenever you're making a Bash script, you should always use [[ rather than [


      3. Should better use $(...) instead of `...` for command substitution


      4. Learn to use parameter expansions instead of sed or cut to manipulate simple strings in Bash. If you want to remove the extension from a filename, use ${filename%.*} instead of`echo "$filename" | sed 's/\.[^.]*$//'` or some other dinosaur.


1. get the exit status in the pipeline command

use PIPESTATUS array(bash only)

     

  eg:
  grep foo somelogfile | head -5
  status=${PIPESTATUS[0]}


2. read ----- 不是一行行的读,而是以null(\0)作为delimier

 while read -r -d $'\0'

           -r -------- prevent it from treating backslash as special usage


3. find  xxxx -print0 | xargs -0 command {}

     note:The find command itself uses the -print0 option as mentioned before to tell it to separate the filenames it finds with a NUL byte instead of a newline.

             also it tells xargs, instead of reading whitespace-separated words, to read NUL-delimited words instead

             {} means the output


   find xxxx -exec commands {} \;

   find xxxx -exec commands {} +

     note: th+ (instead of ;) at the end of the -exec action tells find to use an internal xargs-like feature which causes the commands to be invoked only once for every chunk of files, instead of once per file.

    

4. print the nth line of a file

sed -n "${n}p" "$file"

#But above command reads the entire file even if only the third line is desired, which can be avoided by printing line $n using the p command, followed by a q to exit the script.

sed -n "$n{p;q;}" "$file"

5. glob pattern

  • *: Matches any string, including the null string.

  • ?: Matches any single character.

  • [...]: Matches any one of the enclosed characters.


    note: When a glob is used to match filenames, the * and ? characters cannot match a slash (/) character. So, for instance, the glob */bin might match foo/bin but it cannot match /usr/local/bin. When globs match patterns, the / restriction is removed

6. command substitution

`command` or $(command)


7. command group

$ grep -q goodword "$file" && ! grep -q badword "$file" && { rm "$file" || echo "Couldn't delete: $file" >&2; }
(Note: don't forget that you need a semicolon or newline before the closing curly brace!)

8. process substitution

     <() ---- the result as other's input

    >() ----  others as the process's input

not use process substitution
$ head -n 1 .dictionary > file1
$ tail -n 1 .dictionary > file2
$ diff -y file1 file2
Aachen                                     | zymurgy
$ rm file1 file2

use process substitution
$ diff -y <(head -n 1 .dictionary) <(tail -n 1 .dictionary)
Aachen                              | zymurgy

9. sourcing a script

$ . ./myscript
note: do above in a script means run the "myscript" in current shell environment not in a subshell environment


10. using getopts to parse command options

http://wiki.bash-hackers.org/howto/getopts_tutorial

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值