Writing scripts to run on different Linux computers is difficult. You cannot assume that they will have the same characteristics. the getconf The command allows you to check hundreds of operating system and file system values.

Hardware compatible scripts

If you’ve ever written a script that needs to run on a variety of Linux machines, you may have faced the “Strange, this works great on my machine …” dilemma. Small differences between the computer architecture, operating systems, and file systems on target machines can prevent your scripts from working properly.

If your scripts are simple and nothing they do (or try to run) relies on compatible hardware or some file system functionality, you might never have a problem. But the more complicated your script becomes, the more critical the details of the machine it is running. It should also be noted that a single computer can have multiple file systems installed on it, mounted on different parts of the directory tree. Just because something works in one directory doesn’t mean it will work the same in another directory.

the getconf The command can account for hundreds of different operating system and file system characteristics. Some of them take the form of pairs of values, one containing the system maximum and the other the minimum. This means that you can test the hardware or file system values ​​to make sure your script will react accordingly.

Of course your script cannot use getconf on the target computer if it is not installed on that computer. All distributions that were tested while researching this article had getconf already installed, including Ubuntu, Fedora and Manjaro. Interestingly, they all had the same version of getconf also, version 2.33.

Getting started with the getconf command

You can verify that you have getconf installed on your computer and find out which version it is, using the --version option.

getconf --version

To see how many items getconf can report, use the -a (all) option, and run it through the wc order. This will count the lines, words, and characters in the output.

getconf -a | wc

There are 320 output lines. To see the full list in a manageable way, we’ll direct the output to less .

getconf -a | less

You can scroll the list forward and back to see what getconf can account.

Getconv -a output in the minus file viewer

Some items are duplicates. Many of them can be called by a shorter name or by a longer POSIX-compatible name.

Unique Value Reports

Cause getconf to report a single value, pass the name of the value to getconf on the command line. Note that all option names are in upper case.

getconf LONG_BIT

the LONG_BIT The option indicates whether your kernel, and therefore your operating system, is 32-bit or 64-bit. There are no other parameters required for this command, just the LONG_BIT option itself. Another simple value to report is LOGIN_NAME_MAX .

getconf LOGIN_NAME_MAX

That’s a weirdly high maximum length for a username, but it’s true. If you really wanted to create a user account with a ridiculously long username, you could. However, the hostname of this computer has a maximum length of 64 characters:

getconf HOST_NAME_MAX

To see how many concurrent processes a user account can run, use the CHILD_MAX option:

getconf CHILD_MAX

the OPEN_MAX value returns the maximum number of files a process can open.

getconf OPEN_MAX

To view a subset of getconfvalues ​​we can use -a (all) option to generate all values, and use grep to filter those that interest us. For example, to see each value that has “NAME” in its title, use:

getconf -a | grep NAME

Likewise, to see all values ​​with “GROUP” in their title, use:

getconf -a | grep GROUP

To see values ​​that have “INT_” in their title, use:

getconf -a | grep _INT

This shows the maximum and minimum values ​​that an entire variable can hold on this computer.

RELATED: How to search with “grep” using strings in a file

Using getconf with file system values

When using one of the options related to the characteristics of the file system, you must indicate getconf which filesystem it should report on. You should do this even if you only have one hard drive and only one file system installed on it. the getconf the command will not assume anything. To specify the file system getconf must report, you must provide a path to a directory in this file system.

To see the maximum length of a file name for the file system mounted at the root of the file system directory tree, type:

getconf NAME_MAX /

To see the result for a different file system, point to a directory on the other file system. Even if you don’t have different file systems permanently mounted in your directory tree, you may end up with another file system temporarily attached to your directory tree.

This is exactly what happens when you insert a USB drive, like in this example. The USB stick is called “PINK” and is mounted on “/ media / dave /”.

getconf NAME_MAX /media/dave/PINK/

We can check which file system is on the USB stick using the df command to see which block device it is connected to. We then use the lsblk order with the -f option (filesystem).

df /media/dave/PINK
lsblk -f /dev/sdb1

The USB stick is a block device /dev/sdb1 and the filesystem is FAT32. This is why we get different results for two directories in the same directory tree. They are in the same directory tree, but they have different underlying file systems.

RELATED: What file system should I use for my USB drive?

Using getconf in scripts

We can easily incorporate the output of getconf in decisions inside our shell scripts.

For example, to perform different actions depending on whether the script is running on a 32-bit or 64-bit operating system, you can use a script section like this.

#!/bin/bash

# are we on a 32 or 64 bit OS?
if [ $(getconf LONG_BIT) = "64" ]
then
  # 64 bit
  echo "64-bit machine."
else
  # 32 bit
  echo "32-Bit machine."
fi

the if The instruction compares the output of getconf LONG_BIT command with the value “64”. If they match, the then clause is executed otherwise the else clause is executed. You can either perform version specific actions in the then/elseclauses or set variable flags that can be checked elsewhere in your script.

Copy the text of the script to an editor and save your file as “hw-test.sh”. We will make the script executable with the chmod order and the -x option (executable).

chmod +x hw-test.sh

When we run the script, it tells us which clauses were triggered. Using this technique, you can use any of the getconf values ​​to instruct your script to take the appropriate action.

./hw-test.sh

Overkill option

The large number of values ​​that getconf can pay off can be overwhelming. the man the page is surprisingly short. If you expect him to list the options and explain each one, you are going to be disappointed.

You will find descriptions in the man pages for sysconf and confstr. You may need to cut a few characters on the front of the confstr entries. For example, it didn’t work:

getconf _CS_GNU_LIBC_VERSION

but it did:

getconf GNU_LIBC_VERSION

You will also find descriptions in the “limits.h” file. If you don’t know where this file is located on your system, use:

where is limts.h 

You can then use less to examine this file.

LEAVE A REPLY

Please enter your comment!
Please enter your name here