#!/bin/bash var="music" sports='sports' echo"I like $var"# I like music echo"I like ${var}"# I like music echo I like $var# I like music echo'I like $var'# I like $var echo"I like \$var"# I like $var echo'I like \$var'# I like \$var echo `bash -version` # GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)... echo'bash -version'# bash -version
#!/bin/bash DATE=$(date -u) # UTC 时间#!/bin/bash DATE=$(date -u) # UTC 时间 WHO=$(whoami) # 用户名 UPTIME=$(uptime) # 系统运行时间 echo"Today is $DATE. You are $WHO. Uptime info: $UPTIME" > logfile WHO=$(whoami) # 用户名 UPTIME=$(uptime) # 系统运行时间 echo"Today is $DATE. You are $WHO. Uptime info: $UPTIME" > logfile
内建命令
Shell
内建命令是可以直接在Shell中运行的命令。可以这么查看内建命令:
1 2 3 4 5 6 7 8 9 10 11 12 13
$ compgen -b | sort - . : [ alias autoload bg bindkey break builtin bye cd
也可以用 type 查看命令的类型。
1 2
$ typecd cd is a shell builtin
可以用 which 命令查看可执行文件的文件路径:
1 2
# which sort /usr/bin/sort
可通过 man builtins 查看内建命令的详细描述。
测试
IF条件表达式
if 后面需要接者then:
1 2 3 4 5
if [ condition-for-test ] then command ... fi
或者,
1 2 3 4
if [ condition-for-test ]; then command ... fi
如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/bin/bash VAR=myvar if [ $VAR = myvar ]; then echo"1: \$VAR is $VAR"# 1: $VAR is myvar fi if [ "$VAR" = myvar ]; then echo"2: \$VAR is $VAR"# 2: $VAR is myvar fi if [ $VAR = "myvar" ]; then echo"3: \$VAR is $VAR"# 3: $VAR is myvar fi if [ "$VAR" = "myvar" ]; then echo"4: \$VAR is $VAR"# 4: $VAR is myvar fi
上面,我们在比较时,可以用双引号把变量引用起来。
但要注意单引号的使用。
1 2 3 4 5 6 7 8 9
#!/bin/bash VAR=myvar if [ '$VAR' = 'myvar' ]; then echo'5a: $VAR is $VAR' else echo"5b: Not equal." fibas # Output: # 5b: Not equal.
上面这个就把 ‘$VAR’ 当一个字符串了。
但如果变量是多个单词,我们就必须用到双引号了,如
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/bin/bash
# 这样写就有问题 VAR1="my var" if [ $VAR1 = "my var" ]; then echo"\$VAR1 is $VAR1" fi # Output # error [: too many arguments
# 用双引号 if [ "$VAR1" = "my var" ]; then echo"\$VAR1 is $VAR1" fi
# 正确, 符号前后有空格 if [ $VAR2 = 1 ]; then echo"\$VAR2 is 1." else echo"It's not 1." fi # Output # 2 is 1.
# 错误, 符号前后无空格 if [$VAR2=1]; then echo"$VAR2 is 1." else echo"It's not 1." fi # Output # line 3: =1: command not found # line 5: [=1]: command not found # It's not 1.
文件测试表达式
对文件进行相关测试,判断的表达式如下:
表达式
True
file1 -nt file2
file1 比 file2 新。
file1 -ot file2
file1 比 file2 老。
-d file
文件file存在,且是一个文件夹。
-e file
文件 file 存在。
-f file
文件file存在,且为普通文件。
-L file
文件file存在,且为符号连接。
-O file
文件 flle 存在, 且由有效用户ID拥有。
-r file
文件 flle 存在, 且是一个可读文件。
-s file
文件 flle 存在, 且长度大于0。
-w file
文件 flle 可写入。
-x file
文件 flle 可写执行。
可以使用man test查看那详细的说明。
当表达式为True时,测试命令返回退出状态
0,而表达式为False时返回退出状态1。
1 2 3 4 5 6 7 8 9 10 11 12 13
#!/bin/bash FILE="/etc/resolv.conf" if [ -e "$FILE" ]; then if [ -f "$FILE" ]; then echo"$FILE is a file." fi if [ -d "$FILE" ]; then echo"$FILE is a directory." fi if [ -r "$FILE" ]; then echo"$FILE is readable." fi fi
字符串比较表达式
表达式
True
string1 = string2 或 string1 == string2
两字符相等
string1 != string2
两个字符串不相等
string1 > string2
string1 大于 string2.
string1 < string2
string1 小于string2.
-n string
字符串长度大于0
-z string
字符串长度等于0
1 2 3 4 5 6 7 8 9
#!/bin/bash STRING="" if [ -z "$STRING" ]; then echo"There is no string." >&2 exit 1 fi
#!/bin/bash a=3 b=4 c=3 if (("$a" < "$b")); then echo"$a is less than $b." else echo"$a is not less than $b." fi if (("$a" != "$c")); then echo"$a is not equal to $c." else echo"$a is equal to $c." fi
# 计算 echo"$a + $b = $(($a + $b))"
# Output # 3 is less than 4. # 3 is equal to 3. # 3 + 4 = 7
if [ condition-is-true ] then command A else command B fi
# 或 if [ condition-is-true ]; then command A else command B fi
例如:
1 2 3 4 5 6 7 8
#!/bin/bash MY_SHELL="csh" if [ "$MY_SHELL" = "bash" ] then echo"You are using the bash shell." else echo"You are not using the bash shell." fi
if/elif/else 语句格式如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
if [ condition-is-true ] then command A elif [ condition-is-true ] then command B else command C fi # or if [ condition-is-true ]; then command A elif [ condition-is-true ]; then command B else command C fi
如:
1 2 3 4 5 6 7 8 9
#!/bin/bash MY_SHELL="csh" if [ "$MY_SHELL" = "bash" ]; then echo"You are using the bash shell." elif [ "$MY_SHELL" = "csh" ]; then echo"You are using csh." else echo"You are not using the bash shell." fi
VAR1="variable" VAR2="variable 2" if [[ (VAR1 == "variable") ]]; then echo"They are the same." else echo"Not the same." fi
# 使用 && [[ ($VAR1 == variable) && ( $VAR2 == "variable 2") ]] && echo"They are the same again."
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/bin/bash
digit=4 if [[ $digit =~ [0-9] ]]; then echo"$digit is a digit" else echo"$digit isn't a digit" fi letter="abc" if [[ $letter =~ [0-9] ]]; then echo"$letter is a digit" else echo"$letter isn't a digit" fi
# Output # 4 is a digit # abc isn't a digit
怎么使用 For 循环
for循环的使用如下:
1 2 3 4
for VARIABLE_NAME in ITEM_1 ITEM_N do command A done
例如:
1 2 3 4 5 6 7 8 9 10
#!/bin/bash for COLOR in red green blue do echo"COLOR: $COLOR" done
# Output # COLOR: red # COLOR: green # COLOR: blue
可以在其中使用变量,如下:
1 2 3 4 5 6
#!/bin/bash COLORS="red green blue" for COLOR in$COLORS do echo"COLOR: $COLOR" done
用 for 循环重命名文件
我们举个简单的例子,用for循环重命名当前目录下的jpg图片。
1 2 3 4 5 6 7 8
#!/bin/bash IMGS=$(ls *jpg) DATE=$(date +%F) for IMG in$IMGS do echo"Renaming ${IMG} to ${DATE}-${IMG}" mv${IMG}${DATE}-${IMG} done