LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-01-2013, 10:16 PM   #1
air4time
Member
 
Registered: Aug 2004
Distribution: Slackware 10.2
Posts: 35

Rep: Reputation: 15
Bash script ..Is there a better way?


What I am wanting to do is check the connection to a database via isql. I can do this easy enough from a termial by
~ $ isql -v test
it will show me a few commands I can do then puts me at a prompt SQL>
At this put I just quit out of it using "SQL>quit".

The below code does do this but there may be a better way I am thinking. Most stripts I tried would stop until I typed in quit my self. Then the rest of the stript would run. I was able to get working with the below code but this is not exacly what I was wanting the stript to do.

I wanted it to check for the "SQL>" then send quit and end. If "SQL> was not there then email me with the "bat.bat" file I made already. I will have the code running ever 30 minutes to check the connection.

Any advise would be appricated.If I have comments below that are not correct please tell me. I am not sure on the workings of the code so more info on what it is doing would be great as well. It in the end this does what I want it to do and would be nice to know how. :-)

Code:
#!/bin/bash
exec 2>test.txt                                  # error output and replace info in test.txt

CMD="quit"                                       # define variable for the command I need to do if login is a success this quits isql
T="$(date)"                                      # define variable for date I am using   

isql -v test   <<END_SCRIPT                      # This logs me in using isql test=ODBC dns using unixODBC

$CMD                                             # sends quit 

END_SCRIPT                                       # This I am not sure of. I know it is with the one above but unsure what the two do . But they did allow me to send the quit command if SQL> was present.

if [ -s test.txt ]                               # if test.txt size is greater than zero
then
     /root/bat.bat  # runs bat.bat

 echo "Failed login          ${T}"  >> good.txt  # appends to text file


else                                             # test.txt size is not greater than zero

 echo "good login  ${T}">> good.txt              # appends to text file

fi

Last edited by air4time; 01-01-2013 at 11:44 PM.
 
Old 01-02-2013, 04:01 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
It all looks pretty good to me, although I don't know anything about isql.

The pattern you're using is called a here document. In bash, you can replace it with a herestring, and shorten things up. At least if it's just a single command.

Code:
isql -v test <<<"$CMD"

The big question though is whether the external file redirect is necessary. It appears that isql has a batch mode, for example. Can you use that to print out some kind of status string to stdout instead, perhaps?


Or does it issue a different exit code based on success or failure to connect? If so, you can skip the text capture and just test the command result directly. (Insert an "echo $?" after a command to see what its exit code is.)

If you do need to capture the stderr output like this, then we could still replace the file with a variable. Just redirect the command's stderr to stdout (rather than the whole script's) and capture that directly.

Code:
exit_txt=$( isql -v test <<<"$CMD" 2>&1 )

if [[ -n $exit_txt ]]; then
	....

It's recommended, BTW, to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression
http://mywiki.wooledge.org/ArithmeticExpression
 
Old 01-02-2013, 04:30 AM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
In some databases, isql is interactive sql and plain sql (without the i) is for command line.

Secondly, afaik, in my view, it is not certain that all sql/isql errors are returned in stderr since they are ulimately just message outputs to the sql command. An alternate way maybe to to redirect test.txt in the sql/isql statement thus
Quote:
isql -v test <<END_SCRIPT >> test.txt
Now after coming out of the sql herefile segment we should grep the output of test.txt for some know text that will occur only if the condition is successful. It depends on the specifics of the database.

OK

Last edited by AnanthaP; 01-05-2013 at 08:54 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does this work from the bash command line and then fails in a bash script? Rupadhya Linux - Newbie 5 09-26-2012 12:05 AM
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 09:46 AM
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 02:40 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration