4 Replies Latest reply on May 9, 2019 6:12 AM by Dude!

    Create variable

    happy10319

      Hi,

      In following code I create  variables (var1, var2 ...)

       

      $ more run3.sh

      #/bin/ksh

      ls=( val1 val2 val3 )

      echo ${ls[@]}

       

      c=0

      for file in $( ls ); do

          eval "var$c=$file";

          echo var:${var[$c]}

          c=$((c+1));

       

      done

       

       

      but can not display them:

       

      ./run3.sh

      val1 val2 val3

      var:

      var:

      var:

      var:

      var:

       

      Thank for help.

        • 1. Re: Create variable
          Dude!

          var=( val1 val2 val3 ) defines a so called dimensional array, i.e.:   var[0] = val1, var[1] = val2. var[2]= val2

          var[*] in shuch case would be the same as var="val1 val2 val3", which is a single string.

           

          $( var ) is the same as `var` (backticks) and the syntax for command substitution. Command substitution allows the output of a command to replace the command itself. For example: var=$( ls ) redirects the complete output of the ls command into variable var. Using $() is a POSIX standard.

           

          It's not a good practice to name variables after OS commands, which is simply confusing.

           

          The for file in $( ls ) as in your case is command substitution - it does not use your variable ls, defined earlier, but looping through elements of the ls command output.

           

          eval "var$c=$file";

          echo var:${var[$c]}

           

          You can put sideburns on a old chap but that don't make Elvis

           

          var1=myfile does not define a dimensional array as in var[indexnumber]. Hence your echo command has no output.

           

          The following should work:

          file=abc

          c=1

          var[$c]=$file                    # no need for eval.

          echo var: ${var[$c]}

          • 2. Re: Create variable
            happy10319

            Thank you so much.

            I need to construct:

            var1=a

            var2=b

            var3=c

            I do not need var[1], var[2] .....

            is it the case in your code?

            Output:

            I have not var1, var2, .....

            Regards.

            • 3. Re: Create variable
              Dude!

              I do not need var[1], var[2]

              I have not var1, var2,

              Then why did you do the following:

               

              eval "var$c=$file";

              echo var:${var[$c]}

               

              Sorry this doesn't make sense. I cannot read your mind.

               

              You need to better describe what you are trying to accomplish, otherwise it's a "waste" of time and effort.

               

              Perhaps you do not understand the difference between regular and dimensional variables. Obvioiusly, var1 is not the same as var[1].

              • 4. Re: Create variable
                Dude!

                If what you want is var1, var2, etc.

                 

                For example:

                 

                var=$*

                for item in $var; do

                  (( c++ ))

                  eval var${c}=$item

                  echo var${c}: $item

                done

                 

                $ ksh myfile a b c
                var1: a
                var2: b
                var3: c