Shell 脚本编程保姆级教程(下)

发布于:2024-06-30 ⋅ 阅读:(14) ⋅ 点赞:(0)

七、Shell 流程控制

7.1 if

#!/bin/bash
num1=100
if test $[num1] == 100
then
    echo 'num1 是 100'
fi

7.2 if else 

#!/bin/bash
num1=100
num2=100
if test $[num1] -eq $[num2]
then
    echo '两个数相等!'
else
    echo '两个数不相等!'
fi

7.3 if else-if else

#!/bin/bash
a=10
b=20
if [ $a == $b ]
then
   echo "a 等于 b"
elif [ $a -gt $b ]
then
   echo "a 大于 b"
elif [ $a -lt $b ]
then
   echo "a 小于 b"
else
   echo "没有符合的条件"
fi

7.4 for

#!/bin/bash
for loop in 1 2 3 4 5
do
    echo "The value is: $loop"
done

7.5 while

#!/bin/bash
int=1
while(( $int<=5 ))
do
    echo $int
    let "int++"
done

7.6 until

#!/bin/bash
a=0
until [ ! $a -lt 10 ]
do
   echo $a
   a=`expr $a + 1`
done

7.7 case ... esac 

echo '输入 1 到 4 之间的数字:'
echo '你输入的数字为:'
read aNum
case $aNum in
    1)  echo '你选择了 1'
    ;;
    2)  echo '你选择了 2'
    ;;
    3)  echo '你选择了 3'
    ;;
    4)  echo '你选择了 4'
    ;;
    *)  echo '你没有输入 1 到 4 之间的数字'
    ;;
esac

7.8 break

break 命令允许跳出所有循环(终止执行后面的所有循环)

7.9 continue

continue 命令与 break 命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环 

八、Shell 函数 

8.1 定义函数

#!/bin/bash
demoFun(){
    echo "这是我的第一个 shell 函数!"
}
echo "-----函数开始执行-----"
demoFun
echo "-----函数执行完毕-----"

8.2 函数参数

#!/bin/bash
funWithParam(){
    echo "第一个参数为 $1 !"
    echo "第二个参数为 $2 !"
    echo "第十个参数为 $10 !"
    echo "第十个参数为 ${10} !"
    echo "第十一个参数为 ${11} !"
    echo "参数总数有 $# 个!"
    echo "作为一个字符串输出所有参数 $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73

九、Shell 输入/输出重定向

命令 说明
command > file 将输出重定向到 file。
command < file 将输入重定向到 file。
command >> file 将输出以追加的方式重定向到 file。
n > file 将文件描述符为 n 的文件重定向到 file。
n >> file 将文件描述符为 n 的文件以追加的方式重定向到 file。
n >& m 将输出文件 m 和 n 合并。
n <& m 将输入文件 m 和 n 合并。
<< tag 将开始标记 tag 和结束标记 tag 之间的内容作为输入。

9.1 输出重定向 

注意:输出重定向会覆盖文件内容 

9.2 输入重定向 

9.3 重定向深入讲解

重定向符号

  • >:输出重定向,用于将命令的标准输出重定向到文件。会覆盖文件内容。
  • >>:追加重定向,用于将命令的标准输出追加到文件末尾。
  • <:输入重定向,用于将文件内容重定向为命令的标准输入。
  • 2>:错误重定向,用于将命令的标准错误重定向到文件。会覆盖文件内容。
  • 2>>:错误追加重定向,用于将命令的标准错误追加到文件末尾。
  • &>:将标准输出和标准错误一起重定向到文件。会覆盖文件内容。
  • &>>:将标准输出和标准错误一起追加到文件末尾。

文件描述符重定向

文件描述符是内核用来访问文件的引用。默认情况下:

  • 标准输入的文件描述符是 0
  • 标准输出的文件描述符是 1
  • 标准错误的文件描述符是 2

我们可以使用文件描述符进行更复杂的重定向操作。

[root@dan shell]# cat users who_is_the_best_in_the_world > output.txt 2> error.txt
[root@dan shell]# cat output.txt 
root     tty2         2024-06-27 17:24 (tty2)
root     pts/0        2024-06-27 17:28 (192.168.0.120)
root     pts/1        2024-06-28 12:22 (192.168.0.120)
Cao Licheng is the best in the world
[root@dan shell]# cat error.txt 
[root@dan shell]# cat users 666 > output.txt 2> error.txt
[root@dan shell]# cat error.txt 
cat: 666: No such file or directory
[root@dan shell]# 

第二次查询了一个不存在文件,并将错误输出到了 error.txt 文件中 

9.4 Here Document

Here文档是一种将多行字符串作为命令输入的方法,常用于脚本中 

cat << EOF
This is a here document.
It can contain multiple lines of text.
EOF

上述命令将多行文本作为 cat 命令的输入,EOF 是结束符,可以用其他字符串替换,但需要保持一致 

9.5 /dev/null 文件

如果希望执行某个命令,但又不希望在屏幕上显示输出结果,那么可以将输出重定向到 /dev/null:

$ command > /dev/null

十、Shell 文件包含

test2.sh 代码如下

#!/bin/bash
url="https://eclecticism.blog.csdn.net/"

test1.sh 代码如下

#!/bin/bash
source ./test2.sh # 或者 . ./test2.sh
echo "博客地址:${url}"