简单的命令可以在命令行中直接输入,但是复杂的命令需要写在脚本里。例如一个简单的shell脚本:
#!/bin/bash #输出一行 echo "Hello World!"
#开始的行是注释行,空行会被忽略。
方式一:直接输入脚本的相对路径或绝对路径
./run.sh
方式二:sh+脚本的相对路径或绝对路径
sh ./run.sh
WHAT IS THIS LINE CALLED?
This first line (#!/bin/bash or #!/bin/sh) has a name. It is known as ‘she-bang‘(shabang). This derives from the concatenation of the tokens sharp (#) and bang (!). It is also called as sh-bang, hashbang, poundbang or hash-pling. In computing, a she-bang is the character sequence consisting of the characters number sign and exclamation mark (#!) at the beginning of a script.
shabang,由sharp(#)和bang(!)组合而来,必须位于一个脚本的第一行
In Unix-like Operating Systems when a script starting with a she-bang(#!) is executed as a program, the program loader parses the rest of the script’s initial line as a interpreter-directive. Thus the specified interpreter program is run instead, passing to it as an argument the path that was used initially through the script.
In simple words, the she-bang at the head of the script tells the system that this file is a set of commands to be fed to the command interpreter indicated. Unix-like operating systems has variety of shells and each of script header lines call a different command interpreter.
如果shell看到第一行是shabang,shell就知道这个文件是一个shell脚本,并按照shabang的指引到/bin/bash找到指定的shell解释器,然后把文件中的命令传给shell。
解释器可以是bash,也可以是csh等等。
SOME she-bang EXAMPLES
*#!/bin/sh :Executes the script using the Bourne shell or a compatible shell, with path /bin/sh*
*#!/bin/bash :Executes the script using the Bash shell.*
*#!/bin/csh -f :Executes the script using C shell or a compatible shell.*
*#!/usr/bin/perl -T :Executes the script using perl with the option of taint checks*
*#!/usr/bin/env python :Executes the script using python by looking up the path to the python interpreter automatically from the environment variables*
#!/bin/bash #将一个字符串赋给变量A LOG="monday" echo "The value of logfile is:" #美元符号用于变量替换 echo $LOG
运行结果:
$ sh ./variable.sh The value of logfile is: monday
变量的赋值:
变量的使用:
变量作用范围
变量只在其所在脚本有效。
source可以强行让一个脚本影响其父环境
$ source variable.sh The value of logfile is: monday $ echo $LOG monday
与之相反,export可以让脚本影响其子shell环境
$ export count=5 ##输出变量count $ bash ##启动子shell $ echo $count 5 $ exit ##回到先前的shell中 exit
使用unset可以注销变量
unset log
$用于解析变量,如果要输出这个符号,需要使用转义字符'\'
$ LOG='Monday' $ echo
shell提供了花括号"{}"来限定一个变量的开始和结束。当需要紧跟变量输出字母后缀时,必须使用这个功能
$ WORD='big' $ echo "This apple is ${WORD}ger" This apple is bigger
可以向shell脚本传命令行参数,shell脚本中使用以数字命名的变量来存放这些参数,称为位置变量。
简单地说,第一个位置变量存放在$1,第二个存放在$2,以此类推。当变量数量超过10个时,需要加花括号把数字括起来。例如${10},${23}等。
$0用于存放脚本自己的名字。
!#/bin/bash echo "\$0 = *$0*" echo "\$1 = *$1*" echo "\$2 = *$2*" echo "\$3 = *$3*"
运行结果:
$ sh ./diaplay_para.sh first second $0 = *display_para.sh* $1 = *firsh* $2 = *second* $3 = ** ##不存在第三个变量,所以为空
除了以数字命名的变量外,shell还提供了另外三个位置变量:
#!/bin/bash #显示有多少个参数需要列出 echo "The number of parameter is $#" for para in $@ do echo $para ##也可以写成 echo "$para" done
运行结果:
$ sh ./list_para.sh first second The number of parameter is 2 first second
shell脚本中可以使用的引号有以下三种:
#!/bin/bash LOG=Saturday echo "Today is $LOG" echo 'Today is $LOG' echo "Today is `date`" echo 'Today is `date`'
运行结果:
Today is Saturday Today is $LOG Today is Thu Jun 8 17:37:43 CST 2023 Today is `date`