bourne shell 单引号、双引号 &反引号问题

发布于 2024-08-16 10:10:28 字数 329 浏览 4 评论 0原文

/!bin/sh

   if [ "`echo $desc $status | awk -F"," '{print $3}' | awk -F" " '{print $1}' | sed '/^$/d'`" != "OK" ]; then
        echo "howdy dody"
   fi

echo $desc $status | awk -F"," '{print $3}' | awk -F" " '{print $1}' | sed '/^$/d'

第一个 if 条件不会运行,我猜这是因为引用不当,但我无法弄清楚。

预先感谢您的任何帮助。

/!bin/sh

   if [ "`echo $desc $status | awk -F"," '{print $3}' | awk -F" " '{print $1}' | sed '/^$/d'`" != "OK" ]; then
        echo "howdy dody"
   fi

echo $desc $status | awk -F"," '{print $3}' | awk -F" " '{print $1}' | sed '/^$/d'

First if-condition won't run, im guessing it's because of improper quotation, but i can't figure it out.

Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

£噩梦荏苒 2024-08-23 10:10:29

如果将测试包装在函数中,则编写测试会容易得多:

mytest()
{
  echo "$1 $2" \
  | awk -F"," -v arg3="$3" '{print arg3}' \
  | awk -F" " -v arg1="$1" '{print arg1}' \
  | sed '/^$/d'
}

这样,您可以验证它是否正常工作。一旦你获得了这种信心

if [ "$(mytest "$desc" "$status")" != "OK" ]; then
        echo "howdy doody"
fi

if mytest "$desc" "$status" | grep -q -v '^OK
; then
  echo "howdy doody"
fi

It is much easier to write your test if you wrap it in a function:

mytest()
{
  echo "$1 $2" \
  | awk -F"," -v arg3="$3" '{print arg3}' \
  | awk -F" " -v arg1="$1" '{print arg1}' \
  | sed '/^$/d'
}

This way, you can verify that it works correctly. Once you gained this confidence

if [ "$(mytest "$desc" "$status")" != "OK" ]; then
        echo "howdy doody"
fi

or

if mytest "$desc" "$status" | grep -q -v '^OK
; then
  echo "howdy doody"
fi
橙幽之幻 2024-08-23 10:10:29

如果您使用 Bash,我建议使用 $(...) 而不是反引号。您收到什么错误消息?我的猜测是 awk 的 -F"," 选项没有被正确引用。尝试插入 \ 来转义引号。

If you're using Bash, I'd recommend $(...) instead of back-quotes. What error messages do you get? My guess is that the -F"," option to awk is not being quoted properly. Trying inserting \ to escape the quotation marks.

霞映澄塘 2024-08-23 10:10:29

乍一看,您可能想尝试转义一些双引号

   if [ "`echo $desc $status | awk -F"," '{print $3}' | awk -F" " '{print $1}' | sed '/^$/d'`" != "OK" ]; then
        echo "howdy dody"
   fi

   if [ "`echo $desc $status | awk -F\",\" '{print $3}' | awk -F\" \" '{print $1}' | sed '/^$/d'`" != "OK" ]; then
        echo "howdy doody"
   fi

At first glance, you might want to try escaping some of the double quotes:

   if [ "`echo $desc $status | awk -F"," '{print $3}' | awk -F" " '{print $1}' | sed '/^$/d'`" != "OK" ]; then
        echo "howdy dody"
   fi

to

   if [ "`echo $desc $status | awk -F\",\" '{print $3}' | awk -F\" \" '{print $1}' | sed '/^$/d'`" != "OK" ]; then
        echo "howdy doody"
   fi
情域 2024-08-23 10:10:29

转义双引号当然是一个好主意,但看起来 $3 和 $1 旨在由 awk 解释。相反,它们由您的 shell 解释。您可能想转义“$”。 (您可能在 shell 中具有有意义的 $1 和 $3 值,但可能性不大。)

Escaping the double quotes is certainly a good idea, but it looks like the $3 and the $1 are intended to be interpreted by awk. They are being interpreted by your shell instead. You probably want to escape the '$'s. (It is possible that you have meaningful values for $1 and $3 in the shell, but not likely.)

白首有我共你 2024-08-23 10:10:28

您还可以在 -F 选项的参数周围使用单引号,就像在其他参数周围使用单引号一样:

if [ "`echo $desc $status | awk -F',' '{print $3}' | awk -F' ' '{print $1}' | sed '/^$/d'`" != "OK" ]; then

You can also use single quotes around the argument to the -F option as you have around other arguments:

if [ "`echo $desc $status | awk -F',' '{print $3}' | awk -F' ' '{print $1}' | sed '/^$/d'`" != "OK" ]; then
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文