Solved

I need to validate script so error messages are returned

Posted on 2015-02-09
7
161 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Fine Tune your automatic Updates for Ubuntu / Debian
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

635 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