1

I am trying to write a script where it will cross check to things:

1.The architecture for which the setup file was intended (32 or 64 bit)

2.The Architecture of the system.

The second part is quite easy and can be figured out using commands like lscpu and then extracting that specific line using combination of grep and awk or sed. However the first part is proving out to be a complicated one. I tried using the file command but it has a very irregular output. Hence it becomes very difficult extracting a specific column from it. I also tried using objdump though traditionally not used for things like this. However as expected, due to its limitations, it does not recognize most of the file types.

The rest part of the script is dead simple where I would be comparing these values and proceeding with my intended tasks. I would like your help with the Point 1 mentioned above.

EDIT:

Adding few examples to explain my problem with file command

$ file my_package_name.deb my_package_name.deb: Debian binary package (format 2.0)

$ file my_package_name.rpm my_package_name.rpm: RPM v3.0 bin i386/x86_64

Above examples were for setup files. If I were to run it for say some script, it would give me some things like:

$ file crclient O/P: crclient: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped

As you guys can see, I am getting a very inconsistent output with file command. Hence trying to extract a portion from this has become very complicated.

5
  • what is this setup file?
    – Skaperen
    Apr 14, 2015 at 9:58
  • Could you use a "tell, don't ask" approach? Try to do something which your application needs a 64 bit architecture to run, and then fall back to 32 bit install if that fails.
    – l0b0
    Apr 14, 2015 at 10:00
  • why would the 2 answers differ? maybe if you can say what your script is intended to do would help us understand. are you trying to prepare a userspace system file tree for a VM?
    – Skaperen
    Apr 14, 2015 at 10:03
  • @Skaperen I have added information to the question to explain what I mean by different outputs. And no, I am not trying to prepare a userspace system file tree.
    – theHeman
    Apr 14, 2015 at 10:14
  • @l0b0 That would be my last option :)
    – theHeman
    Apr 14, 2015 at 10:15

2 Answers 2

1

A 64bit CPU can have either 32bit or 64bit OS installed, and if it's running a 64bit OS it's possible to run 32bit applications with the matching compatibility libraries. Therefore, simply checking the CPU isn't necessarily going to give you the right answer.

Fortunately the uname command will give you quite a lot of what you want.

uname -a
Linux parrot 2.6.32-5-amd64 #1 SMP Tue May 13 16:34:35 UTC 2014 x86_64 GNU/Linux

uname -m
x86_64

I believe that you can check the canonical libc for a definitive statement of the type of OS installed on the system:

file $(readlink -f /lib/libc.so* ) | grep -q x86-64 && echo x86-64 || echo x86-32
0

As far as deb packages goes you could compare output of dpkg --print-architecture and dpkg-deb --info packagename.deb | awk '/Architecture:/ {print $2}'

In a script i'd have if statement like if [$SYSARCH -eq $PACKAGEARCH] where sysarch is system architecture and packagarch is package architecture, obviously

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .