bash

Bash: extend timeout for idle ssh sessions using TMOUT

The ClientAliveInterval and ClientAliveMaxCount settings in “/etc/sshd/sshd_config” work together to control the timeout value of an ssh session on the server side.  But under BASH, to keep idle client sessions from timing out, you also need to set the ‘TMOUT’ variable or you will see messages like below when disconnected. timed out waiting for input: Bash: extend timeout for idle ssh sessions using TMOUT

Ubuntu: Running a bash script periodically with a user-level Systemd timer

If you have a Bash script that needs to run periodically, you can run it using a crontab entry.  But you can also have it invoked by Systemd using systemd.timer. Furthermore, you can run Systemd services as  user-level services instead of the typical system-level service for even further isolation. Running via Systemd provides more powerful Ubuntu: Running a bash script periodically with a user-level Systemd timer

Ubuntu: Running a bash script periodically with a system-level Systemd timer

If you have a Bash script that needs to run periodically, you can run it using a crontab entry or file.  But you can also have it invoked from Systemd using systemd.timer. Running via Systemd provides more powerful constructs for invocation, configuration, monitoring, and logging.  In this article, I will show how to periodically run Ubuntu: Running a bash script periodically with a system-level Systemd timer

Bash: deleting a file with special characters using its inode value

If you have a file with special characters (single quotes, wildcard, etc) in the name, it can be difficult to discover the exact escape sequence to correctly delete.  To avoid playing with escape characters, you can simply use the inode number of the file instead. For example, let’s say you accidentally specify tar options incorrectly Bash: deleting a file with special characters using its inode value

Terraform: post-configuration by calling remote-exec script with parameters

If you are creating a VM resource and must run a Bash script as part of the initialization, that can be done within Terraform using the remote-exec provisioner and its ability to execute scripts via ssh. If you need to send arguments to this script, there is a standard pattern described in the official documentation Terraform: post-configuration by calling remote-exec script with parameters

Bash: find most recently modified files

Needing to find the most recently modified files in a directory is a pretty common need.  Luckily the find utility has flags to easily explore a directory recursively and list recently modified files. If you want to find modified files within ‘N’ days ago from the current directory. # files within the last 24 hours Bash: find most recently modified files

Bash: cloning the ownership and permissions of another file using reference

If you need to create a file that has the exact same ownership and permission bits as an existing file, the ‘reference’ flag provides a convenient shortcut. For example, if you had a file named ‘myoriginal’ that had the exact ownership and permissions required for a new file ‘mynewfile’, you could use the commands below Bash: cloning the ownership and permissions of another file using reference

Bash: using printf to display fixed-width padded string

One way to implement character padding in Bash is to use printf and substring extraction.  This can be especially useful in reports or menu display. Given a $padding variable that contains the maximum length of characters, you can subtract out the length of a display string like below. # length of maximum padding padding=”………………………………..” printf Bash: using printf to display fixed-width padded string

Bash: using multiple values from an input pipeline to construct and execute a command

If you have multiple values coming through the Bash input pipeline, it can be difficult to process these into a complex, formatted set of arguments unless you use intermediate temporary files. But one way to neatly put together a complex set of arguments from an input pipeline with multiple values is to use awk printf Bash: using multiple values from an input pipeline to construct and execute a command

Bash: deep listing the most recently modified files in a directory

Finding the most recently modified files in a directory can be extremely beneficial when you have been making changes in files throughout the directory structure as part of a work effort, and now need to go back and pinpoint everything that was changed. This command will provide you the 10 most recently modified files, excluding Bash: deep listing the most recently modified files in a directory

Bash: Difference between two arrays

Whether looking at differences in filenames, installed packages, etc. it can be useful to calculate the difference between two Bash arrays. SiegeX on stackoverflow.com offered the following function using awk, and I have built a full example available on github. function arraydiff() { awk ‘BEGIN{RS=ORS=” “} {NR==FNR?a[$0]++:a[$0]–} END{for(k in a)if(a[k])print k}’ <(echo -n “${!1}”) <(echo Bash: Difference between two arrays

Bash: Associative array initialization and usage

Just as in other programming languages, associative arrays in Bash are useful for search, set management, and keying into a list of values.  The label may be different, but whether called “map”, “dictionary”, or “associative array”, the same concepts apply. In its simplest form, here is an initialization and then loop through the key/value pairs. Bash: Associative array initialization and usage

Bash: Appending to existing values using sed capture group

sed is a powerful utility for transforming text.  One of the nice tricks with sed is the ability to reuse capture groups from the source string in the replacement value you are constructing. For example, if you have have the following kernel parameters in “/etc/default/grub” $ grep GRUB_CMDLINE_LINUX_DEFAULT /etc/default/grub GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash” And wanted to append Bash: Appending to existing values using sed capture group

Bash: Renaming files using shell parameter expansion

Shell parameter expansion provides various ways to manipulate strings, and a convenient way to succinctly express renaming a set of files. In its simplest form, parameter expansion is simply ${parameter}.  But look at these examples: $ mystr=”TheQuickBrownFox.jpg” # chop off last 4 digits $ echo ${mystr:0:-4} TheQuickBrownFox # truncate end of string ‘.jpg’ $ echo Bash: Renaming files using shell parameter expansion

Bash: Using shell or environment variables in awk output

If you are in the middle of a text processing pipeline, and need to insert a shell or environment variable into the output of awk, you can use the “-v” flag. Here are two files containing animal classifications: $ echo -e “shark=fish\ndolphin=mammal” > ocean.txt $ echo -e “dog=mammal\neagle=bird” > land.txt By passing the loop variable Bash: Using shell or environment variables in awk output