当前位置 博文首页 > 测试开发小记:Linux Bash编程
在Linux系统介绍中,介绍了shell的多个版本,现在的Linux发行版基本都默认使用bash(Bourne Again shell),兼容Bourne shell (sh),本文将简要介绍Bash编程语法。
定义变量
your_name="abc"
echo $your_name
拼接字符串
your_name="world"
your_name2="hello,$your_name!"
echo $your_name2
数组
array_name=(value0 value1 value2 value3)
valuen=${array_name[n]} # 数组取值
array_name[0]=value0 # 赋值
数组实例:
my_array=(A B "C" D)
echo "第一个元素为: ${my_array[0]}"
my_array[1]=b
echo "数组的元素为:${my_array[*]}" # 打印所有元素
echo "数组的元素为:${my_array[@]}"
输出:
第一个元素为: A
数组的元素为:A b C D
数组的元素为:A b C D
a="123"
readonly a
unset variable_name #不能删除只读变量
不能删除只读变量
# b=10
# readonly b
# echo $b
10
# unset b
-bash: unset: b: cannot unset: readonly variable
#
显示所有环境变量
env
# 或者
printenv
显示环境变量值
printenv LANG
# 或者
echo $LANG
if condition
then
command1
command2
...
commandN
fi
if和then写在同一行时,用分号分隔。
if [ 2==2 ]; then
echo "true";
else
echo "false";
fi
# 写法一
test expression
# 写法二
[ expression ]
# 写法三
[[ expression ]]
if test 2==2; then echo "true"; fi
if [ 2>1 ]; then echo "true"; fi
if [[ 2>1 ]]; then echo "true"; fi
比较两个变量的大小
a=10
b=20
if [ $a -eq $b ]; then
echo"equal";
elif [ $a -lt $b ]; then
echo "small";
elif [ $a -gt $b ]; then
echo "big";
fi
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
for和do写在同一行时,用分号分隔。
for Ioop in 1 2 3 4 5
do
echo "hello"
done
for Ioop in 1 2 3 4 5;do
echo "hello"
done
循环读取文件内容并输出
for i in $(cat test.txt); do echo $i; done
while condition
do
command
done
int=1
while(( $int<=5))
do
echo $int
let "int++"
done
循环读取文件内容并输出
while read line; do echo $line; done<test.txt
输出:
test1
test222
test3
test4
test5
从标准输入读取输入并赋值给变量
read var
从标准输入读取多个内容
read varl var2 var3
不指定变量(默认赋值给 REPLY)
read
# read a
123
# echo $a
123
# read a b c
1 2 3
# echo $a
1
# echo $b
2
# echo $c
3
#
默认变量
# read
456
# echo $REPLY
456
#
# 注释
# 多行注释
:<<EOF
内容
.......
EOF
vim param.sh:
#!/bin/bash
echo "脚本名称:$0"
echo "脚本运行的当前进程ID号:$$"
echo "参数个数:$#"
echo "所有参数:$*"
echo "第1个参数:$1"
echo "第10个参数:${10}"
echo "return "$?
执行:
# chmod +x param.sh
# ./param.sh 1 2 3 4 5 6 7 8 9 10 1
脚本名称:./param2.sh
脚本运行的当前进程ID号:21097
参数个数:11
所有参数:1 2 3 4 5 6 7 8 9 10 1
第1个参数:1
第10个参数:10
return 0
#
bash会把反引号里面当作一条命令来执行
In:
# echo `date +%y/%m/%d`
20/12/27
# echo `expr 2 + 2`
4
# a=10
# b=20
# echo expr $a + $b
30
# echo $(($a+$b))
30
# echo expr $a - $b
-10
# echo expr $a \* $b
200
# echo expr $b / $a
2
#
# a=10
# b=20
# echo `expr $b % $a`
0
# echo $[$a == $b]
0
# echo $[$a != $b]
1
#
# vim test.sh
# cat test.sh
#!/bin/bash
a=10
b=20
if [ $a -lt $b ]
then
echo "equal"
fi
# chmod +x test.sh
# ./test.sh
equal
#
#!/bin/bash
# 内存使用百分比
free | sed -n '2p' | gawk 'x = int(( $3 / $2 ) * 100) {print x}' | sed 's/$/%/'
# 统计内存
for i in `ps aux | awk '{print $6}' | grep -v 'RSS'`; do
count=$[$count+$i]
done
echo "$count/kb"
# ./test.sh
16%
474608/kb
vim test.sh
read -p "Enter a number:"
factorial=1
for (( count=1; count<=$REPLY; count++))
do
factorial=$[ $factorial * $count ]
done
echo "The factorial of $REPLY is $factorial"
# chmod +x test.sh
# ./test.sh
Enter a number:6
The factorial of 6 is 720
文章标题:Linux Bash编程
本文作者:hiyo
本文链接:https://www.cnblogs.com/hiyong/p/14238495.html
欢迎关注公众号:「测试开发小记」及时接收最新技术文章!