#!/bin/bash # 直接赋值 num1=10 num2=20 # 加法 sum=$((num1 + num2)) echo "The sum is: $sum" # 乘法 product=$((num1 * num2)) echo "The product is: $product" # 幂运算(使用expr命令) result=$(expr $num1 ** $num2) echo "The result of $num1 to the power of $num2 is: $result" # 赋值运算符 num1+=5 echo "After adding 5 to num1, it is: $num1"
关系运算符
1 2 3 4 5 6 7 8 9 10 11 12
#!/bin/bash num1=10 num2=20 if [ $num1 -eq $num2 ]; then echo "num1 is equal to num2" elif [ $num1 -gt $num2 ]; then echo "num1 is greater than num2" else echo "num1 is less than num2" fi
布尔运算符
1 2 3 4 5 6 7 8 9 10
#!/bin/bash num1=10 num2=20 if [ $num1 -eq 10 ] && [ $num2 -eq 20 ]; then echo "Both num1 and num2 are correct" else echo "At least one of the numbers is incorrect" fi
字符串运算符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/bin/bash str1="Hello" str2="World" if [ $str1 = $str2 ]; then echo "$str1 is equal to $str2" else echo "$str1 is not equal to $str2" fi # 检查字符串长度是否为零 str3="" if [ -z "$str3" ]; then echo "str3 is empty" else echo "str3 is not empty" fi
#!/bin/bash file="/path/to/some/file.txt" if [ -e "$file" ]; then echo "The file $file exists" elif [ -d "$file" ]; then echo "The $file is a directory" else echo "The file $file does not exist" fi # 检查文件是否可读、可写、可执行 if [ -r "$file" ]; then echo "The file $file is readable" fi if [ -w "$file" ]; then echo "The file $file is writable" fi if [ -x "$file" ]; then echo "The file $file is executable" fi
命令替换运算符
1 2 3 4 5 6 7 8 9 10
#!/bin/bash # 使用命令替换获取当前日期 today=$(date) echo "Today's date is: $today" # 使用命令替换获取文件内容(假设文件为file.txt) content=$(cat file.txt) echo "The content of file.txt is:" echo "$content"
if [ condition ]; then # code to execute if condition is true elif [ another_condition ]; then # code to execute if another_condition is true else # code to execute if none of the conditions are true fi
if [ condition ]; then # code to execute if condition is true elif [ another_condition ]; then # code to execute if another_condition is true else # code to execute if none of the conditions are true fi
循环结构
for循环:用于遍历列表(如数组或字符串)中的元素。
1 2 3 4
for variable in list do # code to execute for each element in list done
或者使用C风格的for循环:
1 2 3 4
for (( i=0; i<10; i++ )) do # code to execute for each iteration done
while循环:当条件为真时,重复执行代码块。
1 2 3 4
while [ condition ] do # code to execute while condition is true done
until循环:当条件为假时,重复执行代码块。
1 2 3 4
until [ condition ] do # code to execute until condition is true done
控制结构
break:用于立即终止循环(最内层循环)。
1 2 3 4 5 6 7
while [ true ] do # some code if [ some_condition ]; then break fi done
continue:用于跳过当前循环迭代,直接进入下一次迭代。
1 2 3 4 5 6 7
for file in * do if [ -d "$file" ]; then continue# skip directories fi # process files done
函数:允许你定义可重用的代码块,并在脚本中多次调用。
1 2 3 4 5 6
functionmy_function() { # code to execute when function is called }
# calling the function my_function
case语句:类似于其他编程语言中的switch语句,用于根据变量的值执行不同的代码块。
1 2 3 4 5 6 7 8 9 10 11
case$variablein pattern1) # code to execute if $variable matches pattern1 ;; pattern2) # code to execute if $variable matches pattern2 ;; *) # code to execute if $variable doesn't match any patterns ;; esac