Solved

I need to validate script so error messages are returned

Posted on 2015-02-09
7
159 Views
Last Modified: 2015-02-09
My code cycles through several enviroments looking for a first/last name combination. If that is found it will print out the user_id. If not 'NO USER FOUND' is output.

I have a third issue that I have to check for, and am not quite sure how to do this. If for some reason the connection to the database fails, I need to produce a message like "DATABASE CONNECTION FAILED"

echo "environment"
sshpass -p $ldappw ssh $ldapuser@12.345.678.901  'mysql --skip-column-names -h host -u user -ppassword database -e "select user_id from users where first like '"'%${first_name}%' and last like '%${last_name}%';"'"' >$out_file 
sql_string=$(cat $out_file)
if [[ ! -z $sql_string ]]; then
   echo "$sql_string"
else
   echo "NO USER FOUND"
fi

Open in new window

0
Comment
Question by:TampaJay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 40599430
Why didn't you use my suggestion from our previous thread? Are you a big fan of temporary files?

Anyway, change my solution from the previous thread to

#log it all
{
if UID=$(sshpass -p $ldappw ssh $ldapuser@12.345.67.89 'mysql --skip-column-names -hhost -u user -ppassword database -e \
"select user_id from users where first like '"'%${first_name}%' and last like '%${last_name}%';"'"')
then
   if [[ ! -z $UID ]]; then
      echo "environment"
      echo "$UID"
        else
           echo "NO USER FOUND"
   fi
else
  echo  "DATABASE CONNECTION FAILED"
fi 
} > $log

Open in new window


You can take out "echo "environment"" and put it above the line containing "sshpass" as in your question, if you like, yet this would somehow contradict what we discussed in that famous previous thread.
0
 

Author Comment

by:TampaJay
ID: 40599484
I  must have missed that first time around. Sorry about that.

Thanks for the help with these few problems - been a great assistance!
0
 

Author Closing Comment

by:TampaJay
ID: 40599485
Excellent contributor to the site!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:TampaJay
ID: 40599545
I actually spoke a little too soon...

The logic you provided will now give me 'DATABASE CONNECTION FAILED' instead of 'NO USER FOUND'
0
 

Author Comment

by:TampaJay
ID: 40599555
Error I'm getting is as follows:

ERROR 2005 (HY000): Unknown MySQL server host 'database' (0)

However, this isn't being recognized properly.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40599604
Did you put all if's, then's and else's in the right places?

It might be that the "mysql" executable would issue a nonzero returncode when a "no record found" occurs (yet I don't believe so).
I'll check that.
0
 

Author Comment

by:TampaJay
ID: 40599617
NVM, user error :-)
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

736 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question