Bourne Shell 退出将不起作用

发布于 2024-08-20 15:08:54 字数 335 浏览 8 评论 0原文

我有以下脚本,

cat $1 | while read line
do
    line=`echo $line | tr "[:lower:]" "[:upper:]"`

    if [ "`echo $line | cut -f1 -d:`" = "foo" ] && \
       [ "`echo $line | cut -f2 -d:`" = "bar" ]; then
        echo 'exsist'
        exit 1;
    fi
done

一切正常,然后当脚本点击退出时,它不会并继续运行。任何想法。

谢谢

I have the following script

cat $1 | while read line
do
    line=`echo $line | tr "[:lower:]" "[:upper:]"`

    if [ "`echo $line | cut -f1 -d:`" = "foo" ] && \
       [ "`echo $line | cut -f2 -d:`" = "bar" ]; then
        echo 'exsist'
        exit 1;
    fi
done

everything works up to echo and then when the script hits exit it does not and keeps going. Any ideas.

Thanks

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

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

发布评论

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

评论(4

花开半夏魅人心 2024-08-27 15:08:54

您不需要那个反斜杠 - 这不是 C shell。

问题在于 while 循环位于子 shell 中,该循环退出,但由于它作为子 shell 运行,因此主脚本继续执行。

在上下文中,最简单的解决方法可能是:

while read line
do
    line=`echo $line | tr "[:lower:]" "[:upper:]"`
    if [ "`echo $line | cut -f1 -d:`" = "foo" ] &&
       [ "`echo $line | cut -f2 -d:`" = "bar" ]; then
        echo 'exist'
        exit 1
    fi
done < $1

如果您必须处理多个文件('cat "$@"' 而不是 'cat $1'),那么您必须更加努力一点:

cat "$@" |
while read line
do
    line=`echo $line | tr "[:lower:]" "[:upper:]"`
    if [ "`echo $line | cut -f1 -d:`" = "foo" ] &&
       [ "`echo $line | cut -f2 -d:`" = "bar" ]; then
        echo 'exist'
        exit 1
    fi
done
[ $? != 0 ] && exit 1

检查由 'cat' 和 'while' 组成的管道的退出状态,即 'while' 循环的退出状态,在示例中如果在开始处找到 'foo:bar' 则为 1一条线。

当然,还有其他方法可以检测到这一点,例如:

grep -s -q "^foo:bar:" "$@" && exit 1

这比循环版本执行的命令要少得多。 (如果您还需要允许 '^foo:bar$',请使用egrep 而不是普通的grep。)

You don't need that backslash - this is not the C shell.

The problem is that the while loop is in a sub-shell, which exits, but because it is run as a sub-shell, the main script continues.

In the context, the simplest fix is probably:

while read line
do
    line=`echo $line | tr "[:lower:]" "[:upper:]"`
    if [ "`echo $line | cut -f1 -d:`" = "foo" ] &&
       [ "`echo $line | cut -f2 -d:`" = "bar" ]; then
        echo 'exist'
        exit 1
    fi
done < $1

If you have to process multiple files ('cat "$@"' instead of 'cat $1'), then you have to work a lot harder bit harder:

cat "$@" |
while read line
do
    line=`echo $line | tr "[:lower:]" "[:upper:]"`
    if [ "`echo $line | cut -f1 -d:`" = "foo" ] &&
       [ "`echo $line | cut -f2 -d:`" = "bar" ]; then
        echo 'exist'
        exit 1
    fi
done
[ $? != 0 ] && exit 1

This checks the exit status of the pipeline consisting of 'cat' and 'while', which is the exit status of the 'while' loop, which will be 1 in the example if 'foo:bar' is found at the start of a line.

Of course, there are other ways to detect that, such as:

grep -s -q "^foo:bar:" "$@" && exit 1

This executes a lot less commands than the loop version. (If you need to allow for '^foo:bar$' as well, use egrep instead of plain grep.)

遇见了你 2024-08-27 15:08:54

您将文本从小写文本翻译为大写文本,然后针对小写文本进行测试,因此您永远不会到达出口。

You're translating your text from lower case text to upper case text, but then testing against lower case, so you never get to the exit.

泅渡 2024-08-27 15:08:54

因为你想将每一行转换为大写,你可以这样做

#!/bin/sh

tr '[:lower:]' '[:upper:]' < file | while IFS=":" read -r a b c
do
    case "$a $b" in
        "FOO BAR" ) echo "exist";exit;;
        *) echo "$a $b";;
    esac
done

,或者你可以只使用 awk(nawk for Solaris)

nawk -F":" '
topper($1)=="FOO" && topper($2)=="BAR"{
    print "exist"
    exit
}
{
    print topper($0)
}
' file

since you want to convert to upper case for every line, you can do it like this

#!/bin/sh

tr '[:lower:]' '[:upper:]' < file | while IFS=":" read -r a b c
do
    case "$a $b" in
        "FOO BAR" ) echo "exist";exit;;
        *) echo "$a $b";;
    esac
done

OR you can do it with just awk(nawk for Solaris)

nawk -F":" '
topper($1)=="FOO" && topper($2)=="BAR"{
    print "exist"
    exit
}
{
    print topper($0)
}
' file
愛放△進行李 2024-08-27 15:08:54

您可以通过在测试上方添加 shopt -s nocasematch 来使条件不区分大小写。要将其设置回区分大小写:shopt -u nocasematch

You can make your conditionals case-insensitive by adding shopt -s nocasematch above the test. To set things back to case-sensitive: shopt -u nocasematch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文