Shell编程

Online Resources: http://tldp.org/LDP/abs/html/...

set -e
Exit the script if an error happens

圆括号
括号中的命令将会新开一个子shell顺序执行:

(bar="hello world")
echo $bar    # 无输出

&&
&& lets you do something based on whether the previous command completed successfully

$ true && echo "Things went well"
Things went well

$ false && echo "This will always run"

Internal Variables

http://tldp.org/LDP/abs/html/...
$PATH, ${PATH}: Path to binaries
$PWD, ${PWD}: Working directory (directory you are in at the time)

Internal Commands

1. export:
http://blog.51cto.com/beyond3...
export command is used to export a variable or function to the environment of all the child processes running in the current shell.

a.sh

#!/bin/sh
echo "$foo"
echo "$bar"

b.sh

foo="hello world"
bar="hello"
export foo
./a.sh

等价于==>

bar="hello" 
foo="hello world" ./a.sh

2. source 或 点操作符"."
imports code into the script

# import utils
. scripts/utils.sh
source scripts/utils.sh

External Commands

In general, an external command in a script forks off a subprocess, whereas a Bash builtin does not. For this reason, builtins execute more quickly than their external command equivalents.
Basic commands: ls, cat, rm, ...

相关推荐