2.2.1. 哪个 shell 运行 script?
在子shell中运行脚本,你应该定义运行哪个shell。
您编写脚本时使用的 shell 类型可能不是系统的默认类型,因此您输入的命令在由错误的 shell 执行时可能会导致错误。
脚本的第一行决定了要启动的 shell。第一行的前两个字符应该是#! , 然后遵循应该解释后面命令的 shell 的路径。空行也被认为是行,所以不要以空行开始你的脚本。
就本课程而言,所有脚本都以该行开头
#!/bin/bash
2.2.2. 添加注释
您应该意识到您可能不是唯一阅读您的代码的人。许多用户和系统管理员运行由其他人编写的脚本。如果他们想看看你是怎么做到的,注释有助于启发读者。
注释也让你自己的生活更轻松。假设您必须阅读大量手册页才能使用您在脚本中使用的某些命令获得特定结果。如果您需要在几周或几个月后更改脚本,您将不会记得它是如何工作的,除非您已经评论了您所做的、您是如何做到的和/或您为什么这样做。
以script1.sh为例,将其复制到commented-script1.sh,我们对其进行编辑,以便注释反映脚本的作用。shell 在一行上的哈希标记之后遇到的所有内容都将被忽略,并且仅在打开 shell 脚本文件时可见:
#!/bin/bash
# This script clears the terminal, displays a greeting and gives information
# about currently connected users. The two example variables are set and displayed.
clear # clear terminal window
echo "The script starts now."
echo "Hi, $USER!" # dollar sign is used to get content of variable
echo
echo "I will now fetch you a list of connected users:"
echo
w # show who is logged on and
echo # what they are doing
echo "I'm setting two variables now."
COLOUR="black" # set a local shell variable
VALUE="9" # set a local shell variable
echo "This is a string: $COLOUR" # display content of variable
echo "And this is a number: $VALUE" # display content of variable
echo
echo "I'm giving you back your prompt now."
echo
https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_02.html