0

Im running a IP address collection from HMC on AIX for each LPAR

$ lssyscfg -m 780 -r lpar -F name:state:rmc_ipaddr:os_version --filter lpar_names=ABCD123
ABCD123:Not Activated::Unknown
$ lssyscfg -m 780 -r lpar -F name:state:rmc_ipaddr:os_version --filter lpar_names=ABCD456
ABCD456:Running:10.8.34.21:VIOS 2.2.5.20

Im using cut command with delimiter to get the IP details. The non IP values are not output data.

$ lssyscfg -m 780 -r lpar -F name:state:rmc_ipaddr:os_version --filter lpar_names=ABCD123 | cut -d : -f3
10.8.34.21
$ lssyscfg -m 780 -r lpar -F name:state:rmc_ipaddr:os_version --filter lpar_names=ABCD456 | cut -d : -f3

There are some servers with the IP address not available. How could I tell the if command to ignore the one with null or no output to assign a value unknown

Could you please tell me how to use test this condition for the no value data I can assign a variable like ipaddr=unknown

  • Hi Kusalananda, DopeGhoti, William and ilkkachu, Thanks for giving the fix. ipaddr=$( lssyscfg -m 780 -r lpar -F name:state:rmc_ipaddr:os_version --filter lpar_names=ABCD123| cut -d : -f3 | grep . || echo unknown );echo $ipaddr unknown It gives me exact information what im looking for. You guys are really geeks – satsensort Apr 25 '18 at 4:20
  • @kusalananda Need you help on another issue – satsensort Apr 26 '18 at 9:15
  • I seldom look at how asks a question. If a question is interesting, then I will see it and consider it. Depending on the topic, someone interested in that topic will likely answer. You can not, however, request help from particular users. – Kusalananda Apr 26 '18 at 9:30
1

Use bash's own built-in default parameter expansion:

ipaddr="$(lssyscfg -m 780 -r lpar -F name:state:rmc_ipaddr:os_version --filter lpar_names=ABCD123 | cut -d : -f3)"
echo "${ipaddr:-unknown}"

First, we use a subshell construct $( command ) to capture the output of command, and assign that to a variable. Since the command in question is including your cut, it will correctly be null as expected in the case where your command does not return an IP address.

We then use the construct ${var:-default}, which will expand to the provided default value if var is unset or empty.

You can also, if you're going to be referring to that variable more than once, use a default assignation which will also set the value of that variable to the default if it is unset of empty:

$ echo "$foo"

$ echo "${foo:=bar}"
bar
$ echo "$foo"
bar

Or halt your script and throw an error if it's unset or empty:

$ echo "${bar:?bar is unset or empty}"
-bash: bar: bar is unset
$ echo $?
1
  • Hi DopeGhoti, im using the delimiter : here so the 3rd value is null. – satsensort Apr 24 '18 at 15:53
  • I copied the example command from your question which is purported to return the null output. – DopeGhoti Apr 24 '18 at 15:56
  • Hi Guys,I need another help also on the same command im caputing the state if its running or Not Activated in for loop. But it doesnt list the Not Activated systems on the for loop.# command - for status in lssyscfg -r lpar -m $system -F name:state --filter lpar_names="$lpar" | cut -d: -f2; do if [[ $status == Running ]] then state=Running else state=Not-Activated fi – satsensort Apr 25 '18 at 5:25
2
for name in ABCD123 ABCD456; do
    ip_addr=$( lssyscfg -m 780 -r lpar -F name:state:rmc_ipaddr:os_version --filter lpar_names="$name" | cut -d : -f 3 )

    printf 'Got IP address "%s" for lpar name "%s"\n' "${ip_addr:-unknown}" "$name"
done

The parameter expansion ${ip_addr:-unknown} will expand to the string unknown if $ip_addr is empty or if the variable is unset (it's always set to something here, but may be empty), otherwise it expands to the value of the variable ip_addr.

  • Hi Kusalananda, the 3rd value is null on one server and on other server it gives output – satsensort Apr 24 '18 at 15:53
  • @satsensort Could you please explain that comment? Is it a question or a statement? I know that it gives nothing back for one server. – Kusalananda Apr 24 '18 at 16:04
0

There are many ways to do this. One technique is:

ipaddr=$( lssyscfg -m 780 -r lpar -F name:state:rmc_ipaddr:os_version \
    --filter lpar_names=ABCD123 | cut -d : -f3 | grep . || echo unknown )
  • Hi Guys,I need another help also on the same command im caputing the state if its running or Not Activated in for loop. But it doesnt list the Not Activated systems on the for loop.# command - for status in lssyscfg -r lpar -m $system -F name:state --filter lpar_names="$lpar" | cut -d: -f2; do if [[ $status == Running ]] then state=Running else state=Not-Activated fi – satsensort Apr 25 '18 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.