Check if the particular string is present in a output of shell script using shell command

13,186

Solution 1

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)

Solution 2

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" }'
Share:
13,186

Related videos on Youtube

Rebbeca
Author by

Rebbeca

Updated on September 18, 2022

Comments

  • Rebbeca
    Rebbeca over 1 year

    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

    • Egor Vasilyev
      Egor Vasilyev over 6 years
      You need to check output from another script or inside test1.sh?
  • Арсений Черенков
    Арсений Черенков over 6 years
    you can shorten grep | wc -l using grep -c
  • Rebbeca
    Rebbeca over 6 years
    Thanks for the help If I need another field in as acceptable in 'emp Type' ->ITdept or ITdomain then what to execute?
  • Egor Vasilyev
    Egor Vasilyev over 6 years
    You need only replace ITdept word after grep -c. If 'emp Type' contain spaces you need to place it in quotes
  • Rebbeca
    Rebbeca over 6 years
    This gives an error :-./test2.sh: line 7: syntax error near unexpected token (' ./test2.sh: line 7: }' <(sh ./test1.sh)'
  • Rebbeca
    Rebbeca over 6 years
    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)
  • RomanPerekhrest
    RomanPerekhrest over 6 years
    @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
  • Rebbeca
    Rebbeca over 6 years
    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
    Rebbeca over 6 years
    just wanted to know that any "string" I want to check will this check in all of the following and do the job
  • RomanPerekhrest
    RomanPerekhrest over 6 years
    @Rebbeca, you can check field value by specifying its number, for ex. check if "access" is in 3rd field: awk 'NR==2 { printf "%s%s\n",($1=="us321000034006755" && $2=="ITdept" && $3=="access")? "":"not ","found" }' <(sh ./test1.sh)
  • Rebbeca
    Rebbeca over 6 years
    Thanks the script above checks the lines but there is no way to implement anything after the results, if the output contains not found I need to do exit 1 where can I place the following in above script ? This help would really solve the issue