based on user input, select array list: based on array list we choose, example JKT, I want to extract array index value to use as input for other command.
bellow is script:
read -p "Enter xGW need to be binding = JKT|SBY|xGW11_YGY|xGW12_YGY: " xGW
JKT=(saegw1.jkt.node 167.162.162.134 saegw1.jkt 167.162.162.137)
SBY=(saegw1.sby.node 167.162.179.128 saegw1.sby 167.162.179.131)
xGW11_YGY=(vsaegw11.ygy.node 167.162.191.59 vsaegw11.ygy 167.162.191.30)
xGW12_YGY=(vsaegw12.ygy.node 167.162.188.64 vsaegw12.ygy 167.162.188.88)
if [[ $xGW = "JKT" ]]
then
echo ${JKT[@]}
${JKT[0]} = a
${JKT[1]} = b
${JKT[2]} = c
${JKT[3]} = d
echo $a
elif [[ $xGW = "SBY" ]]
then
a = ${SBY[0]}
b = ${SBY[1]}
c = ${SBY[2]}
d = ${SBY[3]}
elif [[ $xGW = "xGW11_YGY" ]]
then
a = ${xGW11_YGY[0]}
b = ${xGW11_YGY[1]}
c = ${xGW11_YGY[2]}
d = ${xGW11_YGY[3]}
else [[ $xGW = "xGW12_YGY" ]]
a = ${xGW12_YGY[0]}
b = ${xGW12_YGY[1]}
c = ${xGW12_YGY[2]}
d = ${xGW12_YGY[3]}
fi
when running Script, we have bellow error:
[root@cacti]# ./binding.sh Enter xGW need to be binding = JKT|SBY|xGW11_YGY|xGW12_YGY: JKT saegw1.jkt.node 167.162.162.134 saegw1.jkt 167.162.162.137 ./binding.sh: line 21: saegw1.jkt.node: command not found ./binding.sh: line 22: 167.162.162.134: command not found ./binding.sh: line 23: saegw1.jkt: command not found ./binding.sh: line 24: 167.162.162.137: command not found
JKT
You have new mail in /var/spool/mail/root [root@cacti]#
my goal is to get a value, b value, c value and d value, so I can use it for other command.
please help,
Thanks in advance
CodePudding user response:
There are a couple of issues with your code as highlighted by @ÖzgürMuratSağdıçoğlu and @Armali in the comments on your question. Your first if
block is assigning the elements of the $JKT
array with the values of 'a' 'b' 'c' and 'd' rather than the other way around as in your elif
blocks. Further, your else
block has a test that does not function so the else
block will function as a catchall for any input that is not 'JKT' or 'SBY' or 'xGW11_YGY'. I refactored your code a bit to the following:
#!/bin/bash
read -rp "Enter xGW need to be binding = JKT|SBY|xGW11_YGY|xGW12_YGY: " xGW
declare -a target_array
if [[ "$xGW" == "JKT" ]] ; then
target_array=(saegw1.jkt.node 167.162.162.134 saegw1.jkt 167.162.162.137)
elif [[ "$xGW" == "SBY" ]] ; then
target_array=(saegw1.sby.node 167.162.179.128 saegw1.sby 167.162.179.131)
elif [[ "$xGW" == "xGW11_YGY" ]] ; then
target_array=(vsaegw11.ygy.node 167.162.191.59 vsaegw11.ygy 167.162.191.30)
elif [[ "$xGW" == "xGW12_YGY" ]] ; then
target_array=(vsaegw12.ygy.node 167.162.188.64 vsaegw12.ygy 167.162.188.88)
else
printf "Unknown option '%s', please try again\n" "$xGW"
exit 1
fi
a="${target_array[0]}"
b="${target_array[1]}"
c="${target_array[2]}"
d="${target_array[3]}"
printf "%s\n%s\n%s\n%s\n" "$a" "$b" "$c" "$d"
Pasting your code into shellcheck and implementing the recommended changes can be helpful as a first step in debugging your scripts.
CodePudding user response:
The -n
flag from the builtin declare
if it is available, should be able to do what you wanted.
Here is how I would do/write it.
#!/usr/bin/env bash
##: Define the arrays
JKT=(saegw1.jkt.node 167.162.162.134 saegw1.jkt 167.162.162.137)
SBY=(saegw1.sby.node 167.162.179.128 saegw1.sby 167.162.179.131)
xGW11_YGY=(vsaegw11.ygy.node 167.162.191.59 vsaegw11.ygy 167.162.191.30)
xGW12_YGY=(vsaegw12.ygy.node 167.162.188.64 vsaegw12.ygy 167.162.188.88)
read -rp "Enter xGW need to be binding = JKT|SBY|xGW11_YGY|xGW12_YGY: " xGW
##: If there is no input given. Print the error message.
##: Exit with a staus of 1.
if [[ -z $xGW ]]; then
printf 'No input given!\n' >&2
exit 1
elif [[ ! -v $xGW ]]; then ##: see `help test' for the `-v'
printf '[%s] is invalid!\n' "$xGW" >&2
exit 1
fi
##: Exit the script if the `declare' failed see `help declare'
declare -n xGW || exit
printf \\n
##: Define the array keys after the test/validation with `-v'
##: Mind you `keys' is defined/set and a valid var name and input.
keys=({a..d})
##: Loop through the elements (key and value) and create a variable
##: Assignment using `-v' flag from `printf', see `help printf'.
for f in "${!xGW[@]}"; do
printf -v "${keys[$f]}" '%s' "${xGW[$f]}"
done
##: Just for the human eye to see. Not something important/useful
##: in scripting. Do not process or parse these last lines! See `help printf`
printf '%s=(%s)\n\n' "${!xGW}" "${xGW[*]}"
printf 'a=%s\nb=%s\nc=%s\nd=%s\n' "$a" "$b" "$c" "$d"
Executing the script gives you:
Enter xGW need to be binding = JKT|SBY|xGW11_YGY|xGW12_YGY:
Not enter/input JKT
Output
JKT=(saegw1.jkt.node 167.162.162.134 saegw1.jkt 167.162.162.137)
a=saegw1.jkt.node
b=167.162.162.134
c=saegw1.jkt
d=167.162.162.137
With associative array, with the -A
flag from declare
just replace the for loop
with.
for f in "${!xGW[@]}"; do
declare -A input["${keys[$f]}"]="${xGW[$f]}"
done
The last two lines with:
printf '%s=(%s)\n\n' "${!xGW}" "${xGW[*]}"
printf 'a=%s\nb=%s\nc=%s\nd=%s\n' "${input[a]}" "${input[b]}" "${input[c]}" "${input[d]}"