User avatar
Botspot
Posts: 1634
Joined: Thu Jan 17, 2019 9:47 pm
Location: Texas! :)
Contact: Website

Is there any executable file that is 100% going to be in /bin?

Mon Oct 12, 2020 9:41 pm

No matter what OS, no matter if it has a GUI, no matter if it uses systemd or sysvinit, are there any executable files that are going to exist in 100% of linux releases?
Weird question, I know. The reason why I ask is because I need a foolproof way of detecting whether what CPU architecture a given OS is meant for 32-bit, 64-bit, armhf, x86, you name it. And the detection must be done automatically and silently using a bash script.
(I thought the easiest way to detect it would be to find an executable file and detect what CPU architecture it's meant for)
Want to run Minecraft Java on your RPi? Easiest way is with Pi-Apps - just click Install.
https://github.com/Botspot/pi-apps
Pi-Apps also includes Zoom, Visual Studio, Tor browser, Windows 10 theme, and 33 more.
Over 50,000 users!

User avatar
jahboater
Posts: 6506
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Is there any executable file that is 100% going to be in /bin?

Mon Oct 12, 2020 9:43 pm

Botspot wrote:
Mon Oct 12, 2020 9:41 pm
No matter what OS, no matter if it has a GUI, no matter if it uses systemd or sysvinit, are there any executable files that are going to exist in 100% of linux releases?
Weird question, I know. The reason why I ask is because I need a foolproof way of detecting whether what CPU architecture a given OS is meant for 32-bit, 64-bit, armhf, x86, you name it. And the detection must be done automatically and silently using a bash script.
(I thought the easiest way to detect it would be to find an executable file and detect what CPU architecture it's meant for)
Do you mean like "file /bin/sh" ?
I think /bin/sh will always be there even if its a link to /bin/dash
Pi4 8GB and Pi4 4GB running Raspberry Pi OS 64-bit

User avatar
Botspot
Posts: 1634
Joined: Thu Jan 17, 2019 9:47 pm
Location: Texas! :)
Contact: Website

Re: Is there any executable file that is 100% going to be in /bin?

Mon Oct 12, 2020 9:47 pm

jahboater wrote:
Mon Oct 12, 2020 9:43 pm
Do you mean like "file /bin/sh" ?
I think /bin/sh will always be there even if its a link to /bin/dash
That could probably work.
But I'd have to find a way to follow the symlink or else when I try to detect the mimetype it will look like this:

Code: Select all

$ file --mime-type -b /media/pi/pi-power-tools/bin/sh
inode/symlink
Want to run Minecraft Java on your RPi? Easiest way is with Pi-Apps - just click Install.
https://github.com/Botspot/pi-apps
Pi-Apps also includes Zoom, Visual Studio, Tor browser, Windows 10 theme, and 33 more.
Over 50,000 users!

User avatar
DougieLawson
Posts: 40495
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Is there any executable file that is 100% going to be in /bin?

Mon Oct 12, 2020 9:50 pm

You'll always have /sbin/init as that's the program that the kernel starts (unless you override it in /boot/cmdline.txt).
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

User avatar
Botspot
Posts: 1634
Joined: Thu Jan 17, 2019 9:47 pm
Location: Texas! :)
Contact: Website

Re: Is there any executable file that is 100% going to be in /bin?

Mon Oct 12, 2020 9:53 pm

DougieLawson wrote:
Mon Oct 12, 2020 9:50 pm
You'll always have /sbin/init as that's the program that the kernel starts (unless you override it in /boot/cmdline.txt).
Another good answer. Too bad init can also be a symlink. Though again, I suppose the script could handle that and follow the symlink before getting the filetype.
Want to run Minecraft Java on your RPi? Easiest way is with Pi-Apps - just click Install.
https://github.com/Botspot/pi-apps
Pi-Apps also includes Zoom, Visual Studio, Tor browser, Windows 10 theme, and 33 more.
Over 50,000 users!

User avatar
kerry_s
Posts: 1737
Joined: Thu Jan 30, 2020 7:14 pm

Re: Is there any executable file that is 100% going to be in /bin?

Mon Oct 12, 2020 10:04 pm

why not just do /bin/bash
i figure if it's a bash script & there's no bash your screwed anyways. i've never come across a linux without a bash shell or i don't remember. :D
Last edited by kerry_s on Mon Oct 12, 2020 10:34 pm, edited 1 time in total.

User avatar
DougieLawson
Posts: 40495
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Is there any executable file that is 100% going to be in /bin?

Mon Oct 12, 2020 10:25 pm

If you're doing it in busybox then /bin/busybox must exist (unless you have some funky way of launching busybox).
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

User avatar
jahboater
Posts: 6506
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Is there any executable file that is 100% going to be in /bin?

Mon Oct 12, 2020 10:36 pm

kerry_s wrote:
Mon Oct 12, 2020 10:04 pm
why not just do /bin/bash
i figure if it's a bash script & there's no bash your screwed anyways. i've never come across a linux without a bash shell or i don't remember. :D
POSIX mandates that /bin/sh must exist.
Pi4 8GB and Pi4 4GB running Raspberry Pi OS 64-bit

User avatar
DougieLawson
Posts: 40495
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Is there any executable file that is 100% going to be in /bin?

Mon Oct 12, 2020 10:44 pm

jahboater wrote:
Mon Oct 12, 2020 10:36 pm
POSIX mandates that /bin/sh must exist.
On RaspiOS that's a symlink to /bin/dash.

Code: Select all

pi@ulysses:/bin $ ls -la sh
lrwxrwxrwx 1 root root 4 Jun 20  2019 sh -> dash
pi@ulysses:/bin $ readlink /bin/sh
dash
pi@ulysses:/bin $
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

DarkElvenAngel
Posts: 1239
Joined: Tue Mar 20, 2018 9:53 pm

Re: Is there any executable file that is 100% going to be in /bin?

Mon Oct 12, 2020 10:44 pm

kerry_s wrote:
Mon Oct 12, 2020 10:04 pm
why not just do /bin/bash
i figure if it's a bash script & there's no bash your screwed anyways. i've never come across a linux without a bash shell or i don't remember. :D
Busybox based distributions don't have bash. Also if busybox is used as the init for a distribution then init is a script.

trejan
Posts: 3062
Joined: Tue Jul 02, 2019 2:28 pm

Re: Is there any executable file that is 100% going to be in /bin?

Mon Oct 12, 2020 11:30 pm

What are you trying to achieve by doing this anyway? The requirement it must work on everything is going to give you lots of weird edge cases to deal with like a multi arch install of Debian. Are you trying to write a system information script?

DarkElvenAngel
Posts: 1239
Joined: Tue Mar 20, 2018 9:53 pm

Re: Is there any executable file that is 100% going to be in /bin?

Mon Oct 12, 2020 11:40 pm

I agree there is going to be somewhere that doesn't put things in the same place or you have a 64 bit kernel and a 32 bit user land.

I was going to suggest parsing the uname output but then you might have trouble with that one too.

cleverca22
Posts: 2393
Joined: Sat Aug 18, 2012 2:33 pm

Re: Is there any executable file that is 100% going to be in /bin?

Tue Oct 13, 2020 3:42 am

DougieLawson wrote:
Mon Oct 12, 2020 9:50 pm
You'll always have /sbin/init as that's the program that the kernel starts (unless you override it in /boot/cmdline.txt).

Code: Select all

[root@amd-nixos:~]# ls -l /sbin
ls: cannot access '/sbin': No such file or directory

[root@amd-nixos:~]# ls -l /lib
ls: cannot access '/lib': No such file or directory

[root@amd-nixos:~]# 
not all distros conform to such rules!, parsing uname will likely be simpler, just "uname -m" i believe

ejolson
Posts: 6321
Joined: Tue Mar 18, 2014 11:47 am

Re: Is there any executable file that is 100% going to be in /bin?

Tue Oct 13, 2020 3:51 am

Botspot wrote:
Mon Oct 12, 2020 9:47 pm
jahboater wrote:
Mon Oct 12, 2020 9:43 pm
Do you mean like "file /bin/sh" ?
I think /bin/sh will always be there even if its a link to /bin/dash
That could probably work.
But I'd have to find a way to follow the symlink or else when I try to detect the mimetype it will look like this:

Code: Select all

$ file --mime-type -b /media/pi/pi-power-tools/bin/sh
inode/symlink
If you are going to use file to detect the mimetype, doesn't that imply that the file program is installed and working? If it is, then use file to check whether file itself is a 32-bit or 64-bit executable.

Code: Select all

$ file /usr/bin/file
/usr/bin/file: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, BuildID[sha1]=7df606942d83740a79979415e18b9960f0344ff2, stripped
Of course that's in /usr/bin, so maybe not quite the answer you wanted.

swampdog
Posts: 465
Joined: Fri Dec 04, 2015 11:22 am

Re: Is there any executable file that is 100% going to be in /bin?

Tue Oct 13, 2020 1:28 pm

I usually start with 'uname' and work from there but to answer your question, I guess we need to find a native executable using minimum tools..

Code: Select all

#!/usr/bin/env sh

E="/bin/sh"
[ -f "$E" ] || exit 1
X=$(readlink "$E")
[ -z "$X" ] && X="$E"
X="$(which $X)"
$(which file) "$X" || exit 1
The above ought to work for other unix as well, BSD's etc that don't have things like /proc/version and /etc/(os-release|redhat-release) and so forth. For instance, the above 'readlink' could be simplified via "-m" option but the BSD's don't have it.

Return to “Beginners”