相关疑难解决方法(0)

访问bash命令行args $ @ vs $*

在许多SO问题和bash教程中,我看到我可以通过两种方式访问​​bash脚本中的命令行参数:

$ ~ >cat testargs.sh 
#!/bin/bash

echo "you passed me" $*
echo "you passed me" $@
Run Code Online (Sandbox Code Playgroud)

结果如下:

$ ~> bash testargs.sh arg1 arg2
you passed me arg1 arg2
you passed me arg1 arg2
Run Code Online (Sandbox Code Playgroud)

$*和之间有什么区别$@
应该何时使用前者,何时使用后者?

bash command-line-arguments

298
推荐指数
4
解决办法
18万
查看次数

在bash参数中保留引号

我正在制作一个bash脚本,它将打印并将复杂的参数传递给另一个外部程序.

./script -m root@hostname,root@hostname -o -q -- 'uptime ; uname -a'
Run Code Online (Sandbox Code Playgroud)

如何打印原始参数:

-m root@hostname,root@hostname -o -q -- 'uptime ; uname -a'
Run Code Online (Sandbox Code Playgroud)

使用$@$*删除uptime ; uname -a可能导致意外结果的单引号.我的脚本不需要解析每个参数.我只需要打印/记录参数字符串并将它们传递给另一个程序,确切地说它们是如何给出的.

我知道我可以用类似的东西来逃避引号, "'uptime ; uname -a'"但我无法保证用户会这样做.

linux bash

17
推荐指数
3
解决办法
9101
查看次数

从shell脚本中的变量运行shell命令

我试图从shell脚本中的变量运行命令.正在使用的shell是bash shell.

该文件exp包含:

abcdef
Run Code Online (Sandbox Code Playgroud)

执行以下命令:

sed s/b/\ / exp
Run Code Online (Sandbox Code Playgroud)

...产生输出:

a  cdef
Run Code Online (Sandbox Code Playgroud)

但执行:

cmd="sed s/b/\ / exp"
echo $cmd
$cmd
Run Code Online (Sandbox Code Playgroud)

...产生以下错误:

sed s/b/\ / exp
sed: -e expression #1, char 5: unterminated `s' command
Run Code Online (Sandbox Code Playgroud)

我可以看到eval在执行前添加工作.但我不明白为什么.你能解释一下为什么一种方法有效而另一种方法不起作用?

linux bash shell sed

7
推荐指数
2
解决办法
342
查看次数

如何从shell获取准确的命令行字符串?

我知道$*,$@,"$@"甚至${1+"@"}和他们的意思.

我需要从shell脚本访问EXACT命令行参数字符串.请注意示例中的引号.像"$ @"这样的东西保存参数但删除了引号,我看不出如何从中恢复.

例:

./my-shell.sh"1 2"3

我需要检索EXACT参数字符串而不进行任何处理:

"1 2"3

知道怎么做到这一点?

shell parsing arguments

3
推荐指数
1
解决办法
483
查看次数

标签 统计

bash ×3

linux ×2

shell ×2

arguments ×1

command-line-arguments ×1

parsing ×1

sed ×1