1.数据结构
字符串
— | — |
---|---|
‘$var’ | 通配符不被转义,$var 不会取值 |
“.*?” | 通配符不被转义,但是$var 会被取值 |
`cmd` | 执行cmd 得到返回值 (似乎不该放在这里,不过先这样吧) |
$ test=123; echo $test
123
$ test=123; echo '$test'
$test
$ test=123; echo "$test"
123
$ echo * # 列出当前目录下的文件
test1 test2 test3
$ echo '*'
*
$ echo "*"
*
另外一种字符串链接的方法,
prefix_${var}_suffix
字符串长度
$ a="12345" ; echo ${#a}
5
字符串截取
$ a=12345; echo ${a:0:4} # 第二个参数是起始位置
1234
$ a=12345; echo ${a:1:4} # 第三个参数是长度
2345
$ a=12345; echo ${a:1:-1} # 第三个参数可以为负,代码倒数的截取位置
234
$ a=12345; echo ${a:1:0} #
$ a=12345; echo ${a:0} # 第三个参数可以没有
12345
$ a=12345; echo ${a:1}
2345
$ a=12345; echo ${a:1:-1} # 掐头去尾
234
另外 ${str/pattern/substitution}
可以用来做字符串替换,如果没有/substitution
则相当于删除, 中间的pattern
可以用通配符
$ a="hello harryhare"; echo ${a/harry*/yourname}
hello yourname
$ a="hello harryhare"; echo ${a/harry*}
hello
数组
$ a=(1 2 3 4 5 a b c d)
$ echo ${a[1]}
2
$ echo ${a[@]} # 全部元素
1 2 3 4 5 a b c d
$ echo ${#a[@]} # 元素个数
9
$ b[5]="T1"
$ b["sth"]="T2"
$ echo ${b["sth"]}
T2
$ echo ${b[@]}
T2 T1
$ echo ${b[0]}
T2
$ echo ${b[1]}
$ echo ${b[5]}
T1
$ b[0]="T3"
$ echo ${b[0]}
T3
$ echo ${b["sth"]}
T3
2.控制结构
if
不要用 >
, <
比较,要用
eq | ne |
---|---|
lt | le |
gt | ge |
$ if [ "1" -lt "2" ]; then echo "True"; else echo "False"; fi;
True
$ if [[ "1" -gt "2" ]]; then echo "True"; else echo "False"; fi;
False
$ if [[ 1 -gt 2 ]]; then echo "True"; else echo "False"; fi;
False
$ if test 1 -gt 2 ; then echo "True"; else echo "False"; fi;
False
我们知道 [ expr ]
和 test expr
是相同的
但是 [ expr ]
和 [[ expr ]]
是有区别的, 似乎是运算优先级的区别 [ ]
>&&
> [[ ]]
case
$ word="b"; case $word in a) echo 1;; b) echo 2;; c) echo 3;; esac
2
while, for
$ n=0; while [ $n -lt 10 ]; do echo $n; ((n++)); done
$ n=0;until [ $n -ge 10 ];do echo $n;((n++));done
$ for ((i=0;i<10;i++)); do echo $i; done
$ for name in "aa" "bb" "cc"; do echo $name; done
$ names=("aaa" "bbb"); for name in ${names[@]}; do echo $name; done
3.运算
$ n=1
$ ((n++))
$ m=$((n+1))
$ echo $n
2
$ echo $m
3
4.函数
# echo
$ function hello() { echo "hello123"; }
$ hello
hello123
# return
$ function hello2() { return "hello123"; }
$ echo hello2
hello2
$ hello2
-bash: return: hello123: numeric argument required
$ function test() { echo $*; }
$ test 1 2 3
1 2 3
$ function test() { echo $1; }
$ test 1 2 3
1
$ function test() { echo $0; }
$ test 1 2 3
-bash
5.子shell
– | – |
---|---|
cmd | |
$(cmd) 和 ‘cmd’ | 获得命令的标准输出 |
(cmd) |
6.特殊变量
— | — |
---|---|
$# | 参数个数 argc |
$0 | argv[0] |
$1 | argv[1] |
${11} | argv[11] |
$? | 上一语句的返回值 |
$$ | 当前bash进程的PID |
$- | 当前的bash参数 |
$* | 作为一个整体的参数 |
$@ | 和$*差不多好像 |
7. ;
&
&&
— | — |
---|---|
command1 & command2 & command3 | 三个命令同时执行 |
command1; command2; command3 | 不管前面命令执行成功没有,后面的命令继续执行 |
command1 && command2 && command2 | 只有前面命令执行成功,后面命令才继续执行 |
可以试下 下面的例子
nc -l 1234 & echo hello # 后面 别忘了 fg %1 下,然后 ctrl +c 掉这个进程
nc -l 1234 ; echo hello
nc -l 1234 && echo hello
[ 1 -eq 2 ] && echo "yes" # 无输出
[ 1 -eq 2 ] ; echo "yes" # 输出 “Yes”
从第一个例子中,我们可以想到为什么,&
放在命令结尾 会另 该命令 后台执行。(相当于第二个命令为空)
8.:
感觉主要四个用处
# 注释
: comment
# 占位
if [ "today" == "2011-08-29" ]; then
:
else
:
fi
# 重定向,清空文件
: < test
# 赋值
$ test=1
$ : ${test:=2}
$ echo $test
1
$ : ${test1:=2}
$ echo $test1
2
9.其他
bash 中函数似乎是不能作为变量的,所以没有闭包一类的概念