Linux Shell脚本中的命令行参数
概述:
命令行参数(也称为位置参数)是在命令提示符下使用要执行的命令或脚本指定的参数。 参数在命令提示符下的位置以及命令的位置或脚本本身都存储在相应的变量中。 这些变量是特殊的Shell变量。 下图将帮助您理解它们。
让我们创建一个名称为“ command_line_agruments.sh”的shell脚本,它将显示所提供的命令行参数,计数的数量,脚本的第一个参数的值和进程ID(PID)。
[email protected]:~$ cat command_line_agruments.sh
为脚本分配可执行权限
[email protected]:~$ chmod +x command_line_agruments.sh
现在使用命令行参数执行脚本
[email protected]:~$ ./command_line_agruments.sh Linux AIX HPUX VMware There are 4 arguments specified at the command line. The arguments supplied are: Linux AIX HPUX VMware The first argument is: Linux The PID of the script is: 16316
转移命令行参数
shift命令用于将命令行参数向左移动一个位置。 在此过程中,第一个参数丢失。 下面的“ command_line_agruments.sh”脚本使用shift命令:
[email protected]:~$ cat command_line_agruments.sh
现在,再次执行脚本。
[email protected]:~$ ./command_line_agruments.sh Linux AIX HPUX VMware There are 4 arguments specified at the command line The arguments supplied are: Linux AIX HPUX VMware The first argument is: Linux The Process ID of the script is: 16369 The new first argument after the first shift is: AIX The new first argument after the second shift is: HPUX [email protected]:~$
可以通过向换档命令提供所需的换档次数作为参数来执行一次尝试中的多个换档。