Shell script to execute sybase system procedures

March 27, 2012 at 03:50:55
Specs: CentOS
I'm writing a shell script using Bourne shell (sh) to connect to sybase and execute some system procedures based on the data given by the user.

However, i want my script to first check if the login id specified by the user exists before i start executing other system procedures to edit or remove some of the user's details.

I want my script to do the following, after connecting to sybase.

1. Execute a system procedure "sp_displaylogin" to check if the user's login exists.
If it exists, then i have to redirect it to a function that performs the rest of the execution.

2. If the login does not exist, then i have to execute a system procedure "sp_addlogin" to create the login.

Now comes the big problem.

I want to know how i can read the results of "sp_displaylogin" so that i can determine if the login id exists or not, and redirect my execution acordingly.

How do i do it?

Please help.

See More: Shell script to execute sybase system procedures

Report •

March 27, 2012 at 07:27:23
Most databases have a utility to access the database: oracle uses sqlplus and sybase uses isql.

I am not a sybase user, but this article from the defunct Sys Admin magazine might give you some ideas how to access the database:

Also, I recently answered a question about trapping the output from an oracle script:

You might be able to modify it to access sybase.

Report •

March 28, 2012 at 00:02:38

I get the idea of how it's done.

I just have to assign the value of the entire isql statement and query to a variable and then I can cross check the value of the variable with my expected values and try to find a match.

Is that how it's done?

In that case, my code will be something like this

chk_val=$(isql -Uuser -Ppassword -Sserver <<EOF
sp_displaylogin $loginid

my_val="Login id does not exist"

if [ $chk_val = $my_val ]
# call function to create login id and then edit details
# call function to only edit details

There might be some errors in this code as it is still untested.
But is my logic right?

Please help.

Report •

March 28, 2012 at 06:55:38

Your logic looks good and reasonable to me.

I will point out that since you are using the Bourne shell, this will fail:

chk_val=$(isql -Uuser -Ppassword -Sserver <<EOF
sp_displaylogin $loginid

The above statement uses the ksh/bash form of command line substitution $(..)

For Bourne, use back tics for command line substitution:

chk_val=`isql -Uuser -Ppassword -Sserver <<EOF
sp_displaylogin $loginid

That character is a back tic - not a single quote.

Report •

Related Solutions

March 28, 2012 at 23:44:21

Thanks a lot for pointing out that error.
But I'm still unable to test your code as I'm experiencing some problems with my Sybase server.

I'll get it up and running in a few days and then I'll let you know how well your code works.

I have one more doubt.

You told me that my code would fail since I'm using the ksh/bash form of command line substitution.

Suppose my default shell is bash, but i'm saving this file with .sh extension (, will this forrm of substitution work, since my default shell is bash ?

Or do i need to save my file with .bash extension for this type of substitution to work?

I hope i'm not confusing you.

P. S : Thanks again !

Report •

March 29, 2012 at 07:23:45
You are welcome.

What file extension you use has nothing at all to do with what shell the script uses, but what shell is invoked in the script. You said that you were using Bourne - #!/bin/sh

If you don't include the shell invocation, then the default shell is used. It's always a good idea to include what shell to use.

BTW, the shell invocation must be on line 1, column 1; if it is not, it is just treated as a comment in the code. Don't try to place any comments above it.

Personally, if the bash shell is installed, I would use it before using the bourne shell.

In closing, my personal preference is to use a file extension of 'ss' for all my shell scripts no matter what shell the script uses.

Report •

March 30, 2012 at 00:01:50

Thanks for the heads up. I actually meant the shell invocation only, but since i save each file with the same extension as that of the shell i'm invoking in the script, I got the both of them mixed up.

i.e., if i'm invoking #!/bin/bash, then i save my file as filename.bash
and if it's #!/bin/sh, then i save it as, and so on.

Anyway, I now have a better understanding of this issue, thanks to you !

Report •

Ask Question