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" }'
Related videos on Youtube
Author by
Rebbeca
Updated on September 18, 2022Comments
-
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 over 6 yearsYou need to check output from another script or inside test1.sh?
-
-
Арсений Черенков over 6 yearsyou can shorten
grep | wc -l
usinggrep -c
-
Rebbeca over 6 yearsThanks for the help If I need another field in as acceptable in 'emp Type' ->ITdept or ITdomain then what to execute?
-
Egor Vasilyev over 6 yearsYou need only replace
ITdept
word aftergrep -c
. If 'emp Type' contain spaces you need to place it in quotes -
Rebbeca over 6 yearsThis gives an error :-./test2.sh: line 7: syntax error near unexpected token
(' ./test2.sh: line 7:
}' <(sh ./test1.sh)' -
Rebbeca over 6 yearsthis 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 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 over 6 yearsAh 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 over 6 yearsjust wanted to know that any "string" I want to check will this check in all of the following and do the job
-
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 over 6 yearsThanks 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