1

I have a shell script (test1.sh) which returns the following output

 Employee ID          emp Type  return type  Admin User
   us321000034006755    ITdept      access    Itadminuser

I wanted to check if the output contains string ITdept for that I have used the following:

if ./test1.sh | grep -q 'ITdept'; 
then
    echo "found"
else
    echo "Not found"
fi

Along with this I wanted to check the the strings Employee ID us321000034006755 too since it doesn't return any fruitful results with the command I am using not sure how to put this through. Am I missing something? any advice would be great

  • You need to check output from another script or inside test1.sh? – Egor Vasilyev Sep 13 '17 at 8:25
0

If the output will always contain only 2 lines - awk solution to check by multiple fields:

awk 'NR==2 { 
         printf "%s%s\n",($1=="us321000034006755" && $2=="ITdept")? "":"not ","found" 
     }' <(sh ./test1.sh)
  • This gives an error :-./test2.sh: line 7: syntax error near unexpected token (' ./test2.sh: line 7: }' <(sh ./test1.sh)' – Rebbeca Sep 13 '17 at 9:31
  • @Rebbeca, it's your script error: it indicates line 7. My script doesn't have 7 lines. Make sure you have specified the path to that shell script properly and there are no other errors in your shell script – RomanPerekhrest Sep 13 '17 at 9:41
  • this in the content of file in test2.sh:- ./test1.sh awk 'NR==2 { printf "%s%s\n",($1=="us321000034006755" && $2=="ITdept")? "":"not ","found" }' <(sh ./test1.sh) – Rebbeca Sep 13 '17 at 9:48
  • @Rebbeca, I've posted my approach as independent script. You should not have been put it into your script to call itself, that's pointless. Run the above awk script as is – RomanPerekhrest Sep 13 '17 at 9:51
  • Ah thanks a lot for pointing out the error :-) just one question if I need to check whether 'ITdept' or 'Emp ID' or one more lets say ('access') field is present what do I need to do should I increase the NR==3 and then add in awk as : printf "%s%s\n",($1=="us321000034006755" && $2=="ITdept" && $3 "access")? "":"not ","found" – Rebbeca Sep 13 '17 at 9:57
0

If you need to execute this check inside another script you can do that with something like this:

#!/bin/bash

var=$(./test1.sh | grep -c ITdept)

if [ $var > 0 ];
then
    echo "found"
else
    echo "Not found"
fi

Script counts the number of strings which contain one or more 'ITdept' words.

Or in single line (with awk):

./test1.sh | grep -c ITdept | awk '{if ($0==0) print "not found"; else if ($0>0) print "found"}'

correct me if I misunderstood your task.

  • 1
    you can shorten grep | wc -l using grep -c – Archemar Sep 13 '17 at 9:01
  • Thanks for the help If I need another field in as acceptable in 'emp Type' ->ITdept or ITdomain then what to execute? – Rebbeca Sep 13 '17 at 9:20
  • You need only replace ITdept word after grep -c. If 'emp Type' contain spaces you need to place it in quotes – Egor Vasilyev Sep 13 '17 at 9:24
0

Assuming that the output will contain the string us321000034006755(separator)ITdept:

if cmd | grep -q '[[:<:]]us321000034006755[[:>:]][[:space:]]*[[:<:]]ITdept[[:>:]]'; then
   ...
fi

If you have the two substrings in variables:

if cmd | grep -q "[[:<:]]$user_id[[:>:]][[:space:]]*[[:<:]]$user_dept[[:>:]]"; then
   ...
fi

The [[:<:]] and [[:>:]] will match on word boundaries.


It would be a lot easier to do this using awk, as RomanPerekhrest suggests, or

cmd | awk '$1 == "us321000034006755" && $2 == "ITdept" { print "found"; exit } END { print "not found" }'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.