Find the answer to your Linux question:
Results 1 to 7 of 7
I have a following shell script on Linux 64bit machine. Code: If [ `uname -a | grep 64` ] then echo 64bit fi The above script doesn't work properly. Why? ...
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    59

    simple grep in a shell script

    I have a following shell script on Linux 64bit machine.
    Code:
    If [ `uname -a | grep 64` ]
    then
    echo 64bit
    fi
    The above script doesn't work properly. Why?
    Also, the following script works fine. What's the difference?
    Code:
    if [ `uname | grep Linux` ]
    then
    echo Linux
    fi

  2. #2

  3. #3
    Just Joined!
    Join Date
    Apr 2007
    Posts
    59
    Sorry, that was a typo while writing here.. It is 'if'

  4. #4
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    post the error you get when you run the script
    Linux and me it's a love story

  5. #5
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    The key is in the output.

    This:

    Code:
    uname -a | grep 64
    produces an output which contains spaces, so, if will complain that there are too many arguments. Quoting when working with strings is a good thing to do.

    On the contrary:

    Code:
    uname | grep Linux
    This other command produces a single word as output. So, it will work even if you don't quote it correctly. In any case, it wouldn't hurt either to quote it. So, let's modify the code this way:

    Code:
    If [ "`uname -a | grep 64`" ]
    then
    echo 64bit
    fi
    However, I think that you should be parsing "uname -m" instead. There are lots of ways that a 64 could appear even if you are not using a 64 bits kernel so "uname -m" might be more accurate. It should say "x86_64".

  6. #6
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Please, delete this post, I posted the same thing twice by accident. Sorry.

  7. #7
    Just Joined!
    Join Date
    Apr 2007
    Posts
    59
    Great!
    Thanks, This is the reply I was expecting..

Posting Permissions

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