Q
Q
qvb2019-10-13 00:27:40
bash
qvb, 2019-10-13 00:27:40

How to assign a specific value to a number or a value from an array?

Hello,
How can this be done properly/shorter?

echo -e "Select location:\n1) ${loc1}\n2) ${loc2}\n3) ${loc3}"
     22 read LOC
     23     if [ "$LOC" = 1 ]; then
     24     LOC=${loc1}
     25     else
     26 <------>if [ "$LOC" = 2 ]; then
     27 <------>LOC=${loc2}
     28 <------>else
     29 <------>    if [ "$LOC" = 3 ]; then
     30 <------>    LOC=${loc3}
     31 <------>    else
     47 <------>    read -p "error!"
     48 <------>    exit
     52 <------>    fi
     53 <------>fi
     54     fi

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
planc, 2019-10-13
@qvb

ar=('a' 'b' 'c')

echo -e "select location"

select loc in ${ar[@]}; do
    if [ -n loc ]; then
        echo $loc
        break;
    fi
done

ar=('a' 'b' 'c')
len=${#ar[@]}

echo -e "select location"

for (( i=0; i<len; i++)); do 
    echo $i\) ${ar[$i]}; 
done

read loc

if [ "$loc" -gt "$len" ]; then
    echo "error"
else
    echo ${ar[loc]}
fi

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question