

shell脚本基本语法详解__acme_的博客-CSDN博客_shell脚本基本语法
source link: https://blog.csdn.net/qq_18297675/article/details/52693464
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

==> 学习汇总(持续更新)
==> 从零搭建后端基础设施系列(一)-- 背景介绍
==> 学习汇总(持续更新)
==> 从零搭建后端基础设施系列(一)-- 背景介绍
编写shell脚本的时候,最前面要加上一行:#!/bin/bash
,因为linux里面不仅仅只有bash一个解析器,还有其它的,它们之间的语法会有一些不同,所以最好加上这一句话,告诉系统要用这个解析器。
一.shell变量
shell变量和一些编程语言不同,一般shell的变量赋值的时候不用带“$”
,而使用或者输出的时候要带“$”
。加减乘除的时候要加两层小括号。括号外面要有一个“$”
,括号里面的变量可以不用“$”
。需要注意的是,变量赋值,变量使用的时候不能有空格,否则会被解析成命令,报错无此命令。
#!/bin/bash
a=10
b=20
c="this is a test"
d=$((a+b))
e=$((a-b))
f=$((a*b))
g=$((a/b))
h=$((a%b))
i=$((a**3))
echo $c
echo "a = "$a #输出a的值
echo "b = "$b #输出b的值
echo "a+b = "${d} #输出a+b的值
echo "a-b = "${e} #输出a-b的值
echo "a*b = "${f} #输出a*b的值
echo "a/b = "${g} #输出a/b的值
echo "a%b = "${h} #输出a%b的值
echo "a^3 = "${i} #输出a的3次方的值
echo "a+b = "$((a+b)) #输出a+b的值
echo "a-b = "$((a-b)) #输出a-b的值
echo "a*b = "$((a*b)) #输出a*b的值
echo "a/b = "$((a/b)) #输出a/b的值
echo "a%b = "$((a%b)) #输出a%b的值
echo "a^3 = "$((a**3)) #输出a的3次方的值
echo $((a+b*a-b/a+a%b+a**2)) #表达式可以很长
结果如下图:
二.shell变量表达式
#!/bin/bash
str="a b c d e f g h i j"
echo "the source string is "${str} #源字符串
echo "the string length is "${#str} #字符串长度
echo "the 6th to last string is "${str:5} #截取从第五个后面开始到最后的字符
echo "the 6th to 8th string is "${str:5:2} #截取从第五个后面开始的2个字符
echo "after delete shortest string of start is "${str#a*f} #从开头删除a到f的字符
echo "after delete widest string of start is "${str##a*} #从开头删除a以后的字符
echo "after delete shortest string of end is "${str%f*j} #从结尾删除f到j的字符
echo "after delete widest string of end is "${str%%*j} #从结尾删除j前面的所有字符包括j
结果如图:
三.shell测试判断test或[]
需要注意的是使用[]的时候必须要每个变量之间都要有空格,和左右中括号也要有空格,否则报错。
echo "Please input a filename: "
read filename
echo "by test\n"
test -f $filename && echo "the file is ordinary file" || echo "the file is not ordinary file"
test -d $filename && echo "the file is document folder" || echo "the file is not document folder"
test -r $filename && echo "the file can read" || echo "the file can not read"
test -w $filename && echo "the file can write" || echo "the file can not write"
test -x $filename && echo "the file can executable" || echo "the file can not executable"
echo "by []\n"
[ -f $filename ] && echo "the file is ordinary file" || echo "the file is not ordinary file"
[ -d $filename ] && echo "the file is document folder" || echo "the file is not document folder"
[ -r $filename ] && echo "the file can read" || echo "the file can not read"
[ -w $filename ] && echo "the file can write" || echo "the file can not write"
[ -x $filename ] && echo "the file can executable" || echo "the file can not executable"
echo "Please input two numbers:"
read num1
read num2
echo "num1 = "${num1}
echo "num2 = "${num2}
echo "by test\n"
test $num1 -eq $num2 && echo "num1 == num2" || echo "num1 != num2"
test $num1 -ne $num2 && echo "num1 != num2" || echo "num1 == num2"
test $num1 -gt $num2 && echo "num1 > num2" || echo "num1 <= num2"
test $num1 -lt $num2 && echo "num1 < num2" || echo "num1 >= num2"
test $num1 -ge $num2 && echo "num1 >= num2" || echo "num1 < num2"
test $num1 -le $num2 && echo "num1 <= num2" || echo "num1 > num2"
echo "by []\n"
[ $num1 -eq $num2 ] && echo "num1 == num2" || echo "num1 != num2"
[ $num1 -ne $num2 ] && echo "num1 != num2" || echo "num1 == num2"
[ $num1 -gt $num2 ] && echo "num1 > num2" || echo "num1 <= num2"
[ $num1 -lt $num2 ] && echo "num1 < num2" || echo "num1 >= num2"
[ $num1 -ge $num2 ] && echo "num1 >= num2" || echo "num1 < num2"
[ $num1 -le $num2 ] && echo "num1 <= num2" || echo "num1 > num2"
结果如图:
四.shell条件分支结构语句
1.单分支判断语句
格式:if 条件 ; then 结果 fi ,最后面一定要有fi,在shell脚本里面,控制分支结构结束都要和开头的单词相反,例如,if <–> fi,case <–> esac。
#!/bin/bash
echo "Please input a filename"
read filename
if [ -f $filename ];then
echo "this file is a ordinary file."
fi
结果如图:
2.双分支判断语句
echo "Please input a filename"
read filename
if [ -f $filename ];then
echo "this file is a ordinary file."
else
echo "this file is not a ordinary file."
fi
结果如图:
3.多分支判断语句
多分支判断有两种,和C语言的一样 if else if,case。只是形式上有一些不同。
#!/bin/bash
echo "Please input your math grades"
read grades
if [ $grades -gt 100 ] || [ $grades -lt 0 ];then
echo "Please input the number range in 0 - 100"
fi
if [ $grades -ge 90 ] && [ $grades -le 100 ];then
echo "Your grade is excellent."
elif [ $grades -ge 80 ] && [ $grades -le 89 ];then
echo "Your grade is good."
elif [ $grades -ge 70 ] && [ $grades -le 79 ];then
echo "Your grade is middle."
elif [ $grades -ge 60 ] && [ $grades -le 69 ];then
echo "Your grade is passing."
else
echo "Your grade is badly."
fi
结果如图:
#!/bin/bash
echo "Please input a command"
read cmd
case $cmd in
cpu) echo "The cpu information is"
cat /proc/cpuinfo;;
mem) echo "The mem information is"
cat /proc/meminfo;;
device) echo "The device information is"
cat /proc/scsi/device_info;;
CD-ROM) echo "The CD-ROM information is"
cat /proc/sys/dev/cdrom/info;;
*) echo "Your input command is invalid"
esac
结果如图:
五.shell循环语句
1.while语句
while语句是只要条件为真就执行下面语句。
格式:
while 条件
do
语句
done
需要注意的是,这里的条件除了 while true 可以这样写,其它的条件都要用 test或者 []来判断
#!/bin/bash
i=$1
while [ $i -gt 0 ]
do
echo $i
((i--))
done
2.until语句
until语句是只要条件为假就执行下列语句
格式:
until 条件
do
语句
done
#!/bin/bash
i=$1
until [ $i -le 0 ]
do
echo $i
((i--))
done
结果如图:
3.for语句
格式:
for 变量 in 列表
do
语句
done
#!/bin/bash
for i in `seq 2 8` #seq是一个命令,顺序生成一串数字或者字符
do
echo $i
done
结果如图:
六.shell函数
格式:
[function] funcName()
{
语句
[return 返回值]
}
返回值是可选的,如果没有显示return 则默认返回最后一条语句执行的结果。
Shell 函数返回值只能是整数,一般用来表示函数执行成功与否,0表示成功,其他值表示失败。如果 return 其他数据,比如一个字符串,往往会得到错误提示:“numeric argument required”。
如果一定要让函数返回字符串,那么可以先定义一个变量,用来接收函数的计算结果,脚本在需要的时候访问这个变量来获得函数返回值。
函数参数从$1
到$n
,$0
是文件名。
#!/bin/bash
#打印数字
printNum()
{
echo $1
}
for i in `seq 2 8` #seq是一个命令,顺序生成一串数字或者字符
do
printNum $i
done
结果如图:
返回字符串,报错
#!/bin/bash
#打印字符串
printNum()
{
return "Hello"
}
for i in `seq 2 8` #seq是一个命令,顺序生成一串数字或者字符
do
printNum
done
结果如图:
Recommend
-
36
shell语法练习 变量的定义和使用 编写shell脚本,第一行是 #!/bin/bash 代表是个shell脚本 输入参数: 形式 说明 $0 当前程序的名称 $n 程...
-
26
变量的定义 /*使用var定义*/ //隐式初始值(初始化为零值) var num1 int var str2 string //显式初始值 var num2 int = 12 var str2 string = "xiaohei" //类型自动推断 var a, b, c = 3, 5, "xiaohei" /*:=方式声明变量*/ f, g...
-
34
前言: 647 目录如下: Go语言基础(一)—— 简介...
-
32
1 hello world 1.1 代码 package main import "fmt" func main() { fmt.Println("hello world") } 1.2 执行 // 方法1 编译并执行 go run ./test002.go // 方法2 先构建后...
-
4
Go 作为一种编译型语言,经常用于实现后台服务的开发。由于 Go 初始的开发大佬都是 C 的老牌使用者,因此 Go 中保留了不少 C 的编程习惯和思想,这对 C/C++ 和 PHP 开发者来说非常有吸引力。作为编译型语言的特性,也让 Go 在多协程环境下的性能有不俗的表现。
-
2
shell编程语法 原创 小怪獣55 2022-04-25 10:02:52 博主文章分类:
-
9
Rathen0.432018.03.01 10:10:51字数 3,294阅读 13,389 Shell就是一个
-
8
Linux shell 语法 if [ $? == 0 ] 详细-嵌入式系统-与非网
-
3
当学习 Python 编程语言时,掌握基本语法和数据类型是必不可少的。在这里,我们将讲解 Python 的变量和常量、...
-
11
“蓝空天末,孤星遥坠。满街游走,打听幸福。” Bash脚本DLC:Bash语法和URL检测脚本实例 其实之前写过一篇bash脚本的文章:
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK