简体   繁体   中英

KornShell check using exit codes

I am trying to create a KornShell (ksh) script to check the status of a process. If the status is "pass", it will just echo "pass". If the status is "fail", it will send an alert.

I can think of two methods:

  1. Capture the process status into a temp file and search for the terms "pass" or "fail" in that file and action accordingly

    eg

     servicename > tmp.file grep pass tmp.file if exists, echo "pass" 
  2. Grep for "pass" or "fail" and use the exit code for other operations

    eg

     servicename | grep pass if exit $? = 0, echo "pass" else do something 

What do you think of the above two approaches and how would you approach it? Any code snippets would be greatly appreciated.

Assuming servicename returns a non-zero exit code on failure, you can do:

if servicename > /dev/null 2>&1
then
    echo pass
else
   # do something
fi

If servicename doesn't set its exit code correctly:

if servicename | grep -q pass
then
    echo pass
else
   # do something
fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM