Find the answer to your Linux question:
Page 2 of 2 FirstFirst 1 2
Results 11 to 16 of 16
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #11

    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.

  2. #12
    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.

  3. #13
    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[*]}

  4. $spacer_open
    $spacer_close
  5. #14
    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[*]}

  6. #15
    Hi,

    it works now. Tnks so much for help and patience.

  7. #16
    Quote Originally Posted by keshk View Post
    it works now.
    That's great, glad you got it sorted.

    I'll mark this thread as Solved for you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •