Solved

I need to validate script so error messages are returned

Posted on 2015-02-09
7
157 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
  • 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

713 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