0

im new to bash and im trying to figure out the simplest way to create a while loop that changes the variable number, and checks for a value. this way i can simply add to the variables and the if function knows to keep looping to the end

var='start loop'
var1='something'
var2='something else'
var3='maybe nothing but probably something'
function run() {
 while [ ! -z "$var" == true ]
 do
  x=1
  var=var$x
  if ! command -v $var &> /dev/null
  then
   DO SOMETHING
  fi
}
x=x+1
done
5
  • 1
    Use an array to store multiple things: linuxhandbook.com/bash-arrays Commented Jul 29, 2022 at 12:39
  • Are you targeting bash or a POSIX shell? Commented Jul 29, 2022 at 14:12
  • @Fravadona bash Commented Jul 29, 2022 at 16:50
  • while [ ! -z "$var" == true ] ?? Surely this gives errors. Commented Jul 29, 2022 at 18:47
  • @WilliamPursell yea probably. I am the king of errors. Thank you though. The help was already received and i was able to get it to work Commented Jul 29, 2022 at 19:39

1 Answer 1

1

Array was the way. Thank you

var=('thing' 'stuff' 'otherStuff')
function run() {
  x=0
  while [ ! -z "${var[x]}" ]
  do
    if ! command -v ${var[x]} &> /dev/null
    then
      apt-get install ${var[x]} -y
    fi
  sleep 2s
  x=$x+1
  done
}
Sign up to request clarification or add additional context in comments.

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.