shell script学习

本文详细介绍了Shell脚本的基础知识,包括如何使用test命令进行文件和字符串的测试,条件判断的实现,for、while和until循环的使用,以及如何创建和使用函数。通过实例展示了变量的使用和条件语句的结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

利用test命令的测试功能

利用判断符号[]

shell的默认变量

条件判断式

利用if...then

利用case...esac判断

 

for命令

读取列表中的值

读取列表中的复杂值

从变量读取列表

从命令读取值

while命令

使用多个测试命令

until命令

创建函数


 

利用test命令的测试功能

mali@mali:/home$ ll
total 20
drwxr-xr-x  5 root  root  4096 7月  29 23:29 ./
drwxr-xr-x 24 root  root  4096 7月  14 20:42 ../
drwxr-xr-x 18 mali  mali  4096 7月  29 23:28 mali/
drwxr-xr-x  2 mali1 mali1 4096 7月  29 23:08 mali1/
drwxr-xr-x  2 mary  mary  4096 7月  29 23:04 mary/

#文件类型判断
#测试该文件名是否存在
mali@mali:/home$ test -e /home/mali
mali@mali:/home$ echo $?
0
mali@mali:/home$ test -e /home/mali && echo "exist" || echo "not exist"
exist
mali@mali:/home$ test -e /home/tom && echo "exist" || echo "not exist"
not exist

#测试文件名是否存在且为文件(file)
mali@mali:/home$ test -f /home/mali
mali@mali:/home$ echo $?
1

#测试文件名是否存在且为目录(directory)
mali@mali:/home$ test -d /home/mali
mali@mali:/home$ echo $?
0

#文件权限检测
#测试该文件名是否存在且具有"可读"的权限
mali@mali:/home$ test -r /home/mali
mali@mali:/home$ echo $?
0

#两个整数之间的判定
#判断是否相等(equal)
mali@mali:/home$ test 2 -eq 3
mali@mali:/home$ echo $?
1
#判断是否小于(less than)
mali@mali:/home$ test 2 -lt 3
mali@mali:/home$ echo $?
0
#判断是否不等(not equal)
mali@mali:/home$ test 2 -ne 3
mali@mali:/home$ echo $?
0

#判定字符串
#判断两个字符串是否相等
mali@mali:/home$ test "hello" = "world"
mali@mali:/home$ echo $?
1
#判断两个字符串是否不等
mali@mali:/home$ test "hello" != "world"
mali@mali:/home$ echo $?
0

#判断字符串是否为空
mali@mali:/home$ test -z "hello"
mali@mali:/home$ echo $?
1
#判断字符串是否不为空
mali@mali:/home$ test -n "hello"
mali@mali:/home$ echo $?
0
mali@mali:/home$ test -z
mali@mali:/home$ echo $?
0
mali@mali:/home$ test -z ""
mali@mali:/home$ echo $?
0

#多重条件判定
#-a 两个条件同时成立
mali@mali:/home$ test 4 -gt 3 -a 4 -gt 5
mali@mali:/home$ echo $?
1
mali@mali:/home$ test 4 -gt 3 -a 4 -gt 2
mali@mali:/home$ echo $?
0
#-o 任何一个条件成立
mali@mali:/home$ test 4 -gt 3 -o 4 -gt 5
mali@mali:/home$ echo $?
0
#! 条件不成立时返回true
mali@mali:/home$ test ! 4 -gt 5
mali@mali:/home$ echo $?
0
mali@mali:/home$ 

利用判断符号[]

中括号的两端需要有空格符来分隔

mali@mali:/home$ [ -z "$HOME" ]; echo $?
1
  • 在中括号内的每个组件都需要有空格键来分隔
  • 在中括号的变量,最好都以双引号括起来
  • 在中括号内的常量,最好都以单或双引号括起来
mali@mali:~$ name="Bruce Li"
mali@mali:~$ [ $name == "Bruce Li" ]; echo $?
bash: [: too many arguments
2
mali@mali:~$ [ "$name" == "Bruce Li" ]; echo $?
0

shell的默认变量

mali@mali:~$ vim sh01.sh 
mali@mali:~$ cat sh01.sh
#!/bin/bash

#show the script name, parameters

echo "The script name is  ==> $0" #文件名
echo "Total parameter number is ==> $#" #参数个数
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." && exit 0

echo "Your whole parameter is ==> $@" #参数全部内容
echo "The lst parameter ==> $1" #第一个参数
echo "The 2st parameter ==> $2" #第二个参数

mali@mali:~$ chmod +x sh01.sh

mali@mali:~$ ./sh01.sh
The script name is  ==> ./sh01.sh
Total parameter number is ==> 0
The number of parameter is less than 2. Stop here.

mali@mali:~$ ./sh01.sh tome jerry lucy
The script name is  ==> ./sh01.sh
Total parameter number is ==> 3
Your whole parameter is ==> tome jerry lucy
The lst parameter ==> tome
The 2st parameter ==> jerry
mali@mali:~$ 

条件判断式

利用if...then

if [ 条件判断式 ]; then
    当条件判断式成立时,可以进行的命令工作内容
fi

或
if command
then
    commands
fi

通过把分号放在待求值的命令尾部,就可以将then语句放在同一行

 &&代表AND

||代表or

mali@mali:~$ vim sh02.sh
mali@mali:~$ cat sh02.sh
#!/bin/bash

#this program shows the user's choice

read -p "please input (Y/N) :" choice
if [ "$choice" == "Y" ] || [ "$choice" == "y" ]; then
    echo "OK, continue"
    exit 0
fi

if [ "$choice" == "N" ] || [ "$choice" == "n" ]; then
    echo "OK, interrupt!"
    exit 0
fi

echo "I don't know what your choice is" && exit 0
mali@mali:~$ chmod +x sh02.sh
mali@mali:~$ ./sh02.sh 
please input (Y/N) :y
OK, continue
mali@mali:~$ ./sh02.sh 
please input (Y/N) :n
OK, interrupt!
mali@mali:~$ ./sh02.sh 
please input (Y/N) :i
I don't know what your choice is
mali@mali:~$ 
if [ 条件判断式 ]; then
    当条件判断式成立时,可以进行的命令工作内容
else
    当条件判断式不成立时,可以进行的命令工作内容
fi
if [ 条件判断式一 ]; then
    当条件判断式一成立时,可以进行的命令工作内容
elif [ 条件判断式二 ]; then
    当条件判断式二成立时,可以进行的命令工作内容
else
    当条件判断式一和二均不成立时,可以进行的命令工作内容
fi

或
if command1
then
    commands
elif command2
then
    more commands
fi

改写sh02.sh

mali@mali:~$ cat sh02.sh
#!/bin/bash

#this program shows the user's choice

read -p "please input (Y/N) :" choice
if [ "$choice" == "Y" ] || [ "$choice" == "y" ]; then
    echo "OK, continue"
elif [ "$choice" == "N" ] || [ "$choice" == "n" ]; then
    echo "OK, interrupt!"
else
    echo "I don't know what your choice is"
fi
mali@mali:~$ ./sh02.sh
please input (Y/N) :y
OK, continue
mali@mali:~$ ./sh02.sh
please input (Y/N) :N
OK, interrupt!
mali@mali:~$ ./sh02.sh
please input (Y/N) :L
I don't know what your choice is
mali@mali:~$ 

利用case...esac判断

case $变量名称 in
    "第一个变量内容")
        程序段
        ;;
    "第二个变量内容")
        程序段
        ;;
  *)
    不包含第一个和第二个变量内容的其他程序执行段
    ;;
esac
或
case variable in
    pattern1 | pattern2)
        commands1;;
    pattern3)
        commands2;;
    *)
        default commands;;
esac
mali@mali:~$ cat sh03.sh
#!/bin/bash
#This script only accepts the following parameter: one,two or three.

echo "This program will print your selection!"
read -p "Input your choice: " choice
case $choice in
    "one")
        echo "Your choice is ONE"
        ;;
    "two")
        echo "Your choice is TWO"
        ;;
    "three")
        echo "Your choice is THREE"
        ;;
    *)
        echo "Usage $0 {one|two|three}"
        ;;
esac        

mali@mali:~$ ./sh03.sh 
This program will print your selection!
Input your choice: three
Your choice is THREE
mali@mali:~$ ./sh03.sh 
This program will print your selection!
Input your choice: four
Usage ./sh03.sh {one|two|three}

 

for命令

for var in list
do
    commands
done

读取列表中的值

for命令最基本的用法就是遍历for命令自身所定义的一系列值

mali@mali:~$ cat test1.sh
#!/bin/bash
#basic for command

for test in Alabama Alaska Arizona Arkansas California Colorado
do
    echo "the next state is $test"
done
mali@mali:~$ ./test1.sh
the next state is Alabama
the next state is Alaska
the next state is Arizona
the next state is Arkansas
the next state is California
the next state is Colorado

每次for命令遍历值列表,它都会将列表中的下个值赋给$test变量。$test变量可以像for命令语句中的其他脚本变量一样使用。 在最后一次迭代中,$test变量的值会在shell脚本的剩余部分一直保持有效。它会一直保持最后一次迭代的值(除非修改)

mali@mali:~$ cat test1.sh
#!/bin/bash
#basic for command

for test in Alabama Alaska Arizona Arkansas California Colorado
do
    echo "the next state is $test"
done
echo "the last state we visited was $test"
test=Connecticut
echo "Wait,now we're visiting $test"
mali@mali:~$ ./test1.sh
the next state is Alabama
the next state is Alaska
the next state is Arizona
the next state is Arkansas
the next state is California
the next state is Colorado
the last state we visited was Colorado
Wait,now we're visiting Connecticut

读取列表中的复杂值

从变量读取列表

mali@mali:~$ cat test1.sh
#!/bin/bash
#using a variable to hold the list
list="Alabama Alaska Arizona Arkansas California Colorado"
list=$list" Connecticut"

for state in $list
do
    echo "Have you ever visited $state?"
done

mali@mali:~$ ./test1.sh
Have you ever visited Alabama?
Have you ever visited Alaska?
Have you ever visited Arizona?
Have you ever visited Arkansas?
Have you ever visited California?
Have you ever visited Colorado?
Have you ever visited Connecticut?
mali@mali:~$ 

$list变量包含了用于迭代的标准文本值列表。

从命令读取值

 

while命令

while test command
do
    other commands
done
mali@mali:~$ cat test2.sh
#!/bin/bash
#while command test

var1=10
while [ $var1 -gt 0 ]
do
    echo $var1
    var1=$[ $var1 - 1 ]
done
mali@mali:~$ ./test2.sh 10
10
9
8
7
6
5
4
3
2
1
mali@mali:~$ 

使用多个测试命令

mali@mali:~$ cat test2.sh
#!/bin/bash
#while command test

var1=10

while echo $var1
        [ $var1 -ge 0 ]
do
    echo "This is inside the loop"
    var1=$[ $var1 - 1 ]
done
mali@mali:~$ ./test2.sh 10
10
This is inside the loop
9
This is inside the loop
8
This is inside the loop
7
This is inside the loop
6
This is inside the loop
5
This is inside the loop
4
This is inside the loop
3
This is inside the loop
2
This is inside the loop
1
This is inside the loop
0
This is inside the loop
-1
mali@mali:~$ 

until命令

until test commands
do
    other commands
done
mali@mali:~$ cat test2.sh
#!/bin/bash
#using the until command
var1=100

until [ $var1 -eq 0 ]
do
    echo $var1
    var1=$[ $var1 - 25 ]
done
mali@mali:~$ ./test2.sh
100
75
50
25
mali@mali:~$ 
mali@mali:~$ cat test2.sh
#!/bin/bash
#using the until command
var1=100

until echo $var1
        [ $var1 -eq 0 ]
do
    echo "inside the loop: $var1"
    var1=$[ $var1 - 25 ]
done
mali@mali:~$ ./test2.sh
100
inside the loop: 100
75
inside the loop: 75
50
inside the loop: 50
25
inside the loop: 25
0
mali@mali:~$ 

shell会执行指定的多个测试命令,只有在最后一个命令成立时停止

创建函数

function name {
    commands
}
或
name() {
    commands
}
mali@mali:~$ cat test2.sh
#!/bin/bash
#using a function in a script

function func1 {
    echo "this is an example of a function"
}

count=1
while [ $count -le 5 ]
do
    func1
    count=$[ $count + 1 ]
done

echo "this is the end of the loop"
func1
echo "now this is the end of the scipt"
mali@mali:~$ ./test2.sh
this is an example of a function
this is an example of a function
this is an example of a function
this is an example of a function
this is an example of a function
this is the end of the loop
this is an example of a function
now this is the end of the scipt
mali@mali:~$ 

在函数中使用变量

向函数传递参数

mali@mali:~$ cat test2.sh
#!/bin/bash
#passing parameters to a function

function addem {
    if [ $# -eq 0 ] || [ $# -gt 2 ]
    then
        echo -1
    elif [ $# -eq 1 ]
    then
        echo $[ $1 + $1 ]
    else
        echo $[ $1 + $2 ]
    fi
}

echo -n "Adding 10 and 15: "
value=$(addem 10 15)
echo $value

echo -n "let's try adding just one number: "
value=$(addem 10)
echo $value

echo -n "Now trying adding no numbers: "
value=$(addem)
echo $value

echo -n "finally,try adding three numbers:"
value=$(addem 10 15 20)
echo $value

mali@mali:~$ ./test2.sh
Adding 10 and 15: 25
let's try adding just one number: 20
Now trying adding no numbers: -1
finally,try adding three numbers:-1
mali@mali:~$ 

向函数传数组参数

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值