Writing scripts to run on different Linux computers is tough. You can’t assume they’ll have the same characteristics. The getconf command lets you check hundreds of operating system and file system values.
Hardware Aware Scripts
If you’ve ever written a script that has to run on a variety of Linux machines, you might well have found yourself facing the “Strange, it runs just fine on my machine…” quandary. Small differences between computer architecture, operating systems, and file systems on the target machines can prevent your scripts from operating correctly.
If your scripts are simple and nothing they do—or try to launch—hinges on compatible hardware, or certain capabilities of the file system, you might never have an issue. But the more complicated your script becomes, the more critical are the details of the machine the script is running. It’s also worth noting that a single computer can have several file systems installed on it, mounted on different parts of the directory tree. Because something works in one directory doesn’t mean it’ll work in the same way in another directory.
The getconf command can report on literally hundreds of different operating system and file system characteristics. Some of these take the form of pairs of values, with one holding the system maximum and the other holding the minimum. This means you can test hardware or file system values to ensure your script will react accordingly.
Of course, your script can’t use getconf on the target computer if it isn’t installed on that computer. All the distributions that were tested when researching this article had getconf already installed, including Ubuntu, Fedora, and Manjaro. Interestingly they all had the same version of getconf too, version 2.33.
First Steps With the getconf Command
You can check that you’ve got getconf installed on your computer and find out which version it is, by using the –version option.
To see how many items getconf can report on, use the -a (all) option, and pipe it through the wc command. This will count the lines, words, and characters in the output.
There are 320 lines in the output. To see the entire list in a manageable way, we’ll pipe the output into less .
You can scroll forward and backward through to the list to see what getconf can report on.
Some of the items are duplicates. Many of them can be called by a shorter name or by a longer POSIX-compliant name.
Reporting on Single Values
To cause getconf to report on a single value, pass the name of the value to getconf on the command line. Note that all the option names are in uppercase.
The LONG_BIT option reports on whether your kernel—and hence, 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 on is LOGIN_NAME_MAX .
That’s a strangely high maximum length for a user name, but it is true. If you really wanted to create a user account with a preposterously long user name, you could. By contrast, this computer’s hostname has a maximum length of 64 characters:
To see how many simultaneous processes a user account can run, use the CHILD_MAX option:
The OPEN_MAX value returns the maximum number of files a process can have open.
To see a subset of the getconfvalues we can use the -a (all) option to generate all of the values, and use grep to filter out the ones we’re interested in. For example, to see every value that has “NAME” in its title, use:
Likewise, to see all values with “GROUP” in their title, use:
To see the values that have “INT_” in their title, use:
This shows the maximum and minimum values that an integer variable can hold on this computer.
RELATED: How to Search with “grep” Using Strings in a File
Using getconf With File System Values
When you use any of the options that relate to file system characteristics, you need to tell getconf which file system it should report on. You need to do this even if you only have a single hard drive and a single file system installed on it. The getconf command won’t assume anything. To indicate the file system getconf should report on, you must provide a path to a directory in that file system.
To see the maximum length for a file name for the file system mounted at the root of the file system directory tree, type:
To see the result for a different file system, point to a directory located in the other file system. Even if you don’t have different file systems permanently mounted in your directory tree, you can find yourself with another file system temporarily attached to your directory tree.
This is exactly what happens when you insert a USB memory stick, like this example. The memory stick is called “PINK” and it is mounted at “/media/dave/.”
We can check what file system is on the USB memory stick by using the df command to see what block device it is connected to. We then use the lsblk command with the -f (file system) option.
The memory stick is block device /dev/sdb1 and the file system is FAT32. That”ll be why we get different results for two directories in the same directory tree. They’re 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 from 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 could use a section of script like this.
The if statement compares the output of the getconf LONG_BIT command with the value of “64.” If they match, the then clause is executed otherwise the else clause is executed. You could either perform version-specific actions in the then/elseclauses or set variable flags that can be checked elsewhere in your script.
Copy the script text into an editor and save your file as “hw-test.sh.” We’ll make the script executable with the chmod command and the -x (executable) option.
When we run the script it tells us which of the clauses were triggered. Using this technique you could use any of the getconf values to direct your script to take the appropriate action.
Option Overkill
The sheer number of values that getconf can report on can be overwhelming. The man page is surprisingly brief. If you expect it to list the options and explain each one, you’re going to be disappointed.
You’ll find some descriptions in the man pages for sysconf and confstr. You might need to trim a few characters off the front of the confstr entries. For example, this didn’t work:
but this did:
You’ll also find some descriptions in the “limits.h” file. If you don’t know where that file is on your system, use:
You can then use less to review that file.