Results 1 to 4 of 4

Thread: Passing arguments to Shell script from text file

  1. #1
    Join Date
    Dec 2008
    Beans
    10

    Lightbulb Passing arguments to Shell script from text file

    Hello All,

    I hope you are all well and I'm really sorry to post this, I know that similar things have been posted before but I am really struggling to get my head around it.

    Essentially, I want to feed in selective arguments from a configuration file to my shell script. As an example my config file is as follows:

    Code:
                                                     
    #Randomcheck here
    randvariable=0
    
    #Cut off
    Cutoff=10
    How would I write a script in bash to only take the value of Cutoff and ignore Randvariable? Please please please write it as if you are speaking to a 5 year old

    Best wishes and thanks,

    Lewis

  2. #2
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Passing arguments to Shell script from text file

    Code:
    $ grep "Cutoff" /tmp/t | sed 's/^.*=//g'
    But really if you can be certain all the non-comment lines are valid bash, you can use
    Code:
    source /tmp/t
    to get the values loaded and use them within the script. If you don't want a value, ignore it or "unset" it. I'd use the source method myself. It is a commonly used method to share settings for a group of scripts that need different values for different systems.
    Last edited by TheFu; September 22nd, 2018 at 12:53 AM. Reason: added source method.

  3. #3
    Join Date
    Nov 2008
    Location
    Boston MetroWest
    Beans
    16,326

    Re: Passing arguments to Shell script from text file

    If you just want the value, 10, I'd modify TheFu's script so that the result of the command is stored in an environment variable:

    Code:
    CUTOFF=$(grep "Cutoff" /tmp/t | sed 's/^.*=//g')
    Encasing a command in $() returns the value of the command which is then stored in the environment variable CUTOFF. You can then use $CUTOFF in the calling script where you want the value "10" to be used.
    If you ask for help, do not abandon your request. Please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

  4. #4
    Join Date
    Dec 2008
    Beans
    10

    Re: Passing arguments to Shell script from text file

    Thank you both for your responses I am very very grateful for your help.

    thanks,

    Lewis

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
  •