3

Here is a problem which bothers me - I need to read version number from user input, and I'd like to create a "menu" using the length of the array storing the version numbers. However, BASH's mysterious syntax is not helping me here:

echo $VERSIONS
2.0.10-1 2.0.7-1 2.0.7-1 2.0.7-1 2.0.10-1

for v in ${!VERSIONS[*]}
do
  echo "$(($v+1))) ${VERSIONS[$v]}  "
done

output

1) 2.0.10-1
   2.0.7-1
   2.0.7-1
   2.0.7-1
   2.0.10-1  
2) 2.0.7-1  
3) 2.0.7-1  
4) 2.0.7-1  
5) 2.0.10-1   

another command

for v in ${!VERSIONS[*]}
do
  echo "$(($v+1))) ${VERSIONS[$v+1]}  "
done

1) 2.0.7-1  
2) 2.0.7-1  
3) 2.0.7-1  
4) 2.0.10-1  
5)   

What I'd really like to have is an output like that:

1) 2.0.7-1  
2) 2.0.7-1  
3) 2.0.7-1  
4) 2.0.10-1 

with out the last 5)....

Would be happy to unravel how to do it in bash...

P.S. A colleague of mine just offered a way without arrays. I'm posting it just for fun:

i=1
for v in $VERSIONS
do
  echo "$i) $v" ; i=$(($i+1))
done

output

1) 2.0.10-1
2) 2.0.7-1
3) 2.0.7-1
4) 2.0.7-1
5) 2.0.10-1

OK, since the solutions don't work inside my script I will post some more info:

for package in $NEWPACKAGES 
do  
    apt-show-versions -a -p $package
    VERSIONS=$(apt-show-versions -a -p $package | cut -d ":" -f 2 | cut -d " " -f 1)
    echo $VERSIONS
    echo "type the number for version you want to install: (type enter to skip)"

    for i in `seq 1 ${#VERSIONS[@]}`; do 
    echo "$i) ${VERSIONS[$(($i-1))]}"; 
done

    echo $VERSIONS    
    read version
    echo "your choice $version"
    # now the problem is that i can't get this part to work !
    apt-get install $package="${#VERSIONS[$version]}"
done
3
  • 1
    Actually your first variant works perfectly fine with me. Maybe you just forgot a declare -a to mark $VERSIONS as real array? Looks like it's just a string of words. Commented Aug 24, 2011 at 11:14
  • Hi Speckinius, I have added how the VERSIONS is created, I don't know how to make it a real array, comments would be appreciated Commented Aug 24, 2011 at 11:24
  • @Oz123 my answer showed you that you can create an array by array=($VERSIONS) Commented Aug 24, 2011 at 11:35

3 Answers 3

3

A version with arrays, if you still need one.

for i in `seq 1 ${#VERSIONS[@]}`; do 
    echo "$i) ${VERSIONS[$(($i-1))]}"; 
done
Sign up to request clarification or add additional context in comments.

6 Comments

versions is not really and array, i get it like this: VERSIONS=$(apt-show-versions -a -p $package | cut -d ":" -f 2 | cut -d " " -f 1)
@Oz123 What happens if you change it into VERSIONS=($(apt-show-versions -a -p $package | cut -d ":" -f 2 | cut -d " " -f 1))?
OK your solution is working: it produces a menu. But I can't get apt-get install $package="${#VERSIONS[$version]}" to work... it is still behaving oddly...
@Oz123 Shouldn't that be ${VERSIONS[$version]}?
@Le_Me_Be don't think so, right now any echo $VERSIONS just returns a single number .... so it is not an array any more...
|
1

So indeed you are looking for a away to convert a normal variable to an array, here it is:

array=($VERSIONS)

Comments

0
VERSIONS=(2.0.10-1 2.0.7-1 2.0.7-1 2.0.7-1 2.0.10-1) 
for i in ${!VERSIONS[@]} ; do echo "$(($i+1))] ${VERSIONS[i]}"; done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.