Quick Links
Writing scripts to run on different Linux computers is tough. You can't assume they'll have the same characteristics. The
getconfcommand 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
getconfcommand 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
getconfon the target computer if it isn't installed on that computer. All the distributions that were tested when researching this article had
getconfalready installed, including Ubuntu, Fedora, and Manjaro. Interestingly they all had the same version of
getconftoo, version 2.33.
First Steps With the getconf Command
You can check that you've got
getconfinstalled on your computer and find out which version it is, by using the
--versionoption.
getconf --version
To see how many items
getconfcan report on, use the
-a(all) option, and pipe it through the
wccommand. This will count the lines, words, and characters in the output.
getconf -a | wc
There are 320 lines in the output. To see the entire list in a manageable way, we'll pipe the output into
less.
getconf -a | less
You can scroll forward and backward through to the list to see what
getconfcan 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
getconfto report on a single value, pass the name of the value to
getconfon the command line. Note that all the option names are in uppercase.
getconf LONG_BIT
The
LONG_BIToption 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.
getconf 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:
getconf HOST_NAME_MAX
To see how many simultaneous processes a user account can run, use the
CHILD_MAXoption:
getconf CHILD_MAX
The
OPEN_MAXvalue returns the maximum number of files a process can have open.
getconf OPEN_MAX
To see a subset of the
getconfvalues we can use the
-a(all) option to generate all of the values, and use
grepto filter out the ones we're interested in. For example, to see every 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 the values that have "INT_" in their title, use:
getconf -a | grep _INT
This shows the maximum and minimum values that an integer variable can hold on this computer.
Using getconf With File System Values
When you use any of the options that relate to file system characteristics, you need to tell
getconfwhich 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
getconfcommand won't assume anything. To indicate the file system
getconfshould 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:
getconf NAME_MAX /
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/."
getconf NAME_MAX /media/dave/PINK/
We can check what file system is on the USB memory stick by using the
dfcommand to see what block device it is connected to. We then use the
lsblkcommand with the
-f(file system) option.
df /media/dave/PINK
lsblk -f /dev/sdb1
The memory stick is block device
/dev/sdb1and 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.
Using getconf in Scripts
We can easily incorporate the output from
getconfin 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.
#!/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
ifstatement compares the output of the
getconf LONG_BITcommand with the value of "64." If they match, the
thenclause is executed otherwise the
elseclause 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
chmodcommand and the
-x(executable) option.
chmod +x hw-test.sh
When we run the script it tells us which of the clauses were triggered. Using this technique you could use any of the
getconfvalues to direct your script to take the appropriate action.
./hw-test.sh
Option Overkill
The sheer number of values that
getconfcan report on can be overwhelming. The
manpage 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
manpages for
sysconfand
confstr. You might need to trim a few characters off the front of the
confstrentries. For example, this didn't work:
getconf _CS_GNU_LIBC_VERSION
but this did:
getconf GNU_LIBC_VERSION
You'll also find some descriptions in the "limits.h" file. If you don't know where that file is on your system, use:
whereis limts.h
You can then use
lessto review that file.