Skip to content
Main Menu
  • Home
  • Blog
  • Contact

DevOps Fu

Tips and Tricks for DevOps Engineers

  • Home
  • Blog
  • Contact

Category: Bash

  • Home
  • Blog
  • Bash

How to persist variables in a loop from a find command in Bash?

December 21, 2020December 21, 2020 Fabrice

The usual way is to use a pipe: find . -name ‘*.jpg’ | while read f do; some_command "$f" done There are two problems here. First, this won’t work if paths have spaces or other blank characters in them. Second,… Read More

Bash

How to iterate an array in Bash?

October 10, 2020 Fabrice

Here you go: myarray[0]="this is a test" myarray[1]="this is another test" for i in "${myarray[@]}"; do echo "Value: $i" done Don’t forget the quotes around the ${myarray[0]}, or the spaces will separate each word on its own item. I work… Read More

Bash

How to trim leading and trailing characters in Bash?

August 25, 2020 Fabrice

Bash has built-in facilities to trim characters at the beginning and end of the value of a variable: $ myvar="AAAthis is a testAAA" $ echo ${myvar##*(A)} this is a testAAA $ echo ${myvar%%*(A)} AAAthis is a test I work as… Read More

Bash

How to use `find` to search for files based on modification time?

February 16, 2020March 10, 2020 Fabrice

To find the files older than 17 days: $ find . -daystart -mtime +17 -print The reason to use `-daystart` is to make things more natural for human beings, because `find` performs some rounding; from the man page: When find… Read More

Bash

How to have a dynamic variable name in bash?

February 12, 2020March 10, 2020 Fabrice

Code snippet below: myvar=something varname=myvar echo ${!varname} I work as a freelancer, so if you don’t want to do that kind of things yourself or don’t have the time, just drop me a line to hire me.

Bash

How to perform a lock/mutex in bash?

February 3, 2020March 10, 2020 Fabrice

This can be done using the `ln` command: tempfile=$(mktemp ./lock.XXXXXX) lockfile=./lockfile trap "rm $lockfile" EXIT try=1 while ! ln $tempfile $lockfile > /dev/null 2>&1; do try=$[ $try + 1 ] if [[ $try -gt 10 ]]; then echo "ERROR: Timeout… Read More

Bash

How to test that an environment variable is set in Bash?

February 3, 2020March 10, 2020 Fabrice

Use this: ${THEVAR:+x} Which will expand to “x” if THEVAR is set and not empty. Alternatively, this test can be used: if [[ ! -v THEVAR ]]; then echo "THEVAR is not set" fi But that will work even if… Read More

Bash

Recent Posts

  • How to persist variables in a loop from a find command in Bash?
  • How to iterate an array in Bash?
  • How to trim leading and trailing characters in Bash?
  • How to install jq on CentOS?
  • Magic SysRq cheatsheet

Categories

  • Automation
  • AWS
  • Bash
  • CentOS
  • Containers
  • Cryptography
  • Database
  • Development
  • Docker
  • Git
  • Linux
  • Python
  • Sysadmin
  • Virtualization

Archives

  • December 2020
  • October 2020
  • August 2020
  • July 2020
  • June 2020
  • April 2020
  • March 2020
  • February 2020
  • November 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
Copyright © 2018-2019 Fabrice Triboix
Best Business by Axle Themes