简体   繁体   中英

Bash script to pass two arguments to get status of ssl

I have a bash script "domain-ssl-status.sh". I need a script with two arguments so that I could run the script in the following way:

./domain-ssl-status.sh cname.domain.com status|unobtained|obtained|error

domainName and status are my 2 arguments

domainName=$1 and status=$2

I have tried creating a status_map using a case statement, but no luck!! I have also seen other hints on here but mine never seems to work. My sql statement includes SELECT * FROM DomainSSL WHERE domainName='cname.domain.com' and I'm still stuck.

A rough pass at a rewrite -

#!/bin/bash 
domainName=$1 
status=$2 
echo "Verifying domain"
case status in
$status) ping -c 1 $domainName || {
           echo "Cannot ping $domainName" >&2
           exit 1
         } ;;
*) echo "Invalid argument '$status'" >&2
   exit 1 ;;
esac

sql="SELECT * FROM DomainSSL WHERE domainName='$domainName'" 
mssql -f csv -c ~/applications/mssql/mssql.json -q "$sql" # mark here
rc=$?
if (( rc )) # nonzero
then echo "FAIL: rc $rc on [$sql]" >&2
else echo "SUCCESS: $sql" 
fi 

You might also want to try saving the stdout and stderr for later parsing. Rewriting from # mark above,

mssql -f csv -c ~/applications/mssql/mssql.json -q "$sql" >ok 2>oops
rc=$?
if (( rc )) # nonzero
then echo -e "FAIL: rc $rc on [$sql]:\n$(<oops)" >&2
     case "$(grep SQLSTATE oops)" in
     *ER_DUP_KEY*) : code to handle duplicate keys error ;;
     # any other errors you choose to handle . . .
     *) : code for bailing on errors you don't care to handle ;;
     esac
else echo "SUCCESS: $sql" 
fi 

cf the mysql documentation here

This is just a general template. Hope it helps. Feel free to ask for clarification.

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