通过 shell 脚本为可执行文件提供命令行参数

An̲*_*rew 2 scripting unix bash command-line-interface ksh

假设我有一个可执行文件xyz,它接受可变数量的命令行参数,以及一个包装器 Korn shell 脚本xyz.ksh。是否有一种简单的方法可以将所有 shell 脚本参数按原样传递给可执行文件?

Mik*_*eyB 11

您需要使用:

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

用于在所有情况下正确的参数扩展。这种行为在 bash 和 ksh 中是相同的。

大多数时候,$* 或 $@ 会给你你想要的。但是,它们用空格扩展参数。"$*" 将所有参数减为 1。"$@" 为您提供实际传递给包装器脚本的内容。

亲自查看(再次,在 bash 或 ksh 下):

[tla ~]$ touch file1 file2 space\ file
[tla ~]$ ( test() { ls $*; }; test file1 file2 space\ file )
ls: cannot access space: No such file or directory
ls: cannot access file: No such file or directory
file1  file2
[tla ~]$ ( test() { ls $@; }; test file1 file2 space\ file )
ls: cannot access space: No such file or directory
ls: cannot access file: No such file or directory
file1  file2
[tla ~]$ ( test() { ls "$*"; }; test file1 file2 space\ file )
ls: cannot access file1 file2 space file: No such file or directory
[tla ~]$ ( test() { ls "$@"; }; test file1 file2 space\ file )
file1  file2  space file
Run Code Online (Sandbox Code Playgroud)