shell简明教程1简洁的shell脚本

发布时间 2023-08-03 10:24:13作者: 磁石空杯

1简洁的shell脚本

1.1 Hello

脚本是包含一系列命令的命令行程序。 脚本中包含的命令由解释器执行。 在shell脚本中,shell充当解释器,一条接一条地执行脚本中列出的命令。
任何可以在命令行中执行的命令都可以放入shell脚本中。shell脚本是自动执行任务的好帮手。
让我们来看简单的shell脚本script1.sh:

#!/bin/bash
echo "Scripting is fun!"

在尝试执行脚本之前,请确保它是可执行的

$ chmod +x script-01.sh
$ ./script-01.sh 
Scripting is fun!

你会注意到,脚本的第一行以'#!'开头,后面是bash shell程序'/bin/bash'的路径。'#!'可以读成 "sharp bang"。 当脚本的第一行以'#!'可开头时,后面的内容将被用作脚本中所列命令的解释器。

1.2 变量

您可以在 shell 脚本中使用变量。变量就是有名称的存储位置。 你可以把变量看作是名称-值对。要为变量赋值,请使用语法VARIABLE_NAME="Value"。 等号前后不要使用空格。此外,变量是区分大小写的,按照惯例,变量名是大写的。要使用变量,请在变量名前加上美元符号。

$ cat script-05.sh 
#!/bin/bash
MY_SHELL="bash"
echo "I like the $MY_SHELL shell."
$ ./script-05.sh 
I like the bash shell.

也可以用大括号括住变量名,并在大括号前加上美元符号。 语法 ${variable_name} .

$ cat script-06.sh 
#!/bin/bash
MY_SHELL="bash"
echo "I like the ${MY_SHELL} shell."
$ ./script-06.sh 
I like the bash shell.

变量前面或后面非空格时通常建议加上大括号,否则会把整个字符串当成变量。

$ ./script-07.sh 
I am bashing on my keyboard.
$ cat script-08.sh 
#!/bin/bash
MY_SHELL="bash"
echo "I am $MY_SHELLing on my keyboard."
$ ./script-08.sh 
I am  on my keyboard.

您还可以将命令的输出赋值给变量, 用"$(命令)"即可,"$(命令)"用来替换原来的"hostname"

$ cat script-09.sh 
#!/bin/bash
SERVER_NAME=$(hostname)
echo "You are running this script on ${SERVER_NAME}."
$ ./script-09.sh 
You are running this script on andrew-HP.
$ cat script-10.sh 
#!/bin/bash
SERVER_NAME=`hostname`
echo "You are running this script on ${SERVER_NAME}."
$ ./script-10.sh 
You are running this script on andrew-HP.

1.3 有效的变量名

变量名可以包含字母、数字和下划线。 变量名可以字母或下划线开头,但不能以数字开头。 以下是有效变量名的示例。

# 有效的变量名
FIRST3LETTERS="ABC"
FIRST_THREE_LETTERS="ABC"
firstThreeLetters="ABC"

# 无效变量名
3LETTERS="ABC"
first-three-letters="ABC"
first@Three@Letters="ABC"

1.4 测试

设计脚本的目的是为了取代人在键盘前输入一系列命令的工作。 如果您想自动执行一项任务,但需要根据不同的情况执行不同的操作,该怎么办?由于在需要运行脚本时,人可能不在身边,无法做出决定,因此我们需要对这些情况进行测试,并让脚本采取相应的行动。
要创建测试,请在括号中加入一个条件表达式。 语法如下 "[ 测试条件 ]" 。你可以测试几种情况。 例如,可以比较字符串是否相等、数字是否大于另一个数字或文件是否存在。 下面测试检查/etc/passwd是否存在。如果存在,则返回true。如果文件不存在,则返回false。 即命令以状态1退出。

[ -e /etc/passwd ]

如果使用的是bash shell,可以运行help test命令查看可以执行的各类测试。 也可以阅读test的man页面:man test 。 下面是一些比较常见的测试。

  • 文件操作符:

    • -d FILE 如果FILE是目录,则为真。
    • -e FILE 如果FILE存在,则为真。
    • -f FILE 如果FILE存在且是普通文件,则为真。
    • -r FILE 如果FILE可读取,则为True。
    • -s FILE-如果FILE存在且不是空文件,则为True。
    • -w FILE 如果FILE可写入,则为True。
    • -x FILE 如果FILE执行可,则为True。
  • 字符串操作符

    • -z STRING如果字符串为空,则为True。
    • -n STRING-如果字符串不为空,则为True。
    • STRING 字符串不为空时为True。
    • STRING1 = STRING2 如果字符串相等,则为True。
    • string1 != string2 如果字符串不相等,则为 rue。
  • 算术运算符

    • arg1 -eq arg2 如果arg1等于arg2,则为True。
    • arg1 -ne arg2 如果arg1不等于arg2,则为True。
    • arg1 -lt arg2 如果arg1小于arg2,则为True。
    • arg1 -le arg2 True如果arg1小于或等于arg2。
    • arg1 -gt arg2 True如果arg1大于arg2。
    • arg1 -ge arg2 如果arg1大于或等于arg2,则为True。

1.5 if语句

既然知道了如何判断某个条件是否为真,就可以结合if语句在脚本中做出判断。
if语句以"if"开头,然后是一个测试。下面一行包含then. 接下来是一系列命令,如果测试条件为真,这些命令将被执行。最后,if语句以 fi结尾。 语法如下

if [ condition-true ]
then
   command 1
   command 2
   ...
fi

下面是一个例子

$ cat script-11.sh 
#!/bin/bash

MY_SHELL="bash"

if [ "$MY_SHELL" = "bash" ]
then
  echo "You seem to like the bash shell."
fi
$ ./script-11.sh 
You seem to like the bash shell.

在进行条件测试时,最好用引号将变量括起来,以防止一些意想不到的结果。

使用if/else语句还可以在条件不为真时执行操作。 下面是if/else语句的例子。

if [ condition-true ]
then
   command 1
    command 2
   ...
else  #
   command 3
   command 4
   ...
fi

修改代码走else:

#!/bin/bash

MY_SHELL="csh"

if [ "$MY_SHELL" = "bash" ]
then
  echo "You seem to like the bash shell."
else
  echo "You don't seem to like the bash shell."
fi
$ ./script-12.sh
You don't seem to like the bash shell.

还可以使用elif来测试多个条件。elif是"else if"的缩写。 和if一样,在elif后面加上要测试的条件。 在下一行,使用 then. 最后,如果条件为真,则执行一系列命令。

if [ condition-true ]
then
   command 1
   command 2
   ...
elif [ condition-true ]
then
   command 3
   command 4
   ...
else  #
   command 5
    command 6
   ...
fi

走elif的示例:

#!/bin/bash

MY_SHELL="csh"

if [ "$MY_SHELL" = "bash" ]
then
  echo "You seem to like the bash shell."
elif [ "$MY_SHELL" = "csh" ]
then
  echo "You seem to like the csh shell."
else
  echo "You don't seem to like the bash or csh shells."
fi
$ ./script-13.sh
You seem to like the csh shell.

参考资料

1.6 FOR循环

如果要对列表执行操作,可以使用for循环。for循环的第一行以"for"开头,然后是变量名,接着是 "in",然后是项目列表。 下一行包含单词"do"。将要执行的语句放在下面几行,最后以单行 "done"结束 for 循环。

for VARIABLE_NAME in ITEM_1 ITEM_2 ITEM_N
do
  command 1
  command 2
  ...
done

列表中的第一项被赋值给变量,代码块被执行。然后将列表中的下一项分配给变量,并执行命令。

$ cat script-14.sh
#!/bin/bash

for COLOR in red green blue
do
  echo "COLOR: $COLOR"
done
$ ./script-14.sh
COLOR: red
COLOR: green
COLOR: blue

这个名为rename-pics.sh的shell脚本会重命名所有以jpg结尾的文件,在原文件名前加上今天的日期。

#!/bin/bash

PICTURES=$(ls *jpg)
DATE=$(date +%F)
for PICTURE in $PICTURES
do
  echo "Renaming ${PICTURE} to ${DATE}-${PICTURE}"
  mv ${PICTURE} ${DATE}-${PICTURE}
done

下面是运行此脚本后的结果。

$ ls
bear.jpg  man.jpg  pig.jpg  rename-pics.sh
$ ./rename-pics.sh
Renaming bear.jpg to 2023-08-03-bear.jpg
Renaming man.jpg to 2023-08-03-man.jpg
Renaming pig.jpg to 2023-08-03-pig.jpg
$ ls
2023-08-03-bear.jpg  2023-08-03-man.jpg  2023-08-03-pig.jpg  rename-pics.sh
$

1.7 位置参数

位置参数是包含命令行内容的变量。脚本本身存储在$0,第一个参数存储在$1,第二个参数存储在$2,以此类推。 以下面的命令行为例

$ script.sh parameter1 parameter2 parameter3

内容如下: $0为"script.sh",$1为 "parameter1",$2为 "parameter2",$3为 "parameter3"。
这个名为archive_user.sh的脚本接受一个用户名参数。


#!/bin/bash

echo "Executing script: $0"
echo "Archiving user: $1"

# Lock the account
passwd -l $1

# Create an archive of the home directory.
tar cf /archives/${1}.tar.gz /home/${1}

特殊变量$@,可以访问命令行上从$1开始到最后一个的所有位置参数。 以下是如何更新 archive_user.sh 脚本以接受一个或多个参数。

#!/bin/bash

echo "Executing script: $0"

for USER in $@
do
  echo "Archiving user: $USER"

  # Lock the account
  passwd -l $USER

  # Create an archive of the home directory.
  tar cf /archives/${USER}.tar.gz /home/${USER}
done

让我们向脚本传递多个用户。

$ ./archive_user.sh chet joe
Executing script: ./archive_user.sh
Archiving user: chet
passwd: password expiry information changed.
tar: Removing leading `/' from member names
Archiving user: joe
passwd: password expiry information changed.
tar: Removing leading `/' from member names

1.8 获取用户输入

如果要接受标准输入,请使用read命令。标准输入通常来自键盘输入,但也可能来自其他来源,如命令管道中命令的输出。read命令的格式是read -p "PROMPT" VARIABLE_NAME 。 此版本的 archive_user.sh脚本会询问用户账户。

#!/bin/bash

read -p "Enter a user name: " USER
echo "Archiving user: $USER"

# Lock the account
passwd -l $USER

# Create an archive of the home directory.
tar cf /archives/${USER}.tar.gz /home/${USER}

让我们运行此脚本,将mitch账户存档。

 ./archive_user.sh
Enter a user name: mitch
Archiving user: mitch
passwd: password expiry information changed.
tar: Removing leading `/' from member names
$