bash脚本备忘

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

不要用 >, < 比较,要用

eqne
ltle
gtge
$ 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
$0argv[0]
$1argv[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 中函数似乎是不能作为变量的,所以没有闭包一类的概念

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值