Shell语法全解

Shell基础语法全解

  • 一、shell简介
  • 二、shell格式
    • [2.1 新建一个shell脚本文件](#2.1 新建一个shell脚本文件)
    • [2.2 执行脚本方式](#2.2 执行脚本方式)
  • 三、变量
    • 3.1系统变量
    • 3.2自定义变量
    • [3.3 特殊变量](#3.3 特殊变量)
      • [3.3.1 `n\` 传入变量](#3.3.1 `n` 传入变量)
      • [3.3.2 `$#` 输入参数个数](#` 输入参数个数)
      • [3.3.3 `\*\`、\`@` 输入参数内容](#3.3.3 $*$@ 输入参数内容)
      • [3.3.4 `?\` 上一条命令执行结果](#3.3.4 `?` 上一条命令执行结果)
  • [四、运算符 `\[\]\`、\`(())`](#四、运算符 $[]$(()))
  • 五、条件判断
  • [六. 流程判断](#六. 流程判断)
    • [6.1 if 判断](#6.1 if 判断)
    • [6.2 case 判断](#6.2 case 判断)
    • [6.3 for 循环](#6.3 for 循环)
    • [6.4 until 循环](#6.4 until 循环)
    • [6.5 while 循环](#6.5 while 循环)

一、shell简介

shell是命令解释程序,它有很多种,例如:sh、bash、ash、dash等。

不同系统可能会使用不同的shell

powershell 复制代码
#查看当前系统使用的shell是哪种
echo $SHELL
#输出,当前系统默认使用bash
/bin/bash

二、shell格式

2.1 新建一个shell脚本文件

powershell 复制代码
#创建hello.sh文件
touch hello.sh
#修改权限
chmod +x hello.sh

#编辑脚本文件
vim hello.sh

向hello.sh添加以下代码

powershell 复制代码
#第一行开始的格式
#!/bin/bash

echo "hello"

2.2 执行脚本方式

powershell 复制代码
#1.查看当前系统有哪些shell
ll /bin/ | grep sh
#输出
-rwxr-xr-x  1 root root 1037528 7月  13  2019 bash*
-rwxr-xr-x  1 root root  253816 8月  14  2019 btrfs-show-super*
-rwxr-xr-x  1 root root  154072 2月  18  2016 dash*
lrwxrwxrwx  1 root root       4 5月  17 20:28 rbash -> bash*
lrwxrwxrwx  1 root root       4 5月  17 20:28 sh -> dash*
lrwxrwxrwx  1 root root       4 5月  17 20:28 sh.distrib -> dash*
lrwxrwxrwx  1 root root       7 3月   7  2019 static-sh -> busybox*
#以上可以看到有bash 和sh 两种


#2.使用不同shell执行
#使用默认shell执行
./hello.sh

#指定使用 bash 执行
bash hello.sh

#指定使用 sh 执行
sh hello.sh

#以上两种都输出
hello

三、变量

3.1系统变量

常用系统变量

powershell 复制代码
#当前主目录
echo $HOME
#当前用户
echo $USER
#默认工作目录
echo $PWD
#默认shell
echo $SHELL
#默认语言
echo $LANGUAGE

echo [$+变量名]
#等价于
printenv 变量名

3.2自定义变量

powershell 复制代码
#定义变量  等号前面不能有空格
#变量名=变量值   
MyName="lisi"

#撤销变量
unset MyName

撤销案例

powershell 复制代码
a=2
echo $a
#输出
2
#撤销变量
unset a
echo $a
#输出空行

3.3 特殊变量

3.3.1 $n 传入变量

$n 中的n是指代数字,表示第n个传入变量 第0个变量是脚本名称,其中$1-$9是第1个到第9个变量,十以上需要用大括号包括 ${10}

3.3.2 $# 输入参数个数

$# 是输入参数个数,可以在shell脚本中通过该变量获取。

3.3.3 $*$@ 输入参数内容

这俩个都是获取所有输入参数,但是$@可以被遍历

powershell 复制代码
#编辑hello.sh

#!/bin/bash
echo '=======$n========='
echo '$0 arg0 is:'  $0
echo '$1 arg1 is:'  $1
echo '$2 arg2 is:'  $2
echo '$3 arg3 is:'  $3
echo '$4 arg4 is:'  $4
echo '=======$#========='
echo 'args total is:' $#
echo '=======$*========='
echo 'args str is:' $*
echo '=======$@========='
echo 'args str enumable is' $@

#执行命令
./hello.sh arg1 arg2 arg3 arg4 arg5
#输出
=======$n=========
$0 arg0 is: ./hello.sh
$1 arg1 is: arg1
$2 arg2 is: arg2
$3 arg3 is: arg3
$4 arg4 is: arg4
=======$#=========
args total is: 5
=======$*=========
args str is: arg1 arg2 arg3 arg4 arg5
=======$@=========
args str enumable is arg1 arg2 arg3 arg4 arg5

3.3.4 $? 上一条命令执行结果

powershell 复制代码
echo $[1/0]
#输出
-bash: 1/0: 除0 (错误符号是 "0")

echo $?
#输出 1为上一条命令执行有错误,0表示没有错误
1

./hello.sh
#没有报错的话,输出0
0

四、运算符 $[]$(())

$[]$(()) 用于数值计算,避免歧义,所以括起来进行计算。

powershell 复制代码
#!/bin/bash
s1=$[(1+3)*5]
s2=$((5+3))
echo '(1+3)*5 =' $s1
echo '5+3 =' $s2
sum=$[$1+$2]
echo '$1+$2=' $sum

#执行
./com.sh 12 13
#输出
(1+3)*5 = 20
5+3 = 8
$1+$2= 25

五、条件判断

5.1判断数值大小

判断符号 含义
-eq 等于
-ne 不等于
-lt 小于
-le 小于等于
-gt 大于
-ge 大于等于

基础案例

powershell 复制代码
test 4 -gt 3
echo $?
#输出  0表示true,1表示false
0

test 4 -lt 3
echo $?
#输出  0表示true,1表示false
1

#等价于上面,但是注意括号前后需要有空格
[ 4 -gt 3 ]
echo $?
#输出  0表示true,1表示false
0

5.2 判断文件权限、文件类型

判断文件权限

判断权限符号 文件权限
-r 有读的权限
-w 有写的权限
-x 有执行的权限

判断文件类型

判断类型符号 文件类型
-e 文件存在
-f 文件存在且是一个常规文件
-d 文件存在且是一个目录

多条件判断

使用 && 进行短路与操作,如果第一个条件为false,直接返回false。

使用 || 进行短路或操作,如果第一个条件为true,直接返回true。

powershell 复制代码
#判断shell脚本是否为文件夹
[ -d com.sh ]
echo $?
#输出1   false
1

#可以利用多条件判断,构成一个类似 ?: 的三元运算
[ -f com.sh ] && echo true || echo false
#输出
true

六. 流程判断

6.1 if 判断

使用格式

powershell 复制代码
#格式一
if [ 条件判断式 ]
then
	语句
fi
或者
if [ 条件判断式 ];then
	语句
fi

#格式二
if [ 条件判断式 ]
then
	语句
elif [ 条件判断式 ]
then
	语句
else
	语句
fi

基础案例

powershell 复制代码
#编辑if.sh

#!/bin/bash
if [ 4 -gt 3 ];then
   echo true
else
   echo false
fi


./if.sh
#输出
true

if判断案例

powershell 复制代码
#!/bin/bash
if [ $# -gt 1 ]
then
  if [ $1 -gt $2 ]
  then
     echo $1 is bigger
  elif [ $1 -lt $2 ]
  then
     echo $2 is bigger
  else
     echo $2 equal $1
  fi
else
  echo args too less
fi

./if.sh 1 3
#输出
3 is bigger

./if.sh
#输出
args too less

6.2 case 判断

使用格式

powershell 复制代码
case $1 in
条件1)
	语句
	;;
条件2)
	语句
	;;
*)
	其他情况
	;;
esac

基础案例

powershell 复制代码
#编辑文件 case.sh

#!/bin/bash
if [ $# -le 0 ]
then
  echo args is less
  exit 1
fi

echo arg num is $#,arg1 is $1

case $1 in
1)
  echo arg is 1
  ;;
2)
  echo arg is 2
  ;;
*)
  echo arg is $1
  ;;
esac


./case.sh 8
#输出
arg num is 1,arg1 is 8
arg is 8

./case.sh 3
#输出
arg num is 1,arg1 is 3
arg is 3

6.3 for 循环

使用格式

powershell 复制代码
#格式一
for i in $@
do
  echo $i
done

#格式二
for ((i=1;i<=5;i++))
do
   echo $i
done

基本案例

powershell 复制代码
#编辑for.sh文件
#!/bin/bash
echo arg num is $#.
for i in $@
do
  echo $i
done


./for.sh 1 2 3 4 5 6
#输出
arg num is 6.
1
2
3
4
5
6

6.4 until 循环

使用格式

powershell 复制代码
until 条件
do
  语句
done

基本案例

powershell 复制代码
#编辑 until.sh

#!/bin/bash
i=1
sum=0
#until中条件为true就退出
#直到i大于10就退出,意思是这里i为11就退出
until [ $i -gt 10 ]
do
  sum=$[$i+$sum]
  let i++
done
echo $sum

./until.sh
#输出
55

6.5 while 循环

使用格式

powershell 复制代码
while 条件
do
   语句
done

基本案例

powershell 复制代码
#!/bin/bash
i=1
sum=0
#while中条件为false就退出
#当i小于等于10就执行循环语句,意思是这里i为11就退出
while [ $i -le 10 ]
do
  sum=$[$i+$sum]
  let i++
done
echo $sum
相关推荐
暗恋 懒羊羊2 分钟前
Linux 生产者消费者模型
linux·开发语言·ubuntu
五味香18 分钟前
C++学习,信号处理
android·c语言·开发语言·c++·学习·算法·信号处理
梓䈑35 分钟前
【C语言】自定义类型:结构体
c语言·开发语言·windows
PYSpring40 分钟前
数据结构-LRU缓存(C语言实现)
c语言·数据结构·缓存
安红豆.1 小时前
Linux基础入门 --13 DAY(SHELL脚本编程基础)
linux·运维·操作系统
..空空的人1 小时前
linux基础指令的认识
linux·运维·服务器
penny_tcf1 小时前
Linux基础命令halt详解
linux·运维·服务器
鱼跃鹰飞1 小时前
Leecode热题100-295.数据流中的中位数
java·服务器·开发语言·前端·算法·leetcode·面试
TANGLONG2221 小时前
【C语言】数据在内存中的存储(万字解析)
java·c语言·c++·python·考研·面试·蓝桥杯
N1cez1 小时前
vscode 连接服务器 不用输密码 免密登录
服务器·vscode