Bourne Shell output will not work

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 until the echo, and then when the script output exits, it does not and continues to move. Any ideas.

thank

+3
source share
4 answers

You don't need this backslash - it's not a C shell.

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

In the context, perhaps the simplest fix:

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 '), lot 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

, "cat" "while", while, 1 , "foo: bar" .

, , :

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

, . ( '^ foo: bar $', egrep grep.)

+5

, , .

+1

,

#!/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 Solaris)

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

You can make your conditions case insensitive by adding the shopt -s nocasematchabove test. To return the value in the register: shopt -u nocasematch.

0
source

Source: https://habr.com/ru/post/1731640/


All Articles