如何将参数传递给shell脚本?

8 unix shell dos batch-file

我有一个这样的批处理文件:

java temptable %1 %2
Run Code Online (Sandbox Code Playgroud)

我需要上面的等效shell脚本.我将参数传递给shell脚本,并将其传递给temptable.

pax*_*blo 17

对于bash(这是一个 shell,但可能是Linux世界中最常见的),等效的是:

java temptable $1 $2
Run Code Online (Sandbox Code Playgroud)

假设参数中没有空格.如果有空格,你应该引用你的论点:

java temptable "$1" "$2"
Run Code Online (Sandbox Code Playgroud)

你也可以这样做:

java temptable $*
Run Code Online (Sandbox Code Playgroud)

要么:

java temptable "$@"
Run Code Online (Sandbox Code Playgroud)

如果你想要通过所有参数(再次,第二个相当于引用每个参数:"$ 1""$ 2""$ 3"......).


小智 8

#!/bin/bash
# Call this script with at least 3 parameters, for example
# sh scriptname 1 2 3
echo "first parameter is $1"
echo "Second parameter is $2"
echo "Third parameter is $3"
exit 0
Run Code Online (Sandbox Code Playgroud)

输出:

[root@localhost ~]# sh parameters.sh 47 9 34
first parameter is 47
Second parameter is 9
Third parameter is 34
Run Code Online (Sandbox Code Playgroud)