给shell脚本传参数Passing arguments to a shell script

本文介绍了如何在Shell脚本中使用特殊变量来处理传递给脚本的参数。这些变量包括$0(脚本名称)、$1-$n(位置参数)、$*和$@(所有参数列表),以及 $#(参数数量)。通过示例展示了这些变量的不同用法。
[size=small]
[b]Simple explanation[/b]
$0 is the name of the command
$1 first parameter
$2 second parameter
$3 third parameter etc. etc
$# total number of parameters
$@ all the parameters will be listed
[b]
Passing arguments to a shell script[/b]
[url]http://osr600doc.sco.com/en/SHL_automate/_Passing_to_shell_script.html[/url]

Any shell script you run has access to (inherits) the environment variables accessible to its parent shell. In addition, any arguments you type after the script name on the shell command line are passed to the script as a series of variables.

The following parameters are recognized:


$*
Returns a single string (``$1, $2 ... $n'') comprising all of the positional parameters separated by the internal field separator character (defined by the IFS environment variable).

$@
Returns a sequence of strings (``$1'', ``$2'', ... ``$n'') wherein each positional parameter remains separate from the others.

$1, $2 ... $n
Refers to a numbered argument to the script, where n is the position of the argument on the command line. In the Korn shell you can refer directly to arguments where n is greater than 9 using braces. For example, to refer to the 57th positional parameter, use the notation ${57}. In the other shells, to refer to parameters with numbers greater than 9, use the shift command; this shifts the parameter list to the left. $1 is lost, while $2 becomes $1, $3 becomes $2, and so on. The inaccessible tenth parameter becomes $9 and can then be referred to.

$0
Refers to the name of the script itself.

$#
Refers to the number of arguments specified on a command line.

For example, create the following shell script called mytest:

echo There are $# arguments to $0: $*
echo first argument: $1
echo second argument: $2
echo third argument: $3
echo here they are again: $@
When the file is executed, you will see something like the following:
$ mytest foo bar quux
There are 3 arguments to mytest: foo bar quux
first argument: foo
second argument: bar
third argument: quux
here they are again: foo bar quux
$# is expanded to the number of arguments to the script, while $* and $@ contain the entire argument list. Individual parameters are accessed via $0, which contains the name of the script, and variables $1 to $3 which contain the arguments to the script (from left to right along the command line).
Although the output from $@ and $* appears to be the same, it may be handled differently, as $@ lists the positional parameters separately rather than concatenating them into a single string. Add the following to the end of mytest:

function how_many {
print "$# arguments were supplied."
}
how_many "$*"
how_many "$@"
The following appears when you run mytest:
$ mytest foo bar quux
There are 3 arguments to mytest: foo bar quux
first argument: foo
second argument: bar
third argument: quux
here they are again: foo bar quux
1 arguments were supplied.
3 arguments were supplied.[/size]
PS F:\spine-runtimes-4.1.00\spine-ts> npm install --legacy-peer-deps npm warn deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated npm warn deprecated uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details. npm warn deprecated source-map-url@0.4.1: See https://github.com/lydell/source-map-url#deprecated npm warn deprecated source-map-resolve@0.5.3: See https://github.com/lydell/source-map-resolve#deprecated npm warn deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated npm warn deprecated opn@6.0.0: The package has been renamed to `open` npm warn deprecated chokidar@2.1.8: Chokidar 2 does not receive security updates since 2019. Upgrade to chokidar 3 with 15x fewer dependencies > @esotericsoftware/spine-ts@4.1.19 prepublish > npm run clean && npm run build > @esotericsoftware/spine-ts@4.1.19 clean > npx rimraf spine-core/dist spine-canvas/dist spine-webgl/dist spine-player/dist spine-threejs/dist (node:10272) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated. (Use `node --trace-deprecation ...` to show where the warning was created) > @esotericsoftware/spine-ts@4.1.19 build > npm run clean && npm run build:modules && concurrently "npm run build:core" "npm run build:canvas" "npm run build:webgl" "npm run build:player" "npm run build:threejs" > @esotericsoftware/spine-ts@4.1.19 clean > npx rimraf spine-core/dist spine-canvas/dist spine-webgl/dist spine-player/dist spine-threejs/dist (node:18004) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated. (Use `node --trace-deprecation ...` to show where the warning was created) > @esotericsoftware/spine-ts@4.1.19 build:modules > npx tsc -b -clean && npx tsc -b (node:20644) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated. (Use `node --trace-deprecation ...` to show where the warning was created) (node:26472) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated. (Use `node --trace-deprecation ...` to show where the warning was created) (node:21092) [DEP0060] DeprecationWarning: The `util._extend` API is deprecated. Please use Object.assign() instead. (Use `node --trace-deprecation ...` to show where the warning was created) [0] [0] > @esotericsoftware/spine-ts@4.1.19 build:core [0] > npx esbuild --bundle spine-core/src/index.ts --tsconfig=spine-core/tsconfig.json --sourcemap --outfile=spine-core/dist/iife/spine-core.js --format=iife --global-name=spine [0] [1] [1] > @esotericsoftware/spine-ts@4.1.19 build:canvas [1] > npx esbuild --bundle spine-canvas/src/index.ts --tsconfig=spine-canvas/tsconfig.json --sourcemap --outfile=spine-canvas/dist/iife/spine-canvas.js --format=iife --global-name=spine [1] [2] [2] > @esotericsoftware/spine-ts@4.1.19 build:webgl [2] > npx esbuild --bundle spine-webgl/src/index.ts --tsconfig=spine-webgl/tsconfig.json --sourcemap --outfile=spine-webgl/dist/iife/spine-webgl.js --format=iife --global-name=spine [2] [3] [3] > @esotericsoftware/spine-ts@4.1.19 build:player [3] > npx copyfiles -f spine-player/css/spine-player.css spine-player/dist/ && npx npx esbuild --bundle spine-player/src/index.ts --tsconfig=spine-player/tsconfig.json --sourcemap --outfile=spine-player/dist/iife/spine-player.js --format=iife --global-name=spine [3] [4] [4] > @esotericsoftware/spine-ts@4.1.19 build:threejs [4] > npx esbuild --bundle spine-threejs/src/index.ts --tsconfig=spine-threejs/tsconfig.json --sourcemap --outfile=spine-threejs/dist/iife/spine-threejs.js --external:three --format=iife --global-name=spine [4] [1] (node:15712) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated. [1] (Use `node --trace-deprecation ...` to show where the warning was created) [0] (node:19492) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated. [0] (Use `node --trace-deprecation ...` to show where the warning was created) [2] (node:14536) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated. [2] (Use `node --trace-deprecation ...` to show where the warning was created) [3] (node:14188) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated. [3] (Use `node --trace-deprecation ...` to show where the warning was created) [4] (node:22140) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated. [4] (Use `node --trace-deprecation ...` to show where the warning was created) [1] spine-canvas\dist\iife\spine-canvas.js 342.5kb [1] spine-canvas\dist\iife\spine-canvas.js.map 693.2kb [1] [1] Done in 36ms [0] spine-core\dist\iife\spine-core.js 333.3kb [0] spine-core\dist\iife\spine-core.js.map 672.9kb [0] [0] Done in 37ms [2] [2] spine-webgl\dist\iife\spine-webgl.js 435.8kb spine-webgl\dist\iife\spine-webgl.js.map 870.9kb [2] [2] Done in 43ms [4] [4] spine-threejs\dist\iife\spine-threejs.js 351.4kb spine-threejs\dist\iife\spine-threejs.js.map 707.3kb [4] [4] Done in 45ms [0] npm run build:core exited with code 0 [2] npm run build:webgl exited with code 0 [1] npm run build:canvas exited with code 0 [4] npm run build:threejs exited with code 0 [3] (node:5080) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated. [3] (Use `node --trace-deprecation ...` to show where the warning was created) [3] (node:22544) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated. [3] (Use `node --trace-deprecation ...` to show where the warning was created) [3] [3] spine-player\dist\iife\spine-player.js 483.4kb [3] spine-player\dist\iife\spine-player.js.map 951.1kb [3] [3] Done in 21ms [3] npm run build:player exited with code 0 > @esotericsoftware/spine-ts@4.1.19 postbuild > npm run minify > @esotericsoftware/spine-ts@4.1.19 minify > npx esbuild --minify spine-core/dist/iife/spine-core.js --outfile=spine-core/dist/iife/spine-core.min.js && npx esbuild --minify spine-canvas/dist/iife/spine-canvas.js --outfile=spine-canvas/dist/iife/spine-canvas.min.js && npx esbuild --minify spine-player/dist/iife/spine-player.js --outfile=spine-player/dist/iife/spine-player.min.js && npx esbuild --minify spine-webgl/dist/iife/spine-webgl.js --outfile=spine-webgl/dist/iife/spine-webgl.min.js && npx esbuild --minify spine-threejs/dist/iife/spine-threejs.js --outfile=spine-threejs/dist/iife/spine-threejs.min.js (node:23992) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated. (Use `node --trace-deprecation ...` to show where the warning was created) spine-core\dist\iife\spine-core.min.js 141.2kb Done in 21ms (node:14972) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated. (Use `node --trace-deprecation ...` to show where the warning was created) spine-canvas\dist\iife\spine-canvas.min.js 144.8kb Done in 23ms (node:22448) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated. (Use `node --trace-deprecation ...` to show where the warning was created) spine-player\dist\iife\spine-player.min.js 228.1kb Done in 31ms (node:26100) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated. (Use `node --trace-deprecation ...` to show where the warning was created) spine-webgl\dist\iife\spine-webgl.min.js 197.9kb Done in 26ms (node:19380) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated. (Use `node --trace-deprecation ...` to show where the warning was created) spine-threejs\dist\iife\spine-threejs.min.js 150.2kb Done in 23ms added 261 packages, and audited 763 packages in 3m 8 packages are looking for funding run `npm fund` for details 71 vulnerabilities (5 low, 19 moderate, 37 high, 10 critical) To address issues that do not require attention, run: npm audit fix To address all issues possible (including breaking changes), run: npm audit fix --force Some issues need review, and may require choosing a different dependency. Run `npm audit` for details.
最新发布
11-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值