LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to test the output only contains a specific string (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-test-the-output-only-contains-a-specific-string-4175567998/)

Mike_Brown 01-27-2016 01:43 AM

How to test the output only contains a specific string
 
I use following shell script

Revised code
Code:

    host_list="/home/campus27/zwang10/bin/hostlist"
HOSTS=`cat $host_list`
cp /dev/null hostlist_available
for line in $HOSTS
do
ssh -o ConnectTimeout=1s $line true &>/dev/null
RESULT=$?
if [ $RESULT -eq 0 ]
        then
        output=$(ssh -f $line "w" | tail -n+3 | awk '{print $1}')
        if [[ $RESULT -eq 0 ]] && [[ -z $output || $output == 'zwang10' ]]
                then
                echo $line
                echo $line >> hostlist_available
        fi
fi
done

I would like to do following:

**Under the condition that
Code:

[ $RESULT -eq 0 ]
holds,
either
Code:

[ -z $(ssh -f $line "w" | tail -n+3 | awk '{print $1}') ]
or
Code:

$(ssh -f $line "w" | tail -n+3 | awk '{print $1}') only contains string "zwang10"
holds, we still continue the loop.**

The command
Code:

$(ssh -f $line "w" | tail -n+3 | awk '{print $1}') ]
will output like following cases:

Only one of my name

Code:

zwang10
Several my names

Code:


  zwang10
  zwang10

Some others name

Code:

    jack
    jack
    ben

Other names and mine

Code:

    jack
    zwang10
    zwang10

How can test whether the output only contains my name `zwang10`?

Revised requirement
Code:

$output == 'zwang10'
only output yes when there is only one
Code:

zwang10
. However, I also want it will output yes when there are multiple zwang10's and no other names.

syg00 01-27-2016 04:27 AM

You seem to be of aware numeric comparisons (and -z which is a string operator) in test, so just use the string comparison operator(s).

MadeInGermany 01-27-2016 03:39 PM

The shell string match [[ == ]] has a problem with exact line match.
That's why I'd use grep here
Code:

if ssh ... | fgrep -xq zwang10
then
 ...
fi

For example it will not match zwang100

syg00 01-27-2016 04:31 PM

Single "=" with enhanced test should work.
Yeah, I know, "==" makes more sense if you've done any real programming.

Mike_Brown 01-27-2016 08:09 PM

Quote:

Originally Posted by syg00 (Post 5487883)
You seem to be of aware numeric comparisons (and -z which is a string operator) in test, so just use the string comparison operator(s).

Hello, I revised my problem

Mike_Brown 01-27-2016 08:09 PM

Quote:

Originally Posted by MadeInGermany (Post 5488186)
The shell string match [[ == ]] has a problem with exact line match.
That's why I'd use grep here
Code:

if ssh ... | fgrep -xq zwang10
then
 ...
fi

For example it will not match zwang100

Hello, I revised my problem

MadeInGermany 01-28-2016 08:28 AM

Based on your revised code.
Code:

host_list="/home/campus27/zwang10/bin/hostlist"
HOSTS=`cat $host_list`

# open the output file for this block, with descriptor 3 (keep 1=stdout and 2=stderr)
for host in $HOSTS
do
  if ssh -nx -o ConnectTimeout=1s "$host" true &>/dev/null
  then
    # No "ssh -f" # stay synchronous if we want to safely capture the output
    # "who" is faster than "w", and is without header
    # The last part of the pipe is the effective exit status
    # Instead of "command; if [ $? -eq 0 ]" you can directly use "if command"
    if ssh -nx "$host" "who" | awk '{print $1}' | fgrep -qx zwang10
    then
      echo "$host"
      echo "$host" >&3  # write to descriptor 3
    fi
  fi
done 3> hostlist_available
# end of the block; the redirected file is closed



All times are GMT -5. The time now is 12:01 AM.