Skip to content

BASH

Bourne Again Shell snippets.

Require PIP to use Virtual Environment

~/.bashrc

export PIP_REQUIRE_VIRTUALENV=true

Create a virtual environment

python3 -m venv /var/venv/{ENV}
source /var/venv/{ENV}/bin/activate  # 'deactivate' will exit.

Rename All File Extensions to Lowercase

find . -type d -execdir rename 's/(\.[A-Z]+$)/lc($1)/ge' *.*[A-Z]* \;

# Alternatively use rename binary.
rename 'y/A-Z/a-z/' *

Find Binary in All Files

# Sed for a signal file.
sed -n 's/.*[,"\[>[:space:]]\(.*\.exe\).*/\1/p'

# For all files.
find . -name "*.txt" -exec sed -n 's/.*[+,"\[>[:space:]]\(.*\.exe\).*/\1/p' {} \; > result-list

# Using grep is faster.
grep -iroh "\(.*\.exe\)" . | tee result-list

Prompt to require a specific keypress or die

echo 'This will cut a production release by overwriting prod with dev.'
read -n 1 -p 'Press Y to continue, any other key to abort: ' READ_CONTINUE

if [ "${READ_CONTINUE}" != 'Y' ]; then
  echo -e '\nAborting.'
  exit 1
fi

Last CLI Argument

Use last argument in current command.

!$
$_

# Alternatively 'alt + .' will copy the string.

Switch to a User with no login shell

su - -s /bin/bash {USER}

Parse INI value from file

#!/bin/sh
if [ "$1" != "${1#*[0-9].[0-9]}" ]; then
  echo IPv4
elif [ "$1" != "${1#*:[0-9a-fA-F]}" ]; then
  echo IPv6
else
  echo "Unrecognized IP format '$1'"
fi