Hi, I tried the above code you mentioned. Right now instead of getting a 1 2 3 4 print out, I get a 0 1 2 3 4 print out. -1 gone but a 0 added. :?
Printable View
Hi, I tried the above code you mentioned. Right now instead of getting a 1 2 3 4 print out, I get a 0 1 2 3 4 print out. -1 gone but a 0 added. :?
it is just saving (and printing) what you enter, isn't it? did you enter a "0"? it prints "1 2 3 4" when I enter just those four numbers.
Hm, seems I'm doing something wrong then. I'm using the codes below and a 0 keep adding.
Code:#!/bin/sh
option=0
myArray=()
while [ $option -ne -1 ] && myArray+=($option)
do
echo "Enter number to add to array: "
read option
done
echo ${myArray[*]}
you've got to move the && myArray+=($option) code inside the loop, b/c 0 does not equal -1 and it is being evaluated. try this:
Code:#!/bin/sh
option=0
myArray=()
while [ $option -ne -1 ]; do
echo "Enter number to add to array. Enter -1 to quit: "
read option
[ "$option" != '-1' ] && myArray+=($option)
done
echo ${myArray[*]}
Hi,
it works now. Tnks so much for help and patience. :)