bash: 用引号引用 var
bash: quote var with quotes
我尝试制作一个用于调试的虚拟脚本。该脚本应将命令和所有参数传递给系统日志。但是当我得到一个有引号的参数时,我只会失败。
#!/bin/sh
set -x
param="$*"
logger -t [=11=] -p local3.debug "$*"
#logger -t [=11=] -p local3.debug "$param"
#logger -t [=11=] -p local3.debug "$(echo "$param")"
#logger -t [=11=] -p local3.debug "${param//\"/}"
#logger -t [=11=] -p local3.debug "${param[@]}"
我尝试了一些我发现的变体,但没有任何效果。
# d-iptables -L dkdsdds "ddd"
+ param='-L dkdsdds ddd'
++ echo '-L dkdsdds ddd'
+ logger -t /usr/sbin/d-iptables -p local3.debug '-L dkdsdds ddd'
logger: invalid option -- L
Usage:
logger [options] [message]
Options:
-d, --udp use UDP (TCP is default)
-i, --id log the process ID too
-f, --file <file> log the contents of this file
-h, --help display this help text and exit
-n, --server <name> write to this remote syslog server
-P, --port <number> use this UDP port
-p, --priority <prio> mark given message with this priority
-s, --stderr output message to standard error as well
-t, --tag <tag> mark every line with this tag
-u, --socket <socket> write to this Unix socket
-V, --version output version information and exit
我建议您将脚本更改为 #!/bin/bash
并使用
logger -t [=10=] -p local3.debug -- "$(printf "%q " "$@")"
您需要 --
,因此记录器认为 -L
不是其选项之一。
首先,您将其标记为 bash,但您使用的是 /bin/sh(bourne 或 dash)。接下来,您的记录器正在读取 - 作为参数。只是引用它更好。
#!/bin/bash
set -x
param="\"$@\"" # quotes removed by the shell
logger -t [=10=] -p local3.debug "'$param'" # expand, then strong quote
我尝试制作一个用于调试的虚拟脚本。该脚本应将命令和所有参数传递给系统日志。但是当我得到一个有引号的参数时,我只会失败。
#!/bin/sh
set -x
param="$*"
logger -t [=11=] -p local3.debug "$*"
#logger -t [=11=] -p local3.debug "$param"
#logger -t [=11=] -p local3.debug "$(echo "$param")"
#logger -t [=11=] -p local3.debug "${param//\"/}"
#logger -t [=11=] -p local3.debug "${param[@]}"
我尝试了一些我发现的变体,但没有任何效果。
# d-iptables -L dkdsdds "ddd"
+ param='-L dkdsdds ddd'
++ echo '-L dkdsdds ddd'
+ logger -t /usr/sbin/d-iptables -p local3.debug '-L dkdsdds ddd'
logger: invalid option -- L
Usage:
logger [options] [message]
Options:
-d, --udp use UDP (TCP is default)
-i, --id log the process ID too
-f, --file <file> log the contents of this file
-h, --help display this help text and exit
-n, --server <name> write to this remote syslog server
-P, --port <number> use this UDP port
-p, --priority <prio> mark given message with this priority
-s, --stderr output message to standard error as well
-t, --tag <tag> mark every line with this tag
-u, --socket <socket> write to this Unix socket
-V, --version output version information and exit
我建议您将脚本更改为 #!/bin/bash
并使用
logger -t [=10=] -p local3.debug -- "$(printf "%q " "$@")"
您需要 --
,因此记录器认为 -L
不是其选项之一。
首先,您将其标记为 bash,但您使用的是 /bin/sh(bourne 或 dash)。接下来,您的记录器正在读取 - 作为参数。只是引用它更好。
#!/bin/bash
set -x
param="\"$@\"" # quotes removed by the shell
logger -t [=10=] -p local3.debug "'$param'" # expand, then strong quote