Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2008
    Posts
    13

    Unanswered: How to check if database exists via script.

    Hi folks!

    First off I'm working with ASE 12.5.3.

    I'm using you're basic ISQL command in a shell script to connect to my DB...

    isql -S$DB_SERVER -D$DB_NAME -U$DB_USR -P$DB_PWD <<!EOF > $log_file

    My question is, is there a way to determine if a database exists using a UNIX shell script?

    For example, if somehow I can check if a database called DB_Test1 exists do the following...

    isql -S$DB_SERVER -D$DB_Test1 -U$DB_USR -P$DB_PWD <<!EOF > $log_file

    else do the following...

    isql -S$DB_SERVER -D$DB_Test2 -U$DB_USR -P$DB_PWD <<!EOF > $log_file

    Thanks for any help!

  2. #2
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    How come you don't know what database is on your server?
    e.g. in ksh
    Code:
    isql -S$DB_SERVER -U$DB_USR -P$DB_PWD <<EOF | grep "$DB_NAME"
    sp_helpdb
    go
    EOF
    if [ $? -eq 0 ] 
    then
      echo 'db found'
      : do some stuff
    else
      echo 'db not found'
      : do some other stuff
    fi

  3. #3
    Join Date
    Sep 2008
    Posts
    13
    Thanks for the reply! I did a quick test and so far your solution looks like it will work for me.

    Well I do know which database are on our servers, but the thing is that my script will run on several servers and not all of our servers have the same databases. So I'm trying to make my script as generic or adaptable as possible.

    Thanks for your help!

  4. #4
    Join Date
    Sep 2003
    Location
    Switzerland
    Posts
    443
    Since you mentioned shell script its a bit easy. There might be other ways to do it, like do all processing in a stored proc.

    #!/bin/sh

    isql ..... -b -o /tmp/myserver.dbs << EOF
    set nocount on
    go
    select name from sysdatabases
    go
    EOF

    #-b doesnt display any header

    for i in `cat /tmp/myserver.dbs`
    do
    isql ..... << EOF
    sp_helpdb $i
    go
    EOF
    done

  5. #5
    Join Date
    Sep 2003
    Location
    Switzerland
    Posts
    443
    Sorry just read your original question... and my answer doesnt solve it... but I guess you get the picture and use some of the logic.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •