Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read file - assign to shell vars 1

Status
Not open for further replies.

ksbrace

Programmer
May 13, 2000
501
US
Hello,
I have been working on some shell scripts that have a lot of interaction and typing from the user. So, when I test any change, I have to type in about 20 values for variables. And when it goes into actual use, we have about 100 libraries that it will need to run against. So, that is a lot of chance for human error if I leave it as is.

I would like to be able to set up a 'properties'/'config' file that the shell script will read a file and assign the values to the shell vars. I have included one of the interaction files that I'm working with. Thanks in advance for any help.

Code:
#!/usr/bin/bash


#############################################################
#Function aleph_start_menu
#Menu to setup variables for aleph_start file
#############################################################
aleph_start_menu () {
CHOICE="0"
while [ "$CHOICE" = "0" ]
 do
   clear
   echo "Please define variables for aleph_start."
   echo
   echo "Which variable would you like to modify?"
   echo "1.) ORA_HOST: $ORA_HOST"
   echo "2.) [URL unfurl="true"]WWW_HOST:[/URL] $[URL unfurl="true"]WWW_HOST"[/URL]
   echo "3.) Z39_HOST: $Z39_HOST"
   echo "4.) ORACLE_SID: $ORACLE_SID"
   echo "5.) HTTPD_PORT: $HTTPD_PORT"
   echo "6.) [URL unfurl="true"]WWW_SERVER_PORT:[/URL] $[URL unfurl="true"]WWW_SERVER_PORT"[/URL]
   echo "7.) PC_SERVER_PORT: $PC_SERVER_PORT"
   echo "8.) OCLC_SERVER_PORT: $OCLC_SERVER_PORT"
   echo "9.) Z39_SERVER_PORT: $Z39_SERVER_PORT"
   echo "10.) Z39_GATE_PORT: $Z39_GATE_PORT"
   echo "11.) SIP2_SERVER_PORT: $SIP2_SERVER_PORT"
   echo "12.) SLNP_SERVER_PORT: $SLNP_SERVER_PORT"   
   echo "13.) NCIP_SERVER_PORT: $NCIP_SERVER_PORT"
   echo "14.) SERVER_NAME: $SERVER_NAME"
   echo "15.) SERVER_URL: $SERVER_URL"
   echo "m.) Return to main menu."
   echo "q.) To quit script."
   echo "--------------------------"
   echo -n "Choice: "
   read CHOICE
   case "$CHOICE" in
     1) echo -n "Change ORA_HOST: "
        read ORA_HOST
        CHOICE="0"
        ;;
     2) echo -n "Change [URL unfurl="true"]WWW_HOST:[/URL] "
        read [URL unfurl="true"]WWW_HOST[/URL] 
        CHOICE="0"
        ;;
     3) echo -n "Change Z39_HOST: "
        read Z39_HOST 
        CHOICE="0"
        ;;
     4) echo -n "Change ORACLE_SID: "
        read ORACLE_SID
        CHOICE="0"
        ;;
     5) echo -n "Change HTTPD_PORT: "
        read HTTPD_PORT
        CHOICE="0"
        ;;
     6) echo -n "Change [URL unfurl="true"]WWW_SERVER_PORT:[/URL] "
        read [URL unfurl="true"]WWW_SERVER_PORT[/URL]
        CHOICE="0"
        ;;
     7) echo -n "Change PC_SERVER_PORT: "
        read PC_SERVER_PORT
        CHOICE="0"
        ;;
     8) echo -n "Change OCLC_SERVER_PORT: "
        read OCLC_SERVER_PORT
        CHOICE="0"
        ;;
     9) echo -n "Change Z39_SERVER_PORT: "
        read Z39_SERVER_PORT
        CHOICE="0"
        ;;
     10) echo -n "Change Z39_GATE_PORT: "
        read Z39_GATE_PORT
        CHOICE="0"
        ;;
     11) echo -n "Change SIP2_SERVER_PORT: "
        read SIP2_SERVER_PORT
        CHOICE="0"
        ;;
     12) echo -n "Change SLNP_SERVER_PORT: "
        read SLNP_SERVER_PORT
        CHOICE="0"
        ;;
     13) echo -n "Change NCIP_SERVER_PORT: "
        read NCIP_SERVER_PORT
        CHOICE="0"
        ;;
     14) echo -n "Change SERVER_NAME: "
        read SERVER_NAME
        CHOICE="0"
        ;;
     15) echo -n "Change SERVER_URL: "
        read SERVER_URL
        CHOICE="0"
        ;;
     m) main_menu
        ;;
     q) echo "Good-bye!"
        exit 1
        ;;
     *) echo "Invalid choice!"
        CHOICE="0"
       ;;
    esac
done
 
Hi

The script :
Code:
[gray]# ...[/gray]
while [ "$CHOICE" = "0" ]
 do
   clear
   echo "Please define variables for aleph_start."
   echo
   echo "Which variable would you like to modify?"
   echo "1.) ORA_HOST: $ORA_HOST"
[gray]# ...[/gray]
[red]   echo "l.) Load settings from file"[/red]
   echo "q.) To quit script."
   echo "--------------------------"
   echo -n "Choice: "
   read CHOICE
   case "$CHOICE" in
     1) echo -n "Change ORA_HOST: "
        read ORA_HOST
        CHOICE="0"
        ;;
[gray]# ...[/gray]
[red]     l) read -p "File name : " filename
        source "$filename"
        ;;[/red]
     q) echo "Good-bye!"
        exit 1
        ;;
     *) echo "Invalid choice!"
        CHOICE="0"
       ;;
    esac
done
The settings file :
Code:
ORA_HOST="theoraclehostname"
[URL unfurl="true"]WWW_HOST="thewebhostname"[/URL]
Z39_HOST="noideawhatkindofhostname"
[gray]# ...[/gray]
SERVER_URL="uniformresourcelocator"

Feherke.
 
Ok, I made the change, but I'm getting an unexpected end of file on line 112. Funny thing, there is only 111 lines in the file. I incorporated your change into the file, so maybe that is not allowed. I also tried the settigns file with and without quotes for the var values. Thanks in advance!

script:
Code:
#!/usr/bin/bash


#############################################################
#Function aleph_start_menu
#Menu to setup variables for aleph_start file
#############################################################
aleph_start_menu () {
CHOICE="0"
while [ "$CHOICE" = "0" ]
 do
   clear
   echo "Please define variables for aleph_start."
   echo
   echo "Which variable would you like to modify?"
   echo "l.) Load settings from file"
   echo "2.) ORA_HOST: $ORA_HOST"
   echo "3.) [URL unfurl="true"]WWW_HOST:[/URL] $[URL unfurl="true"]WWW_HOST"[/URL]
   echo "4.) Z39_HOST: $Z39_HOST"
   echo "5.) ORACLE_SID: $ORACLE_SID"
   echo "6.) HTTPD_PORT: $HTTPD_PORT"
   echo "7.) [URL unfurl="true"]WWW_SERVER_PORT:[/URL] $[URL unfurl="true"]WWW_SERVER_PORT"[/URL]
   echo "8.) PC_SERVER_PORT: $PC_SERVER_PORT"
   echo "9.) OCLC_SERVER_PORT: $OCLC_SERVER_PORT"
   echo "10.) Z39_SERVER_PORT: $Z39_SERVER_PORT"
   echo "11.) Z39_GATE_PORT: $Z39_GATE_PORT"
   echo "12.) SIP2_SERVER_PORT: $SIP2_SERVER_PORT"
   echo "13.) SLNP_SERVER_PORT: $SLNP_SERVER_PORT"   
   echo "14.) NCIP_SERVER_PORT: $NCIP_SERVER_PORT"
   echo "15.) SERVER_NAME: $SERVER_NAME"
   echo "16.) SERVER_URL: $SERVER_URL"
   echo "m.) Return to main menu."
   echo "q.) To quit script."
   echo "--------------------------"
   echo -n "Choice: "
   read CHOICE
   case "$CHOICE" in
     l) read -p "File name : " filename
        CHOICE="0"
        ;;
     2) echo -n "Change ORA_HOST: "
        read ORA_HOST
        CHOICE="0"
        ;;
     3) echo -n "Change [URL unfurl="true"]WWW_HOST:[/URL] "
        read [URL unfurl="true"]WWW_HOST[/URL] 
        CHOICE="0"
        ;;
     4) echo -n "Change Z39_HOST: "
        read Z39_HOST 
        CHOICE="0"
        ;;
     5) echo -n "Change ORACLE_SID: "
        read ORACLE_SID
        CHOICE="0"
        ;;
     6) echo -n "Change HTTPD_PORT: "
        read HTTPD_PORT
        CHOICE="0"
        ;;
     7) echo -n "Change [URL unfurl="true"]WWW_SERVER_PORT:[/URL] "
        read [URL unfurl="true"]WWW_SERVER_PORT[/URL]
        CHOICE="0"
        ;;
     8) echo -n "Change PC_SERVER_PORT: "
        read PC_SERVER_PORT
        CHOICE="0"
        ;;
     9) echo -n "Change OCLC_SERVER_PORT: "
        read OCLC_SERVER_PORT
        CHOICE="0"
        ;;
     10) echo -n "Change Z39_SERVER_PORT: "
        read Z39_SERVER_PORT
        CHOICE="0"
        ;;
     11) echo -n "Change Z39_GATE_PORT: "
        read Z39_GATE_PORT
        CHOICE="0"
        ;;
     12) echo -n "Change SIP2_SERVER_PORT: "
        read SIP2_SERVER_PORT
        CHOICE="0"
        ;;
     13) echo -n "Change SLNP_SERVER_PORT: "
        read SLNP_SERVER_PORT
        CHOICE="0"
        ;;
     14) echo -n "Change NCIP_SERVER_PORT: "
        read NCIP_SERVER_PORT
        CHOICE="0"
        ;;
     15) echo -n "Change SERVER_NAME: "
        read SERVER_NAME
        CHOICE="0"
        ;;
     16) echo -n "Change SERVER_URL: "
        read SERVER_URL
        CHOICE="0"
        ;;
     m) main_menu
        ;;
     q) echo "Good-bye!"
        exit 1
        ;;
     *) echo "Invalid choice!"
        CHOICE="0"
       ;;
    esac
done

Settings file:
Code:
ORA_HOST="xxx.xxx.xxx.xxx"
[URL unfurl="true"]WWW_HOST="xxx.xxx.xxx.xxx"[/URL]
Z39_HOST="xxx"
ORACLE_SID="aleph00"
HTTPD_PORT="5000"
[URL unfurl="true"]WWW_SERVER_PORT="6000"[/URL]
PC_SERVER_PORT="7000"
OCLC_SERVER_PORT="8000"
Z39_SERVER_PORT="9000"
Z39_GATE_PORT="1000"
SIP2_PORT="1100"
SLNP_SERVER_PORT="1200"
NCIP_SERVER_PORT="1300"
SERVER_NAME="xxx"
SERVER_URL="xxx.xxx.xxx.xxx"
 
I just realized I posted the wrong file:

it should read:
Code:
 l) echo -n "Please give the filename: "
        read filename
        read -p "File name : " filename
        source $filename        
        CHOICE="0"

my apologies.

 
Hi

Sorry, I can not try it right now. But there is a strange thing : the [tt]function[/tt] aleph_start_menu() misses its closing brace ( } ). In your initial post too. Please check what happend with it.

Feherke.
 
Doh! that solved that error. Thanks for the good eyes.

 
Hi

Just one more word. Setting the $CHOICE variable back to 0 in each brach of the [tt]case[/tt] looks abit ugly. I prefer this way :
[ul]
[li]put all that interactive thing in an endless [tt]while[/tt] loop[/li]
[li]exit the loop with [tt]exit[/tt] if is to stop the script[/li]
[li]exit the loop with [tt]break[/tt] if more processing is required after the loop[/li]
[/ul]
Code:
#! /bin/bash

while :; do
  cat <<ENDOFTEXT
  1) first operand : $first
  2) second operand : $second
  p) process
  q) quit
ENDOFTEXT
  read -p "Your choice : " choice
  case "$choice" in
    1) read -p "First operand : " first ;;
    2) read -p "Second operand : " second ;;
    p) break ;;
    q) exit ;;
    *) echo "Wrong choice !" ;;
  esac
done

echo "Processing data..."
echo "$first + $second = $((first+second))"

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top