Match O/p of a command

Hello,

We have a script, when executed it asks for ( it searches servername is portal inventory )

Username
Password

Once's you provide that - if your server name is present in the portal it returns

O/p
PASS Gold correctly registered

My Query is based on above o/p
i want to decide weather to proceed next in script or not
so, if it says PASS then proceed else exit with a message.

I am not able to find a logic to match the o/p word PASS.....

how can i take o/p as input to match condition...any help is appreciated....

Does your script have a return code? If so you could try:

if script > /dev/null 2>&1; then
  echo "we can proceed"
else
  echo "we cannot"
fi

Or if you need to match "PASS" in the scripts output you could try:

if script | grep -q PASS; then
 echo "we can proceed"
else
  echo "we cannot"
fi
1 Like

I tried option 2 as suggested.

if /home/script/myscript.py | grep -q PASS;
 then
 echo "we can proceed"
else
  echo "we cannot"
fi

Unfortunately, It did not worked..

as It it did not prompt anything ( As, when the script is run - then it prompt for user and password, and when then input is provided - It checks in the portal ( whose credentials we have given ) and return back if found in inventory ]

So, What i have thought is

run the script and provide username and password and when the o/p is given PASS or FAIL - I should take the o/p in a file and grep PASS or FAIL from that...

Now, the issue how to redirect the script o/p to a temporary file....

any suggestions ?

If you pipe the output to grep (or anything), of course nothing can be prompted to e.g. the screen. Are you able to modify the script? Then prompt to another file descriptor pointing to the screen.
Still the output PASS should be detected by grep and the evaluation thereafter should be OK.

Unfortunately - that script is in python and i am not authorized to edit it . :frowning:

looking for another way out

Try

/home/script/myscript.py | tee /dev/tty | grep -q PASS;

It really would be preferable if you could the one who maintains the script if they could add a return code, then you could try:

if script; then
  echo "we can proceed"
else
  echo "we cannot"
fi

If not, you could try something like:

script |
{ 
  found=false 
  while read answ 
  do
    printf "%s\n" "$answ"
    case $answ in 
      (*PASS*) found=true
    esac
  done
  if $found; then 
      echo "we can proceed"
  else
    echo "we cannot"
  fi
}

-- edit --

Indeed in the second case, RudiC's idea would probably be a better suggestion:

if script | tee /dev/tty | grep -q PASS; then
  echo "we can proceed"
else
  echo "we cannot"
fi

Thank you for the reply.

I tried both the options provided by Rudic and Scrutinizer

However, when i ran it - it will stuck as the script will prompt for username and password.

---------- Post updated 12-28-13 at 12:21 AM ---------- Previous update was 12-27-13 at 11:29 PM ----------

I tried both the options given by Rudic and Scrutinizer. However, it hangs bcoz in the script it asks for username and password.