相关疑难解决方法(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万
查看次数

为什么 bash 变量扩展保留引号?

> echo "hi"
hi
> VAR='echo "hi"'
> $VAR
"hi"
Run Code Online (Sandbox Code Playgroud)

为什么上述命令的输出不同?

单引号也会发生类似的事情:

> VAR="echo 'hi'"
> $VAR
> 'hi'
Run Code Online (Sandbox Code Playgroud)

shell bash quoting

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

在“变量中的命令”中引用/转义/扩展问题

我想在 bash 脚本中运行这样的命令:

freebcp <authentication and other parameters> -t "," -r "\r\n"
Run Code Online (Sandbox Code Playgroud)

直接在命令行上运行时,该命令按预期工作。但是当放在 bash 脚本中的变量中时,它会返回如下错误:

Msg 20104, Level 3
Unexpected EOF encountered in bcp datafile

Msg 20074, Level 11
Attempt to bulk copy an oversized row to the server

bcp copy in failed
Run Code Online (Sandbox Code Playgroud)

当命令放在变量中并且双引号被转义时:

cmd="freebcp ${db_name}.dbo.${table_name} in ${p_file} -S ${server_name} -U ${username} -P ${password} -t \",\" -r \"\r\n\" -c"
`$cmd`
Run Code Online (Sandbox Code Playgroud)

注意:将以下内容按预期工作:

`freebcp ${db_name}.dbo.${table_name} in ${p_file} -S ${server_name} -U ${username} -P ${password} -t "," -r "\r\n" -c`
Run Code Online (Sandbox Code Playgroud)

所以我知道有一些引用/转义/扩展问题,但我不知道如何解决它。

注 2 …

bash shell-script quoting

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

标签 统计

bash ×3

quoting ×3

shell ×2

shell-script ×1

variable ×1