相关疑难解决方法(0)

我们如何运行存储在变量中的命令?

$ ls -l /tmp/test/my\ dir/
total 0
Run Code Online (Sandbox Code Playgroud)

我想知道为什么以下运行上述命令的方法失败或成功?

$ abc='ls -l "/tmp/test/my dir"'

$ $abc
ls: cannot access '"/tmp/test/my': No such file or directory
ls: cannot access 'dir"': No such file or directory

$ "$abc"
bash: ls -l "/tmp/test/my dir": No such file or directory

$ bash -c $abc
'my dir'

$ bash -c "$abc"
total 0

$ eval $abc
total 0

$ eval "$abc"
total 0
Run Code Online (Sandbox Code Playgroud)

shell bash quoting variable

104
推荐指数
2
解决办法
13万
查看次数

是否可以“保护”IFS 字符免受字段拆分?

在 POSIX sh 或 Bourne shell(如在 Solaris 10's 中/bin/sh)中,是否有可能具有以下内容:

a='some var with spaces and a special space'
printf "%s\n" $a
Run Code Online (Sandbox Code Playgroud)

并且,使用默认值IFS,得到:

some
var
with
spaces
and
a
special space
Run Code Online (Sandbox Code Playgroud)

也就是说,保护之间的空间special,并space通过引用或逃逸的某种组合?

a事先不知道字数,或者我会尝试类似的东西:

a='some var with spaces and a special\ space'
printf "%s\n" "$a" | while read field1 field2 ...
Run Code Online (Sandbox Code Playgroud)

上下文是Cassandra 中报告的这个错误,其中 OP 试图设置一个环境变量,指定 JVM 的选项:

export JVM_EXTRA_OPTS='-XX:OnOutOfMemoryError="echo oh_no"'
Run Code Online (Sandbox Code Playgroud)

在执行 Cassandra 的脚本中,它必须支持 POSIX sh 和 Solaris sh:

JVM_OPTS="$JVM_OPTS $JVM_EXTRA_OPTS"
#...
exec …
Run Code Online (Sandbox Code Playgroud)

shell posix bourne-shell

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

标签 统计

shell ×2

bash ×1

bourne-shell ×1

posix ×1

quoting ×1

variable ×1