Results 1 to 4 of 4

Thread: Howto: Set parameter to a shell script when executing

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default Howto: Set parameter to a shell script when executing

    hi everyone ,

    i want to set parameter to a script when im executing it.

    for ex ;

    sh abc.sh -parameter1=palsec -parameter2= secpal -parameter3=lapsec

    how can i do this ?? pls help.

    Im using $1 $2 $3 in my script but it works but this is dummy solution i think.

    thanks a a lot.


    ex1:

    ----- OK----------------------

    sh abc.sh palsec secpal lapsec

    echo $1 $2 $3

    -----OK-----------------------


    ex2:

    -----------HELP -----------
    sh abc.sh -parameter1=palsec -parameter2= secpal -parameter3=lapsec

    ?????
    ?????
    ?????
    -----------HELP -----------

  2. #2
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,500
    Thanks
    17
    Thanked 789 Times in 507 Posts
    Rep Power
    10

    Default

    You need to use the getopts to parse positional parameters. For example:

    Code:
    ### Evaluate the options passed on the command line
    while getopts ae:f:hd:s:qx: option
    do
            case "${option}"
            in
                    a) ALARM="TRUE";;
                    e) ADMIN=${OPTARG};;
                    d) DOMAIN=${OPTARG};;
                    f) SERVERFILE=$OPTARG;;
                    s) WHOIS_SERVER=$OPTARG;;
                    q) QUIET="TRUE";;
                    x) WARNDAYS=$OPTARG;;
                    \?) usage
                        exit 1;;
            esac
    done
    See the following script for complete example:

    Last edited by nixcraft; 26th October 2011 at 06:34 PM.
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Quote Originally Posted by nixcraft View Post
    You need to use the getopts to parse positional parameters. For example:

    Code:
    ### Evaluate the options passed on the command line while getopts ae:f:hd:s:qx: option do         case "${option}"         in                 a) ALARM="TRUE";;                 e) ADMIN=${OPTARG};;                 d) DOMAIN=${OPTARG};;                 f) SERVERFILE=$OPTARG;;                 s) WHOIS_SERVER=$OPTARG;;                 q) QUIET="TRUE";;                 x) WARNDAYS=$OPTARG;;                 \?) usage                     exit 1;;         esac done
    See the following script for complete example:

    sorry

    icant understand.

    can you give me a short example like i did.
    thanks a lot.

  4. #4
    Senior Member
    Join Date
    Jul 2009
    Posts
    138
    Thanks
    67
    Thanked 4 Times in 3 Posts
    Rep Power
    5

    Default

    while and shift comamnd
    Code:
    while [ $# -gt 0 ] 
    do 
    case "$1" in 
    	-a) options="$options $1";; 
    	-f) options="$options $1" argfile="$2" shift ;; 
    	*) files="$files $1";; 
    esac 
    shift 
    done
    English not my first language. If not understood my question ask me.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Shell Script Error Pseudo-terminal using parameter -t
    By facundo82 in forum Shell scripting
    Replies: 4
    Last Post: 28th July 2011, 02:32 AM
  2. Replies: 2
    Last Post: 5th September 2010, 11:50 PM
  3. HowTo: Compile a shell script
    By opnine in forum Shell scripting
    Replies: 1
    Last Post: 8th August 2010, 07:11 AM
  4. Problem executing script through at
    By viktor4124 in forum Shell scripting
    Replies: 1
    Last Post: 7th April 2009, 12:09 AM
  5. How to pass a parameter to a shell script
    By flyingrat in forum Shell scripting
    Replies: 3
    Last Post: 24th July 2008, 02:01 PM

Tags for this Thread

Posting Permissions

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