3 Replies Latest reply on Oct 21, 2010 3:08 PM by Vinnie Lima

    Performing Split/Array assignments in NSH

    Vinnie Lima



      Trying to parse out an input into an NSH script within Bladelogic to perform the equivalent of the code below (for perl).  I rather not use perl due to the target dependency on perl being installed.  Does NSH support split and Array assignments, and foreach loops?



      $environment = "manager1,manager2,manager3" ;


      @MANAGERS = split(/,/,$environment) ;


      foreach $manager (@MANAGERS)
              print "manager: $manager \n" ;


        • 1. Re: Performing Split/Array assignments in NSH
          Bill Robinson

          The default NSH is ZSH 4.0.4, you can also switch to ZSH 4.3.4.


          I’d look at the zsh ref docs for this info if no one else responds.

          • 2. Re: Performing Split/Array assignments in NSH
            Wenchi Liao

            Does this work?


            $ a="manager1,manager2,manager3"
            $ b=( ${(s:,:)a} )
            $ for i in $b
            echo "$i"


            If the values have spaces in it, make sure to turn off SH_WORD_SPLIT.


            ${(s:,:)a} tells nsh to expand the value of a and split into fields based on the character between the colons. The colons can actually be any single character; (s#,#) would work as well.

            1 of 1 people found this helpful
            • 3. Re: Performing Split/Array assignments in NSH
              Vinnie Lima

              I fixed my own issue (thanks to Bill posting a blcli NSH script somewhere else).  Below will work, assuming you create an Property for the Server in Property Dictionary:

              # Obtain the list of Name = Value pairs for a Server Property called SNMP_MANAGERS
              # and then store the values into an array
              # Then execute a registry set for each manager


              typeset -i VARX=1


              blcli -a <appserver> -r <role> -u <user> -s <password> -v defaultProfile PropertyClass  listEnumPropertyValues Class://SystemObject/Server SNMP_MANAGERS
              #blcli -a emda-nbp-uea21 -r BLAdmins -u BLAdmin -s Emtoolset123 -v defaultProfil
              e Server printPropertyValue emda-nbp-uea26 SNMP_MANAGERS |


              awk -F= '{ print $2 }' | awk '{ gsub(/,/,"\n")}1' | awk '{ gsub(/ /,"")}1' |


              while read LINE
                      set -- ${LINE}


                       (( VARX = VARX + 1 ))


              typeset -i CHKVAR=1
              (( COUNT = VARX - 1 ))


              while (( CHKVAR <= COUNT ))
                  echo "${VARVALUE[${CHKVAR}]}"
                  (( CHKVAR = CHKVAR + 1 ))



              The output ends up being:


              <server>% ./test1.nsh