1

I am making a script that needs to loop over a list of files named like file1, file2, file3, all named the same with the suffix number increasing according to users input.

So I made thia script, but when I input number 3, she loops only over file1 and it stops. Why?

How do I make it loop over n files according to users input?

This is my script:

    #!/bin/bash
    echo "Base name of machines to bring down:"
    read vm
    echo "Number of machines:"
    read num

    COUNTER=0
    while [  $COUNTER -lt $num ]; do
      num=$[$COUNTER+1]
      VBoxManage controlvm $vm$num poweroff
      let COUNTER=COUNTER+1
    done
8
  • You are incrementing the counter twice. Is this intended? Commented Mar 13, 2012 at 17:45
  • You need to inspect this line: num=$[$COUNTER+1] Commented Mar 13, 2012 at 17:47
  • what is this? num=$[$COUNTER+1] Commented Mar 13, 2012 at 17:47
  • btw.: Did you try -p (prompt) with read: read -p "Number of machines:" num? Commented Mar 15, 2012 at 11:35
  • @Niklas,@bpgergo , I just need the num variable in first increment. I don't want to increment the COUNTER variable. I just neet to use counter+1 value in $vm$num part which is the name of machine to be used in a command. Since COUNTER starst at 0, and vm-s start at say vm1, I need to get that 1 part in vm1, by incrementing the COUNTER by 1. Commented Mar 17, 2012 at 0:55

3 Answers 3

3

use double paren for arithmetic:

#!/bin/bash
echo "Base name of machines to bring down:"
read vm
echo "Number of machines:"
read num

let COUNTER=1
while ((  COUNTER <= num )); do
    echo $COUNTER of $num
    let COUNTER=COUNTER+1
done

Update

I fixed the script so the counter starts at 1 instead of 0. It seems @toninoj wants to start at 1, but in his script, he started at 0.

Sign up to request clarification or add additional context in comments.

1 Comment

You can also use double paren for the increment. (( COUNTER++ )).
3

First line in the while loop you set num to 1, then at the bottom of the while loop you set COUNTER to 2, so it will never execute more than once. So you want something like this:

    #!/bin/bash
    echo "Base name of machines to bring down:"
    read vm
    echo "Number of machines:"
    read num

    COUNTER=1
    while [  $COUNTER -le $num ]; do          
        VBoxManage controlvm $vm$COUNTER poweroff
        let COUNTER=COUNTER+1
    done

Comments

3

I would think that this may be a better approach:

#!/bin/bash


for (( i=1 ; $i <= $1 ; i=i+1 ))   
do
      VBoxManage controlvm $2$i

done

$1 is the first parameter passed to a script. $2 the second parameter. Now you can call your script with

myscript vlaue vm

where when $1 is reached the script will stop and $2 is your vm parameter.

i tried this with the echo command

l1zard@Marvin:~/Downloads> ./test.sh 3 maria
VBoxManage controlvm maria0
VBoxManage controlvm maria1
VBoxManage controlvm maria2
VBoxManage controlvm maria3

2 Comments

-1: Your script does not work, it is full of errors and redundancy.
It is not bad, it should just start at 1, not 0

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.