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

Thread: "Compile" script with command line

  1. #1
    Join Date
    Apr 2013
    Beans
    46

    "Compile" script with command line

    I have a bash script like this one

    Code:
    #!/bin/bash
    
    SERVER_ID=1
    SERVER_NAME="Brazil-1"
    SERVER_IP="123.213.123.123"
    SERVER_TYPE="Privado"
    
    // do stuff
    I want to run other script called make-config.sh, it should "compile" the first script variables (set them) like this command

    Code:
    make-config.sh -server_id 123 server_name "name" server_ip = "123.123.123.123" server_type = "private"
    The problem is that I have no idea of how do it, someone can share some tutorials about that? Thanks.

  2. #2
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: "Compile" script with command line

    Do you need to use long options, i.e. "make-config --serverid=xyz" or would short options suffice, e.g. "make-config -s xyz"?
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  3. #3
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: "Compile" script with command line

    You can use getopt and a here-document for it
    Code:
    cat <<END >~/compiled-script.bash
    #!/bin/bash
    
    SERVER_ID=$server_id
    SERVER_NAME=$server_name
    SERVER_IP=$server_ip
    SERVER_TYPE=$server_type
    END
    Last edited by schragge; April 13th, 2013 at 07:00 PM.

  4. #4
    Join Date
    Apr 2013
    Beans
    46

    Re: "Compile" script with command line

    Quote Originally Posted by r-senior View Post
    Do you need to use long options, i.e. "make-config --serverid=xyz" or would short options suffice, e.g. "make-config -s xyz"?
    Doesnt matter they size, I just want know how to do it.

    Quote Originally Posted by schragge View Post
    You can use getopt and a here-document for it
    Code:
    cat <<END >~/compiled-script.bash
    #!/bin/bash
    
    SERVER_ID=$server_id
    SERVER_NAME=$server_name
    SERVER_IP=$server_ip
    SERVER_TYPE=$server_type
    END
    For real, didnt get how to use it.

  5. #5
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: "Compile" script with command line

    If short options are OK, you can use the Bash getopts builtin:

    http://wiki.bash-hackers.org/howto/getopts_tutorial

    If you need long options, you can use the getopt program, as suggested by schragge. Here's an example: http://software.frodo.looijaard.name...opt-parse.bash

    I haven't checked that either of these examples work.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  6. #6
    Join Date
    Apr 2013
    Beans
    46

    Re: "Compile" script with command line

    Quote Originally Posted by r-senior View Post
    If short options are OK, you can use the Bash getopts builtin:

    http://wiki.bash-hackers.org/howto/getopts_tutorial

    If you need long options, you can use the getopt program, as suggested by schragge. Here's an example: http://software.frodo.looijaard.name...opt-parse.bash

    I haven't checked that either of these examples work.
    But, how to save them into compiled-bash.sh?

  7. #7
    Join Date
    Apr 2012
    Beans
    7,256

    Re: "Compile" script with command line

    You could have your getopt script create a file that exports the variables, and then source it from your other script, e.g.

    Code:
    $ server_id=1; server_type="Privado"
    $ 
    $ cat > servervars <<EOF
    export SERVER_ID="$server_id"
    export SERVER_TYPE="$server_type"
    EOF
    $ 
    $ echo $SERVER_ID $SERVER_TYPE
    
    $ 
    $ source servervars
    $ echo $SERVER_ID $SERVER_TYPE
    1 Privado
    $
    FWIW you should probably avoid using ALL_CAPS names though as these are traditionally reserved for system variables

  8. #8
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: "Compile" script with command line

    This is just a minimal example. Better check the manual page of getopt and study the example script linked by r-senior above to understand how getopt works.
    Code:
    script="compiled-bash.sh"
    temp=`getopt -l id:,name:,address:,type: -n "$0" -- '' "$@"` || exit 1
    eval set -- optname optval "$temp"
    declare -A opt
    while shift 2; do
      case $1 in
        --id|--name|--address|--type) opt[${1#--}]="$2";;
      esac
    done
    cat <<EOF >"$script"
    #!/bin/bash
    
    SERVER_ID=${opt[id]}
    SERVER_NAME=${opt[name]}
    SERVER_IP=${opt[address]}
    SERVER_TYPE=${opt[type]}
    EOF
    Last edited by schragge; April 14th, 2013 at 01:09 AM.

  9. #9
    Join Date
    Apr 2013
    Beans
    46

    Re: "Compile" script with command line

    Quote Originally Posted by schragge View Post
    This is just a minimal example. Better check the manual page of getopt and study the example script linked by r-senior above to understand how getopt works.
    Code:
    script="compiled-bash.sh"
    temp=`getopt -l id:,name:,address:,type: -n "$script" -- '' "$@"` || exit 1
    eval set -- optname optval "$temp"
    declare -A opt
    while shift 2; do
      case $1 in
        --id|--name|--address|--type) opt[${1#--}]="$2";;
      esac
    done
    cat <<EOF >"$script"
    #!/bin/bash
    
    SERVER_ID=${opt[id]}
    SERVER_NAME=${opt[name]}
    SERVER_IP=${opt[address]}
    SERVER_TYPE=${opt[type]}
    EOF
    I should call it using compiler.sh --id or --name? If the script already exists, it will override it?

  10. #10
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: "Compile" script with command line

    Yep. E.g.
    Code:
    ./compiler.sh --id=1 --name 'Brazil-1' --address 1.2.3.4 --type="Privado"

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
  •