0

I'm trying to know whether a kernel is 64 or 32 in #!/bin/sh.

I build this code!

krnl=$(uname -i)

# Check whether 64 or 32 bit kernel and download the correct version;
if [ $krnl='i386' ] ; then # 32
  # Do 32-bits stuff
else
  # Do 64-bits stuff
fi

But it seems to return always true. What I'm doing wrong, Why this is happening and how I can fix this? What I googled always show me in bash or doesn't work.

Thanks in advance!

waldyr.ar
  • 14,424
  • 6
  • 33
  • 64

5 Answers5

1

Whitesapce is significant in bash. You need spaces around the '=' character, like so:

if [ $krnl = 'i386' ] ; then # 32

Otherwise, you've defined a long string "x86_64=i386" or "i386=i386", and the non-empty string returns true.

Pat
  • 1,882
  • 2
  • 15
  • 22
1

Spaces are needed around '=' sign to properly test $krnl value:

krnl=$(uname -i)

# Check whether 64 or 32 bit kernel and download the correct version;
if [ $krnl = 'i386' ] ; then # 32
  # Do 32-bits stuff
else
  # Do 64-bits stuff
fi
Stephane Rouberol
  • 4,286
  • 19
  • 18
0

You have to put spaces around the equals sign in the [ command. Otherwise, it gets a single argument and returns true if that argument is not an empty string, which it will never be.

Also, uname -m might be better. I get "unknown" from uname -i on an amd64 machine.

Alan Curry
  • 14,255
  • 3
  • 32
  • 33
0

On my machine, uname -p returns i686 whereas uname -i returns i386.

AGS
  • 14,288
  • 5
  • 52
  • 67
0

In my console uname -m, uname -i and uname -p return x86_64 so :

krnl=`uname -i`

if [ $krnl="x86_64" ] ; then # 64
  echo "64"
else
  echo "32"
fi

should work works on my machine.

Update :

krnl=`uname -i`
if [ "$krnl" == "x86_64" ]; then
        echo "64"
else
        echo "32"
fi

When I change to :

if [ "$krnl" != "x86_64" ]; then

echoes 32

ant
  • 22,634
  • 36
  • 132
  • 182
  • You haven't fixed the spacing problem, all you did is reverse it so it always takes the 64 branch instead of always taking the 32 branch. It's still wrong. – Alan Curry Aug 18 '12 at 21:45
  • you were right but for different reasons (I didn't just swap). take a look at the edited question – ant Aug 18 '12 at 21:55
  • this is the correct answer I see no reason for down-vote http://stackoverflow.com/a/106416/169277 – ant Aug 18 '12 at 21:59
  • The updated version is not as bad. The first one is just wrong. Using `==` in a `[` command is a bashism though. – Alan Curry Aug 18 '12 at 22:12
  • true story, `= ''` works as well – ant Aug 18 '12 at 22:13