A Practical Guide to Uncovering Screen Resolutions in Linux Scripts for Enhanced Display Performance

A Practical Guide to Uncovering Screen Resolutions in Linux Scripts for Enhanced Display Performance

Discover the screen resolution in Linux scripts effortlessly Gain a clearer understanding of the display your script operates on

Key Takeaways

Obtain and modify screen resolutions by executing the xrandr command and retrieve the current resolution by parsing the output using awk. To accomplish this, employ the following line: xrandr | awk -F'[ +]' '/primary/{print $4}'

The xdpyinfo command offers extensive screen information. By utilizing awk, you can isolate the line containing the current resolution and further extract the X and Y resolutions as variables. On Linux machines, there are various commands available to determine the current screen resolution, making it convenient to integrate this functionality into a Bash script.

Why Change Your Screen Resolution

Determining your screen resolution prior to launching a script or performing additional tasks can be essential. This information can be used to pass the resolution settings as command line parameters for the upcoming application launch. Additionally, it can help verify the success of a resolution change if you have recently made alterations.

Lowering the resolution is a practical technique for enabling gameplay on older or less powerful hardware. However, constantly adjusting the resolution manually can become tedious. To simplify this process, it is recommended to incorporate the necessary commands into a script.

Fortunately, the commands we will discuss are commonly available on all the distributions we have examined. Therefore, there is no requirement to install additional software. You can immediately utilize these techniques in your scripts, which should remain adaptable across various distributions.

Using the xrandr Command

The xrandr command offers the ability to configure and retrieve information about the screen resolution. When used without any specific options, it provides a comprehensive list of the supported video modes for the screen or screens connected.

xrandr

A Practical Guide to Uncovering Screen Resolutions in Linux Scripts for Enhanced Display Performance

The preferred video modes are indicated by a plus sign "+", while the current video mode is denoted by an asterisk "*". This screen consists of a single preferred video mode, coincidentally serving as the current video mode. Despite the possibility of having multiple preferred modes, there can only be one current mode. Hence, identifying the line containing the asterisk allows us to ascertain the current video mode settings.

Online examples often employ grep to locate the line that includes an asterisk, followed by sed or awk to extract the desired information. However, a more straightforward approach exists. The xrandr output provides comprehensive details about the "connected primary" screen, including the current video mode resolution. By utilizing awk alone, we can effortlessly extract the required data without the need for grep.

xrandr | awk -F'[ +]' '/primary/{print $4}'

A Practical Guide to Uncovering Screen Resolutions in Linux Scripts for Enhanced Display Performance

Here's the rewritten version:

To execute the task, the -F (field separator) option is employed in awk, which designates that spaces and plus signs “+” should function as field delimiters. The objective is to locate lines containing the term "primary". This specific word was chosen because it only appears in the desired line and does not occur anywhere else in the xrandr output. By splitting the line using spaces and plus signs “+” as delimiters, the current resolution can be found in field four.

The awk print command is used for printing the fourth field. The resolution is also split at plus signs, removing the "0+0" from the end and making them fields five and six. However, those fields are not of interest to us. Check out the guide on using the awk command on Linux for more details.

Using the xdpyinfo Command

By default, the xdpyinfo command displays a lot of information about your screen and available video modes. It generates well over 1000 lines of output on our test machine.

xdpyinfo

A Practical Guide to Uncovering Screen Resolutions in Linux Scripts for Enhanced Display Performance

The current mode provides a more detailed description compared to the other available modes. Among the output, the line that specifies the resolution of the current mode contains the word "dimensions." By utilizing awk, we can extract the line of interest and retrieve the value following the word "dimensions."

Result:

xdpyinfo | awk '/dimensions/ {print $2}'

A Practical Guide to Uncovering Screen Resolutions in Linux Scripts for Enhanced Display Performance

The command used with xrandr was more complex compared to this one. No field delimiters need to be specified with awk as it defaults to using whitespace as field delimiters. In the output from xdpyinfo, we only have to deal with whitespace as delimiters. The resolution can be found as the second field in the line of output, and {print $2} will display it in the terminal window.

Putting It Into a Script

Using either of these methods in a script involves minimal additional effort. We must assign a variable to store the resolution that we find, allowing us to evaluate whether the script should continue processing or terminate if the current screen resolution does not meet its specific requirements.

Copy these lines to an editor, save the file as “get_res.sh”, then close your editor.

#!/bin/bashcurrent_res=$(xdpyinfo | awk '/dimensions/ {print $2}')echo $current_res

We’ll need to make the script executable using the chmod command.

chmod +x get_res.sh

A Practical Guide to Uncovering Screen Resolutions in Linux Scripts for Enhanced Display Performance

This assigns the output of the awk command to a variable called current_res, and echoes the value of that variable to the terminal window. Let's run the script.

./get_res.sh

A Practical Guide to Uncovering Screen Resolutions in Linux Scripts for Enhanced Display Performance

Encouragingly, we see the same results as we did when running the commands in the terminal window manually.

Separating the X and Y Resolutions

To separate the X and Y resolutions, follow these steps:

1. Open your editor and copy the following lines of code:

```bash

#!/bin/bash

current_res=$(xdpyinfo | awk '/dimensions/ {print $2}')

current_x=$(echo $current_res | awk -F'[ x]' '{print $1}')

current_y=$(echo $current_res | awk -F'[ x]' '{print $2}')

echo $current_res

echo $current_x

echo $current_y

```

2. Save the file as `get_x_y.sh`.

3. Close your editor.

To make it executable, we need to use chmod again.

chmod +x get_x_y.sh

A Practical Guide to Uncovering Screen Resolutions in Linux Scripts for Enhanced Display Performance

Here’s the output from the script on our test machine.

./get_x_y.sh

A Practical Guide to Uncovering Screen Resolutions in Linux Scripts for Enhanced Display Performance

By having separate variables for the X and Y resolutions, you can compare each dimension individually. If the horizontal resolution is the only one that matters to you, and it meets or surpasses a minimum, your script can continue.

Keep It Sweet and Simple

To ensure the uninterrupted functioning of your script that relies on resolution sensitivity, it is essential to have the capability of isolating the vertical and horizontal resolutions. This will allow you to test either one or both of them independently. Moreover, these values can also be utilized as command line parameters for launching other applications that require this information.

Getting hold of these values needn’t be convoluted. Even a little knowledge of the standard Linux utilities such as awk will repay the time spent learning them, many times over.