Linux Bash编程
2021-02-03 21:37
1496 查看
在Linux系统介绍中,介绍了shell的多个版本,现在的Linux发行版基本都默认使用bash(Bourne Again shell),兼容Bourne shell (sh),本文将简要介绍Bash编程语法。
变量
命名规则
- 只能使用英文字母,数字和下划线,首个字符不能以数字开头
- 中间不能有空格,可以使用下划线(_)
- 不能使用标点符号
- 不能使用bash里的关键字(可用help命令查看保留关键字)
定义与使用变量
定义变量
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 } # 数组取值 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
if定义
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
for定义
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
while定义
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命令
- read命令是用于从终端或者文件中读取输入的内部命令
- 读取整行输入
- 每行末尾的换行符不被读入
read命令使用
从标准输入读取输入并赋值给变量
read var
从标准输入读取多个内容
read varl var2 var3
不指定变量(默认赋值给 REPLY)
read
实例
# read a 123 # echo $a 123 # read a b c 1 2 3 # echo $ 1044 a 1 # echo $b 2 # echo $c 3 #
默认变量
# read456 # echo $REPLY 456 #
注释
# 注释 # 多行注释 :<<EOF 内容 ....... EOF
脚本参数传递
- $0 脚本名称
- $1~$n 获取第n个参数:
- $# 传递到脚本的参数个数
- $$ 脚本运行的当前进程ID号
- $* 以一个单字符串显示所有向脚本传递的参数
- $? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误
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=$b 将把变量b的值赋给a
- == 相等 相同则返回true
- != 不相等 不相同则返回true
# a=10 # b=20 # echo `expr $b % $a` 0 # echo $[$a == $b] 0 # echo $[$a != $b] 1 #
- -eq 检测相等
- -ne 检测不相等
- -gt 检测左边是否大于右边
- -lt 检测左边是否小于右边
- -ge 检测左边是否大于等于右边
- -le 检测左边是否小于等于右边
# 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
欢迎关注公众号:「测试开发小记」及时接收最新技术文章!
相关文章推荐
- 谢烟客---------Linux之bash脚本编程---选择执行
- linux笔记之bash编程(二)
- Linux之bash编程基本语法
- 2018-2-4Linux系统管理(5)bash脚本编程参数变量及条件判断
- Bash编程-linux诸多熟记命令
- Linux进阶之bash编程(流程控制if-else)
- linux笔记 第六天 bash编程入门之变量、条件判断、条件测试和for循环等
- Linux命令:bash脚本编程--脚本
- 怎样用 Bash 编程:循环 | Linux 中国
- Linux进阶之bash编程(流程控制-case,select)
- Linux--Shell 编程-bash,命令替换,if分支嵌套,运算,输入输出
- Linux基础入门及系统管理01-bash脚本编程之一变量、变量类型等16
- Linux下Bash编程之條件判斷详解(二)
- 谢烟客---------Linux之bash脚本编程---用户交互
- Linux Socket编程bash服务程序
- Linux--Shell 编程-bash,命令替换,if分支嵌套,运算,输入输出
- 8.17_Linux之bash shell脚本编程入门篇(三)之循环以及函数function的使用
- Linux进阶之bash编程(函数及总结)
- Linux--Shell 编程-bash,命令替换,if分支嵌套,运算,输入输出
- Linux基础入门及系统管理01-bash脚本编程之五字符串测试及for循环21