8 Replies Latest reply on May 9, 2019 6:28 AM by Dude!

    parameter with several values

    happy10319

      Hi,

      I'm going to write a shell script. To which I will give a parameter:children; It has several values separated by blanck espace:

       

      myscripy.ksh -children child1 child2  child3

       

      How can I do this? How to read this parameter in my script?

       

      Thank you.

        • 1. Re: parameter with several values
          Dude!

          Arguments are stored in $1 - $9 and all are in $* as one string.

           

          For example:

           

          #/bin/ksh

          echo $1, $2, $3, $4, $5, $6, $7, $8, $9

          echo $*

           

          $ ./script a b c d e f g h i j k l m n o p q
          a, b, c, d, e, f, g, h, i
          a b c d e f g h i j k l m n o p q
          

           

          So what you probably want to do is to analyze $*.

           

          This can be quite demanding if you want do this properly, allowing parameters in various orders and fashion, allowing only certain combinations of parameters, applying defaults and catching user errors.

           

          Here's another simple example:

           

          #/bin/ksh

          case $* in

            -children*) shift

                        children=$* ;;

                     *) echo Missing -children parameter ;;

          esac

          echo the names are $children

           

          $ ./script -children peter paul mary
          the names are peter paul mary
          
          • 2. Re: parameter with several values
            happy10319

            Realy thanks.

            How can I separate the values and give them to several variables?

            var1=child1

            var2=child2

            Var3=

            .......

            .......

             

            I thought about:

            $ children="child1 child2"

            $ echo $children

            child1 child2

            $ echo $children | awk '{print $1}'

            child1

            $ echo $children | awk '{print $2}'

            child2

             

            Thanks.

            • 3. Re: parameter with several values
              Dude!

              If you don't know how many parameters there are, I guess the easiest will be to convert the string to an array.

               

              For example:

               

              #/bin/ksh

              case $* in

                -children*) shift

                            children=$* ;;

                         *) echo Missing -children parameter ;;

              esac

               

              children=( $children )

              echo Total number of parameter: ${#children[@]}

              echo ${children[0]}            # 1. parameter

              echo ${children[1]}            # 2. parameter

              echo ${children[2]}            # 3. parameter

              • 4. Re: parameter with several values
                happy10319

                Thank you.

                How can I transform the following into a for loop:

                echo ${children[0]}            # 1. parameter

                echo ${children[1]}            # 2. parameter

                echo ${children[2]}            # 3. parameter

                 

                Like this:

                for i in {1..${#children[@]}}

                   do

                      echo "val"$i:  ${children[$i]}

                   done

                 

                I have:

                ./run2.sh -children child1 child2

                Total number of parameter: 2

                child1

                child2

                 

                ++ for i in '{1..${#children[@]}}'

                ./run2.sh: line 27: {1..2}: syntax error: operand expected (error token is "{1..2}")

                 

                Thanks

                • 5. Re: parameter with several values
                  Gaz in Oz
                  $ more myscripy.ksh
                  case $* in
                    -children*) shift;
                                child_arr=($*);;
                              *) echo "Usage:";
                                echo "  $0 -children ...";;
                  esac
                  for i in "${child_arr[@]}"; do echo "$i"; done
                  $ ./myscripy.ksh -children x y z
                  x
                  y
                  z
                  $
                  
                  • 6. Re: parameter with several values
                    Dude!

                    You can use any space delimited string for that and not need to convert a variable into an array.

                     

                    For example

                     

                     

                    #/bin/ksh

                    case $* in

                      -children*) shift

                                  children=$* ;;

                               *) echo Missing -children parameter ;;

                    esac

                     

                    for child in $children; do

                       echo $child

                    done

                     

                    Its important however not to enclose $children in quotes.

                    • 7. Re: parameter with several values
                      happy10319

                      Thanks to all.

                      I also tried this

                      for ((i=0; i<${#children[@]}; i++));

                      do

                          echo var[$i]:::${children[i]};

                      done

                       

                      It works.

                      • 8. Re: parameter with several values
                        Dude!

                        for ((i=0; i<${#children[@]}; i++));

                        do

                            echo var[$i]:::${children[i]};

                        done

                        In the context of what this will accomplish I'd say it's a bit of a Rube Goldberg machine.

                         

                        echo var[$i] has no value and is not a dimensional array.