LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-27-2016, 01:43 AM   #1
Mike_Brown
Member
 
Registered: May 2015
Posts: 37

Rep: Reputation: Disabled
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.

Last edited by Mike_Brown; 01-27-2016 at 08:09 PM.
 
Old 01-27-2016, 04:27 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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).
 
Old 01-27-2016, 03:39 PM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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
 
Old 01-27-2016, 04:31 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Single "=" with enhanced test should work.
Yeah, I know, "==" makes more sense if you've done any real programming.
 
Old 01-27-2016, 08:09 PM   #5
Mike_Brown
Member
 
Registered: May 2015
Posts: 37

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
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
 
Old 01-27-2016, 08:09 PM   #6
Mike_Brown
Member
 
Registered: May 2015
Posts: 37

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
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
 
Old 01-28-2016, 08:28 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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

Last edited by MadeInGermany; 01-28-2016 at 08:30 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace a string if another specific string exists in the line victorcheung Programming 3 06-26-2015 07:20 PM
[SOLVED] C - How to put a specific arbitrary part of a string into it's own string? golmschenk Programming 9 04-19-2010 08:27 PM
read string after specific string from a text file using C++ programing language badwl24 Programming 5 10-08-2009 05:41 AM
bash-script: output text between two ocurrences of a specific string isl01jbe Programming 1 06-17-2004 02:36 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration