Shell script accepts parameters/sh script parameter passing

Article directory


This chapter mainly explains how to pass parameters outside the shell script, such as the kill process command: kill -9 进程号 , then how to write the command in the script, and then pass in the process number parameter in the form of parameter passing.

Create the kill.sh script and write the following command

kill -9 $1

Execute the script:

root> bash kill.sh 1327
或者
root> ./kill.sh 1327

shell pass parameters

method one

We can pass parameters to the script when executing the Shell script. The format for obtaining parameters in the script is: $n. n represents a number, 1 is the first parameter to execute the script, 2 is the second parameter to execute the script, and so on...

Parameter explanation
$0:执行的文件名(包含文件路径)/脚本本身的存储名称

$1: stores the first command line parameter

$2: stores the second command line argument

$3: stores the third command line parameter

$9: stores the 9th command line parameter

$10: stores the 10th command line parameter

$99: Stores the 99th command line argument

demoexampletest.sh
_

#!/bin/bash
 
echo "Shell 传递参数实例!";
 
echo "执行的文件名:$0";
 
echo "第一个参数为:$1";
 
echo "第二个参数为:$2";
 
echo "第三个参数为:$3";

Excuting an order

./test.sh  1  2  3

result:

Shell 传递参数实例!
执行的文件名:test.sh
第一个参数为:1
第二个参数为:2
第三个参数为:3

Guess you like

Origin blog.csdn.net/weixin_49114503/article/details/132358127