#!/bin/bash
a=1
b=2
if test $a -eq $b
then
echo "a=b"
elif test $a -ne $b
then
echo "a!=b"
fi
字符串比较
参数选项
功能说明
-n
非空字符串
-z
空字符串
=
相等
!=
不相等
示例:
#!/bin/bash
a=""
b="abc"
if test -n "$a"
then
if test $a = $b
then
echo "a=b"
elif test $a != $b
then
echo "a!=b"
fi
elif test -z "$a"
then
echo "a is null"
fi
文件比较
参数选项
功能说明
-d
目录
-f
普通文件
-e
存在
-r
读权限
-w
写权限
-x
执行权限
示例:
if test -d my_dir;then echo "It is a directory.";fi
逻辑判断
参数选项
功能说明
-a
两条件是否同时成立
-o
两条件是否至少一个成立
!
取反
示例:
[root@openEluer ~]# if test 1 -eq 1 -a 2 -eq 2;then echo "ok";fi
ok
[root@openEluer ~]# if test ! 1 -eq 1 -a 2 -eq 2;then echo "ok";fi
[root@openEluer ~]# if test 1 -eq 2 -o 2 -eq 2;then echo "ok";fi
ok
括号逻辑判断简介
参数选项
功能说明
&&
逻辑与
||
逻辑或
!
逻辑非
[]、[[]]、(())也可进行逻辑判断,通过单语句判断来控制执行:
a && b :a为真,才执行b
a || b :a为假,才执行b
多个&&或||组合使用时,判断如下:
&&前面命令执行成功,才会执行下一条命令
||前面命令执行成功,后面的命令就不再执行
示例:
[ 1 -eq 1 ] && echo a
条件判断用于for循环嵌套
#!/bin/bash
# 指定要列出的目录路径
directory="/path/to/directory"
# 使用嵌套的 for 循环列出指定目录及其子目录中的所有文件和目录
echo "Listing files and directories in $directory and its subdirectories:"
for dir in "$directory"/*; do
if [ -d "$dir" ]; then
echo "Directory: $dir"
for file in "$dir"/*; do
echo " File: $file"
done
elif [ -f "$dir" ]; then
echo "File: $dir"
fi
done
---
#!/bin/bash
# 打印九九乘法表
for (( i = 1; i <= 9; i++ )); do
for (( j = 1; j <= i; j++ )); do
# 计算乘积并输出格式化的结果
echo -n " $j * $i = $[$i*$j]"
done
# 在每一行结束后换行
echo ""
done
条件判断用于while循环嵌套
示例:99乘法表
#!/bin/bash
i=1
while [ $i -le 9 ]
do
j=1
while [ $j -le $i ]
do
echo -n "$i * $j = $[i+j] " # -n 表示在末尾不自动添加换行符
let j++
done
let i++
echo ""
done
#!/bin/bash
# 定义一个函数来检查一个数是否为素数
is_prime() {
local num=$1
if [ $num -le 1 ]; then
return 1
fi
for ((i=2; i*i<=num; i++)); do
if [ $((num % i)) -eq 0 ]; then
return 1
fi
done
return 0
}
# 调用函数并根据返回值输出结果
number=29
if is_prime $number; then
echo "$number is a prime number."
else
echo "$number is not a prime number."
fi
带有数组参数的 Shell 函数
#!/bin/bash
# 定义一个函数来打印数组中的所有元素
print_array() {
local array=("$@")
for element in "${array[@]}"; do
echo $element
done
}
# 定义一个数组
my_array=(apple banana cherry)
# 调用函数并传递数组
print_array "${my_array[@]}"
shell语言
shell 脚本常用的条件判断方式
Shell脚本中常见的条件判断方式有:
test
中括号[ ]
双中括号[[ ]]
双括号(( ))
test 常见使用场景
数值比较
示例:
#!/bin/bash a=1 b=2 if test $a -eq $b then echo "a=b" elif test $a -ne $b then echo "a!=b" fi字符串比较
示例:
#!/bin/bash a="" b="abc" if test -n "$a" then if test $a = $b then echo "a=b" elif test $a != $b then echo "a!=b" fi elif test -z "$a" then echo "a is null" fi文件比较
示例:
逻辑判断
示例:
括号逻辑判断简介
示例:
条件判断用于for循环嵌套
#!/bin/bash # 指定要列出的目录路径 directory="/path/to/directory" # 使用嵌套的 for 循环列出指定目录及其子目录中的所有文件和目录 echo "Listing files and directories in $directory and its subdirectories:" for dir in "$directory"/*; do if [ -d "$dir" ]; then echo "Directory: $dir" for file in "$dir"/*; do echo " File: $file" done elif [ -f "$dir" ]; then echo "File: $dir" fi done --- #!/bin/bash # 打印九九乘法表 for (( i = 1; i <= 9; i++ )); do for (( j = 1; j <= i; j++ )); do # 计算乘积并输出格式化的结果 echo -n " $j * $i = $[$i*$j]" done # 在每一行结束后换行 echo "" done条件判断用于while循环嵌套
示例:99乘法表
#!/bin/bash i=1 while [ $i -le 9 ] do j=1 while [ $j -le $i ] do echo -n "$i * $j = $[i+j] " # -n 表示在末尾不自动添加换行符 let j++ done let i++ echo "" done函数
函数结构简介
基本的 Shell 函数定义和调用
#!/bin/bash # 定义一个简单的函数 say_hello() { echo "Hello, $1!" } # 调用函数并传递参数 say_hello "World"带有返回值的 Shell 函数
#!/bin/bash # 定义一个计算两个数之和的函数 add() { local result=$(( $1 + $2 )) echo $result } # 调用函数并捕获返回值 sum=$(add 5 7) echo "The sum is: $sum"带有全局变量的 Shell 函数
#!/bin/bash # 定义一个函数来修改全局变量 increment() { count=$(( count + 1 )) } # 初始化全局变量 count=0 # 调用函数多次 increment increment increment # 输出全局变量的值 echo "The count is: $count"带有条件判断和循环的 Shell 函数
#!/bin/bash # 定义一个函数来检查一个数是否为素数 is_prime() { local num=$1 if [ $num -le 1 ]; then return 1 fi for ((i=2; i*i<=num; i++)); do if [ $((num % i)) -eq 0 ]; then return 1 fi done return 0 } # 调用函数并根据返回值输出结果 number=29 if is_prime $number; then echo "$number is a prime number." else echo "$number is not a prime number." fi带有数组参数的 Shell 函数
#!/bin/bash # 定义一个函数来打印数组中的所有元素 print_array() { local array=("$@") for element in "${array[@]}"; do echo $element done } # 定义一个数组 my_array=(apple banana cherry) # 调用函数并传递数组 print_array "${my_array[@]}"