0

Im collecting the LPAR names and assigning to a variable on AIX , Then using multiple commands for processing.

LPAR Names on the system are : ABCD56777 TSM Mobile CD CPT 2

for lparname in `lssyscfg -r lpar -m $system -F name,lpar_env |cut -f 1 -d, | sort`;

But the above for loop variable doesn't pick up the complete LPAR value after the space. (Exact LPAR name is: ABCD56777 TSM Mobile CD CPT 2 )

It assigns the value of LPAR as ABCD56777 alone.

Could you advise me on how to assign variable with spaces. Unfortunately awk is not installed and only sed will work.

2
  • There is no assignment taking place in the code that you show. Please edit your question and add exactly what you're doing along with the expected result. It would also be good to know what the output of the lssyscfg command is as some of us don't have access to systems with that command.
    – Kusalananda
    May 2, 2018 at 7:53
  • command substitution should now use syntax $() instead of ` command `
    – Kiwy
    May 2, 2018 at 8:05

2 Answers 2

1

The issue is that your command substitution expands to a whitespace-delimited string and the loop will iterate over all words in that string. A "word" is anything delimited by a space, tab or newline character.

Also note that in Bourne-like shells other than zsh, those words are further subject to filename generation (aka globbing), so wildcard characters (and { in some shells) would also be a problem in addition to space and tab.

lssyscfg -r lpar -m "$system" -F name,lpar_env | cut -d, -f 1 | sort |
while IFS= read -r lpar; do
     echo "$lpar"
done

This uses a while loop instead of a static for loop. The while loop body is executed in a subshell (in bash), so the lpar variable will not exist after the end of the loop body. That's not an issue if you only use the variable in the loop body though.

Related: Understanding "IFS= read -r line"

Another solution would be to set IFS to a newline before the loop (and reset it afterwards):

oldIFS=$IFS
IFS=$'\n'    # assumes bash
set -o noglob

for lpar in $( lssyscfg -r lpar -m "$system" -F name,lpar_env | cut -d, -f 1 | sort )
do
    echo "$lpar"
done

set +o noglob
IFS=$oldIFS

The default value of $IFS includes spaces, which is why your original loop iterates over the wrong things. I have also turned off filename globbing for the loop, as we otherwise might get unexpected results if the text returned from lssyscfg includes filename globbing patterns as discussed above.

This last variation is IMHO quite inelegant, and downright inappropriate for commands that produce more than a handful of lines of output.

2
  • @Kusalananda, Thanks a lot for helping with the data. It print out the complete LPAR Names in that HMC console. 'ABCD56777 TSM Mobile CD CPT 2'
    – satsensort
    May 2, 2018 at 9:42
  • Many thanks everyone for helping with detailed solution
    – satsensort
    May 2, 2018 at 9:56
-1

I would try

for lpar in $(lssyscfg -r lpar -m $system -F name,lpar_env |awk -F, '{printf "\"%s\"",$1}' | sort) 
do
   echo "$lpar"
done
  • use $( ) as command substitution
  • {printf "\"%s\"",$1} will quote argument
1
  • Inserting quotes in the output will not help. The split+glob operator doesn't do anything with quotes (though some implementations understand backslash as a wildcard operator that disables wildcard operators) May 2, 2018 at 9:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .