bash编程备忘录

记录一下bash编程相关的知识,方便下次忘了可以来查查


来源


变量基础

变量

1
2
3
4
5
6
7
8
9
10
11
# 清理指定的文件

# 变量赋值
LOG_DIR=/var/log

# 变量使用
cd $LOG_DIR

cat /dev/null > wtmp
echo "Logs cleaned up."
exit 0

静态变量

使用关键词 readonly

1
2
3
4
5
6
MY_URL="http://www.shiyanlou.com"

readonly MY_URL

# 此处会报错
MY_URL=2233

小括号

1
2
3
4
5
6
7
a=123
# 作用域不同
( a=321; )
echo "$a" # 123

arr=(1 4 5 7 9 2) # 数组
echo ${arr[3]} # 7

中括号

1
2
3
4
5
6
7
a=5
if [ $a -lt 10 ] # test语句
then
echo "a: $a"
else
echo 'a>10'
fi

详细的 test 判别符号见下


花括号

1
2
3
4
5
6
7
8
9
if [ ! -w 't.txt' ]; then
touch t.txt
fi
echo 'test text' >> t.txt
cp t.{txt,back} # 变量拆分

a=123
{ a=321; } # 作用域相同
echo "a = $a" # 321

竖线

管道

1
2
3
# 将输入的值全部大写
ls -l | tr 'a-z' 'A-Z'
exit 0

三元表达式

1
2
3
a=10
(( t=a<50?8:9 ))
echo $t

破折号

案例:压缩文件

1
2
3
4
5
6
7
8
9
10
11
BACKUPFILE=backup-$(date +%m-%d-%Y)

# if not assign from cmd, then use the default $BACKUPFILE
archive=${1:-$BACKUPFILE}

tar cvf - `find . -mtime -1 -type f -print` > $archive.tar

gzip $archive.tar
echo "Directory `pwd` backed up in archive file \"$archive.tar.gz\""

exit 0

特殊符号

1
2
3
4
5
6
7
8
9
10
echo "传入参数的数量: ${#}"
echo "显示参数内容: ${*}"
echo "显示参数内容: ${@}"

if [ ${#} -ge 2 ]; then
# 取传参的值
echo "the second var is $2"
fi

echo "当前进程的 pid: ${$}"

计算数学表达式

1
2
3
echo `expr 2 + 3`

echo `awk 'BEGIN{a=2.4+6.5;print a}'

判断

if

1
2
3
4
5
6
7
8
9
10
#!/bin/bash

echo hello; echo there

filename=ttt.sh
if [ -e "$filename" ]; then
echo "File $filename exists."; cp $filename $filename.bak
else
echo "File $filename not found."; touch $filename
fi; echo "File test complete."

elif

1
2
3
4
5
6
7
8
9
10
11
a=10
b=20
if [ $a == $b ]; then
echo "a == b"
elif [ $a -gt $b ]; then
echo "a > b"
elif [ $a -lt $b ]; then
echo "a < b"
else
echo "Something went wrong"
fi

case

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash

varname=b

case "$varname" in
[a-z]) echo "abc";;
[0-9]) echo "123";;
esac

read aNum
case $aNum in
1) echo 'You have chosen 1'
;;
*) echo 'other case'
;;
esac

test

image1.png
image2.png
image3.png


循环

for 循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for item in `seq 2 2 10`; do
echo "The value is: $item"
done

for str in 'This is a string'; do
echo $str
done

# The value is: 2
# The value is: 4
# The value is: 6
# The value is: 8
# The value is: 10
# This is a string

打印输入的参数

1
2
3
4
5
6
7
#!/bin/bash
# 测试for循环,打印传入的参数

for var in $@
do
echo "${var}"
done

常见的 for 循环

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
# 另一种形式的for循环
# 生成五个个随机数0-9之间的数字

for (( i = 0; i < 5; i++ ))
do
echo -n $(( ${RANDOM} * 10 / 32767 )) " "
done

echo ""

while 循环

1
2
3
4
5
6
7
8
9
10
11
12
13
int=1
while(( $int <= 5 )); do
echo $int
let "int++"
done

# ====

echo 'press <CTRL-D> exit'
echo -n 'Who do you think is the most handsome: '
while read MAN; do
echo "Yes! $MAN"
done

无限循环

1
2
3
4
5
6
7
8
9
while : ; do
# cmd
done

while true; do
# cmd
done

for (( ; ; ))

函数

1
2
3
4
5
6
7
8
9
10
11
12
domeFun(){
for var in $*; do
echo -n "$var "
done
echo ""
return $(( 2+3 ))
}

domeFun 1 2 3 4 5 6

# get function result
echo $?