Find the answer to your Linux question:
Results 1 to 2 of 2
I created a script to get an argument out of some string. This script will be used by other scripts to parse string. Code: #!/bin/bash if [ $# -lt 2 ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Posts
    1

    Bash - getarg.sh <arg#> <argument>

    I created a script to get an argument out of some string. This script will be used by other scripts to parse string.
    Code:
    #!/bin/bash
    
    if [ $# -lt 2 ]; then
      echo "Usage: $0 <argnum> <arg>"
      exit 1
    fi
    
    num=$1
    shift
    echo `eval echo '$'$num`
    Examples:
    $ ./getarg.sh 1 hi how are "you ?"
    hi

    $ ./getarg.sh 4 hi how are "you ?"
    you ?

    1) Is there away for me to do this without having to execute a script? i.e. using getopts or something similar?

    2) If not is this the best way of doing it?

    Thanks all !

  2. #2
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    im not sure that i understood the question but if what you mean is
    1) instead of calling this script in the final script can I parse the arguments directly in the final script?

    then the answer is yes. you can use getopts to do this . but in that case you will need to introduce options like you see them in every Unix-like tool( for example "top -n 1").

    getopts is explained here
    getopts


    sorry if i misunderstood your question
    Linux and me it's a love story

Posting Permissions

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