Shell Cheatsheet

2017-03-12  本文已影响0人  捕鲸小能手

变量

变量定义

var="hello"
echo $var ${var}-name

位置参数

param desc
$0 script file
$1 .. $n script params
$# param count
$@ $* all params
$? most recent ret code
$! most recent background job pid

$@ vs $*

# script.sh
function main() {
   echo 'I got ' $# ' args'
}
main $*
main $@
main "$*"
main "$@"

# output
$ sh script.sh 'a b c' d e
I got 5 args
I got 5 args
I got 1 args
I got 3 args

条件控制

注意项

逻辑测试

param desc
(e) ([ -n "hello" ])
not e [ ! -n "hello" ] 等价于 ! [ -n "" ]
e1 and e2 [ -n "" -a -n "" ] 等价于 [ -n "" ] && [ -n "" ]
e1 or e2 [ -n "" -o -n "" ] 等价于 [ -n "" ] [ -n "" ]

文件测试

param desc
-d FILE is directory
-f FILE is file
-e FILE file or dir exists
-x FILE excutable
f1 -nt f2 f1 newer then f2
f1 -ot f2 f1 older then f2
... man test

字符串测试

param desc
-z str len(str) == 0
-n str len(str) > 0
s1 = s2 s1 == s2
s1 != s2 s1 != s2
s1 > s2 s1 > s2
!(s1 < s2) s1 >= s2
s1 < s2 s1 < s2
!(s1 > s2) s1 <= s2
... man test

数字测试

param desc
n1 -eq n2 algebraically ==
n1 -ne n2 algebraically !=
n1 -gt n2 algebraically >
n1 -ge n2 algebraically >=
n1 -lt n2 algebraically <
n1 -le n2 algebraically <=
... man test

直接测试

$ test expression && echo yes
$ [ expression ] && echo yes
$ [[ expression ]] && echo yes

条件判断

if [[ expression ]]; then
  # ...
elif [[ expression ]]; then
  # ...
else
  # ...
fi

条件选择

case VAR in
  var1) cmd1 ;;
  var2) cmd2 ;;
  # ...
  *) cmd ;;
esac

循环控制

for

for var in (list); do
  # ...
  break
  continue
done

for ((i=1, j=10; i <= 10; i++, j--)); do
  # ...
done

while

while ((1)); do
  # ...
  break
  continue
done

while true; do
  # ...
done

until

until [[ -n "" ]]; do
  # ...
  break
  continue
done

select DAY in Mon Tue Wed Thu Fri Sat Sun; do
case $DAY in
Mon) echo "Today is Monday" ;;
Tue) echo "Today is Tuesday" ;;
# ...
*) echo "Unkown day, stop" && break ;;
esac
done


# 函数

```shell
function foo() {
  local var=0
  return $var
} 
上一篇下一篇

猜你喜欢

热点阅读