Find the answer to your Linux question:
Page 2 of 2 FirstFirst 1 2
Results 11 to 16 of 16
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. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #11
    Just Joined!
    Join Date
    Nov 2012
    Posts
    21

    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
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    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
    Just Joined!
    Join Date
    Nov 2012
    Posts
    21
    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
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    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
    Just Joined!
    Join Date
    Nov 2012
    Posts
    21
    Hi,

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

  7. #16
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    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.

Page 2 of 2 FirstFirst 1 2

Posting Permissions

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