0

I'm quite new to shell scripting and have encountered an issue when trying to check for substrings within a string. I want to build code that checks if you are running a 64bit-based system. This is indicated by the output of the uname -m && cat /etc/*release command by the x86_64 in the first line.

Here's my code:

INFO=$(uname -m && cat /etc/*release)
if [ "$INFO" == *"x86_64"* ]
then
    echo "You are running a 64bit-based system!"
else
    echo "Your system architecture is wrong!"
    exit
fi

Although I run a 64-bit based system and the x86_64 shows up in the output of my command, the if statement still returns false, so I get the output Your system architecture is wrong!. It should be the opposite. Can someone help me out by identifying what I did wrong? I also accept general suggestions for improving my approach, but in the first place, I'd like to know where the bug is.

Many thanks for your help!

Totemi1324
  • 468
  • 1
  • 6
  • 19
  • 1
    Note that Arch Linux's `/etc/arch_release` doesn't contain the string `x86` or `x86_64`. (It has some lines like `PRETTY_NAME="Arch Linux"` and `BUILD_ID=rolling`). Perhaps `file /bin/ls` would be more portable as a way to check the user-space bitness. (As opposed to kernel bitness with `uname -a`, which might be a 64-bit kernel running 32-bit user-space.) – Peter Cordes Jun 17 '20 at 15:02
  • 1
    Are you running this script with `/bin/sh` or with `/bin/bash` or something else? – glenn jackman Jun 17 '20 at 15:25
  • @glennjackman I'm running with /bin/bash – Totemi1324 Jun 17 '20 at 15:36

4 Answers4

4
[

The command [ is equivalent to test command. test doesn't support any kind of advanced matching. test can compare strings with = - comparing strings with == in test is a bash extension.

By doing:

[ "$INFO" == *"x86_64"* ]

You are actually running command like [ "$INFO" == <the list of files that match"x86_64"pattern> ] - the *"x86_64"* undergoes filename expansion. If you would have a file named something_x86_64_something it would be placed there, the same way cat *"x86_64"* would work.

The bash extensions [[ command supports pattern matching. Do:

if [[ "$INFO" == *"x86_64"* ]]

For portable scripting that will always work with any kind of posix shell use case:

case "$INFO" in
*x86_64*) echo yes; ;;
*) echo no; ;;
esac
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
2

With bash version >= 3 you can use a regex:

[[ "$INFO" =~ x86_64 ]]
Cyrus
  • 84,225
  • 14
  • 89
  • 153
1

Unsure why it's so but your code starts working after doubling the square brackets:

INFO=$(uname -m && cat /etc/*release)
if [[ "$INFO" = *x86_64* ]]
then
    echo "You are running a 64bit-based system!"
else
    echo "Your system architecture is wrong!"
    exit
fi

Perhaps some explanation can be found under Is double square brackets [[ ]] preferable over single square brackets [ ] in Bash? and alikes.

bipll
  • 11,747
  • 1
  • 18
  • 32
  • 1
    Sure. Double-brackets ensure that globbing is done on the left-hand side of the operation, i.e. `$INFO`, and not to the files in the directory. BTW, you can omit the double-quotes, if you want to, since no word splitting is done in this situtation. – user1934428 Jun 17 '20 at 14:57
0

One way to check 64 bit is to simply grep the output of /bin/arch

if  /bin/arch | grep -q x86_64
then
  echo "it is 64 bit"
else
  echo "it is not"
fi
Sam Daniel
  • 1,800
  • 12
  • 22