Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Bash Scripting Help, how to use external file for variables

  1. #1
    Join Date
    Jun 2007
    Beans
    182
    Distro
    Ubuntu 14.04 Trusty Tahr

    Bash Scripting Help, how to use external file for variables

    This probably isn't the correct forum for this but these Forums have a lot of knowledge so I thought I would give this a shot.

    I'm working on a bash script for a linux thin client where the script will check if the Thin Client is connected to the Virtual Machine. If it's not the script will reconnect to the VM. I know I can load up the script (like I've done so far) with the various IP addresses. What I'd like to do instead is list my variables of Hostname and their IP Address in a config file and then have my script check the hostname of the current computer, and find the hostname in the config file and use the IP Address. So far I've got it working where it reads the hostname and finds the IP to use based on the case statement without using a config file. This is what I have so far.


    Code:
    #!/bin/sh
    # Check if rdesktop is running
    LOGFILE=/home/rpitc/scripts/check.log
    read var < ipfile
    THISHOST=$(hostname)
    if pgrep rdesktop > /dev/null
    then
            echo "$(date "+%m%d%Y %T") : rdesktop is already running." >> $LOGFILE$
            exit 0
    else
            echo "$(date "+%m%d%Y %T") : rdesktop has Stopped. Attempting to resta$
            case $THISHOST in
              (rpiA) rdesktop -z -u user -p password 10.100.100.10;;
              (rpiB) rdesktop -z -u user -p password 10.100.100.11;;
              (rpiC) rdesktop -z -u user -p password 10.100.100.12;;
            esac
            exit 0
    fi

    Right now my code is not using the line: read var < ipfile

    So I would like list all my server IP's in the ipfile like this:


    Code:
    rpiA="10.100.100.10"
    rpiB="10.100.100.11"
    rpiC="10.100.100.12"

    And then modify my code to reference the variables in ipfile in my script. I'm assuming I wouldn't use a case statement any longer, probably a for loop.

    Any assistance you can give me on how best to accomplish this task would be greatly appreciated.

    Thank you.

  2. #2
    Join Date
    May 2012
    Beans
    248
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Bash Scripting Help, how to use external file for variables

    I haven't scripted for years but something like this might work:

    If your ipfile was a CSV (Comma Separated Value) file like:

    rpiA,10.100.100.10
    rpiB,10.100.100.11
    rpiC,10.100.100.12

    Test script:

    #!/bin/bash
    while IFS="," read host ip
    do
    echo "Host: $host has address: $ip"
    done < ipfile

    Output would be:

    Host: rpiA has address: 10.100.100.11
    Host: rpiB has address: 10.100.100.12
    Host: rpiC has address: 10.100.100.13

    IFS means Internal Field Separator, in this case a comma. Choose your own variable names after the 'read' command.

    Obviously in the 'while' loop you would do your testing.

    It would be interesting if this is of any help to you.

  3. #3
    Join Date
    Oct 2010
    Location
    Above ground...(for now)
    Beans
    761

    Re: Bash Scripting Help, how to use external file for variables

    You have this:
    Code:
    else
            echo "$(date "+%m%d%Y %T") : rdesktop has Stopped. Attempting to resta$
            case $THISHOST in
              (rpiA) rdesktop -z -u user -p password 10.100.100.10;;
              (rpiB) rdesktop -z -u user -p password 10.100.100.11;;
              (rpiC) rdesktop -z -u user -p password 10.100.100.12;;
            esac
            exit 0
    fi
    You could replace the red lines above with the blue lines below as follows:
    Code:
    else
            echo "$(date "+%m%d%Y %T") : rdesktop has Stopped. Attempting to resta$
            ip_string=$(grep $THISHOST ipfile)
            ip_addr=${ip_string#* }
            rdesktop -z -u user -p password ${ip_addr};;
            exit 0
    fi
    In order for this to work, your ipfile must be formatted like this:
    Code:
    rpiA 10.100.100.10
    rpiB 10.100.100.11
    rpiC 10.100.100.12
    NOTE: All bash global environment variables are named using ALL UPPERCASE CHARACTERS. In order to prevent modifying the value of one or more of these important variables unintentionally, local variables within scripts should not be named using all uppercase characters. For example: THISHOST should be named ThisHost, thishost, this_host, or something similar, likewise for: LOGFILE.
    Have Fun,

    Crusty
    Last edited by Crusty Old Fart; September 10th, 2013 at 09:45 PM. Reason: Added note regarding global environment variables.
    Suffering from severe chronic female supervision deficiency syndrome resulting in
    an increasing intolerance of adolescent scatological effluence and PMS induced nefarious diatribe.
    How to: Mark your thread as: [SOLVED]

  4. #4
    Join Date
    Oct 2009
    Location
    Reykjavík, Ísland
    Beans
    13,647
    Distro
    Xubuntu

    Re: Bash Scripting Help, how to use external file for variables

    Quote Originally Posted by greavette View Post
    This probably isn't the correct forum for this...
    ... so moved to Programming Talk.
    Bringing old hardware back to life. About problems due to upgrading.
    Please visit Quick Links -> Unanswered Posts.
    Don't use this space for a list of your hardware. It only creates false hits in the search engines.

  5. #5
    Join Date
    Jul 2009
    Beans
    516
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Bash Scripting Help, how to use external file for variables

    With a config file containing something like
    Code:
    MY_HOST="1.2.3.4"
    you can then use the following to get the variable if the output from "hostname -s" was "my-host".
    Code:
    #!/bin/bash
    
    [ -f config ] && . ./config
    
    # need to convert - to _ as you can't have it in variable names
    THISHOST=$(hostname -s | sed -e 's/-/_/g' | tr '[:lower:]' '[:upper:]')
    
    if [[ -n ${!THISHOST} ]]; then
      echo ${!THISHOST}
    fi
    It uses bash indirect variables.
    Mark your thread as [SOLVED], use Thread Tools on forum page.

  6. #6
    Join Date
    Jun 2007
    Beans
    182
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Bash Scripting Help, how to use external file for variables

    Hello,

    Thank you all for the replies. I've successfully been able to connect to the virtual machine of my choice using the code:

    Code:
    else
            echo "$(date "+%m%d%Y %T") : rdesktop has Stopped. Attempting to resta$
            ip_string=$(grep $THISHOST ipfile)
            ip_addr=${ip_string#* }
            rdesktop -z -u user -p password ${ip_addr};;
            exit 0
    fi
    I will test the other suggestions and reply back with my results as well.

    But I do have a further question for this forum. What if I wanted to add in more variables, like the Userid. How would I change this bash script to read in two variables to assign and IP Address and Userid to the rdesktop instruction?

    And what does my ipfile need to look like to set these two variables by hostname?

    Thank you.

  7. #7
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Bash Scripting Help, how to use external file for variables

    I'd go with something like this, using sourcing. Each server cfg is in a separate file, script finds all cfgs and creates convenient menu for bonus points.

    cfg.sh:
    Code:
    #!/bin/bash
    
    cfg_files=( *.cfg )  # create array of all *.cfg files
    select h in "${cfg_files[@]%.cfg}"; do break; done  # menu consists of cfg names with extension stripped
    source "$h.cfg"   # load variables from selected cfg file
    
    echo "${user}:${password}@${host}"
    server1.cfg
    Code:
    host=heaven
    user=god
    password=amen
    server2.cfg
    Code:
    host=rome
    user=caesar
    password=ave
    how it works
    Code:
    $ ./cfg.sh 
    1) server1
    2) server2
    #? 1
    god:amen@heaven
    $ ./cfg.sh 
    1) server1
    2) server2
    #? 2
    caesar:ave@rome
    and if you want to stay with a single flat file that looks like this
    Code:
    heaven god amen
    rome caesar ave
    Code:
    $ read h; read -r host user pw < <( grep "$h" servers.txt ); echo "$host / $user / $pw"
    rome
    rome / caesar / ave
    Last edited by Vaphell; September 21st, 2013 at 05:27 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  8. #8
    Join Date
    Oct 2010
    Location
    Above ground...(for now)
    Beans
    761

    Re: Bash Scripting Help, how to use external file for variables

    Hmmm...Since this thread has become somewhat of a contest to see how many different ways we can write this script, I thought this was a good time bring in some awkological awkumen...sorry, couldn't resist. <-- Edit: Pretty bad idea on my part. I should have paid more attention to what Vaphell was doing with the read command at the bottom of his post directly above.

    Also, in this post, pursuant to good programming practices, I'm changing the variable name: THISHOST to: ThisHost...couldn't resist that either.
    Quote Originally Posted by greavette View Post
    ...What if I wanted to add in more variables, like the Userid. How would I change this bash script to read in two variables to assign and IP Address and Userid to the rdesktop instruction?
    Code:
    else
            echo "$(date "+%m%d%Y %T") : rdesktop has Stopped. Attempting to resta$
            ip_string=$(grep $THISHOST ipfile)
            ip_addr=${ip_string#* }
            rdesktop -z -u user -p password ${ip_addr};;
            exit 0
    fi
    You could replace the red lines in the code box above, with the blue lines in the code box below.
    The green lines are included for your testing enjoyment.
    Code:
    else
            echo "$(date "+%m%d%Y %T") : rdesktop has Stopped. Attempting to resta$
            string=$(grep $ThisHost ipfile)
            host=$(echo $string | awk  '{ print $1 }')
            ip_addr=$(echo $string | awk '{ print $2 }')
            userid=$(echo $string | awk '{ print $3 }')
            var_1=$(echo $string | awk '{ print $4 }')
            var_2=$(echo $string | awk '{ print $5 }') <-- Edit replace all the struck out lines above with the following single line. Hat tip to Vaphell for setting me straight here.
            read -r host ip_addr userid var_1 var_2 < <( grep "$ThisHost" ipfile )
    
    # ~~~ And now, for your testing pleasure: ~~~
            echo "Host = $host"
            echo "IP Addr = $ip_addr"
            echo "User ID = $userid"
            echo "Variable One = $var_1"
            echo "Variable Two = $var_2"
    
            rdesktop -z -u user -p password ${ip_addr};;
            exit 0
    fi

    Quote Originally Posted by greavette View Post
    ...And what does my ipfile need to look like to set these two variables by hostname?

    Thank you.
    It needs to look something like this:
    Code:
    rpiA 10.100.100.10 UseridA VariableOneA VariableTwoA
    rpiB 10.100.100.11 UseridB VariableOneB VariableTwoB
    rpiC 10.100.100.12 UseridC VariableOneC VariableTwoC
    ...and you're mighty welcome.
    Quote Originally Posted by Vaphell View Post
    I'd go with something like this, using sourcing. Each server cfg is in a separate file, script finds all cfgs and creates convenient menu for bonus points...
    Pretty sexy, Vap.
    You never disappoint, Sir.
    Last edited by Crusty Old Fart; September 22nd, 2013 at 05:24 AM.
    Suffering from severe chronic female supervision deficiency syndrome resulting in
    an increasing intolerance of adolescent scatological effluence and PMS induced nefarious diatribe.
    How to: Mark your thread as: [SOLVED]

  9. #9
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Bash Scripting Help, how to use external file for variables

    thanks

    as I mentioned earlier read is all you need.
    wasteful this
    Code:
    string=$(grep $ThisHost ipfile)
    host=$(echo $string | awk  '{ print $1 }')
    ip_addr=$(echo $string | awk '{ print $2 }')
    userid=$(echo $string | awk '{ print $3 }')
    var_1=$(echo $string | awk '{ print $4 }')
    var_2=$(echo $string | awk '{ print $5 }')
    becomes this
    Code:
    $ choice=rpiB
    $ read -r host ip user var1 var2 < <( grep "$choice" servers.txt )
    $ echo [$host][$ip][$user][$var1][$var2]
    [rpiB][10.100.100.11][UseridB][VariableOneB][VariableTwoB]
    Last edited by Vaphell; September 22nd, 2013 at 02:26 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  10. #10
    Join Date
    Oct 2010
    Location
    Above ground...(for now)
    Beans
    761

    Re: Bash Scripting Help, how to use external file for variables

    Quote Originally Posted by Vaphell View Post
    thanks
    as I mentioned earlier read is all you need.
    wasteful this...
    As I was preparing my last post, my inner child had this funny feeling that you would show up and change his diaper.
    Thanks.

    Crusty
    Last edited by Crusty Old Fart; September 22nd, 2013 at 06:22 AM.
    Suffering from severe chronic female supervision deficiency syndrome resulting in
    an increasing intolerance of adolescent scatological effluence and PMS induced nefarious diatribe.
    How to: Mark your thread as: [SOLVED]

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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