Skip to content

Instantly share code, notes, and snippets.

@santaklouse
Last active May 9, 2022 14:27
Show Gist options
  • Select an option

  • Save santaklouse/9a46d741f2f6bb57b6fa14507735c44d to your computer and use it in GitHub Desktop.

Select an option

Save santaklouse/9a46d741f2f6bb57b6fa14507735c44d to your computer and use it in GitHub Desktop.
Adds to bash env command for search cheatsheets (from cheat.sh)

Adds to bash env command for search cheatsheets (from cheat.sh)

Install

this will add function to ~/.bashrc file (create it if needed)

bash -c "$(curl -fsSL https://cutt.ly/HG6lFaV)"

Example:

reminds how to check directory existence

$ rtfm sh dir exists

will display

Search manuals for: 'sh+dir+exists' // (https://cheat.sh/sh+dir+exists) ...


# Check if a directory exists in a shell script
#
# To check if a directory exists in a shell script you can use the
# following:

if [ -d "$DIRECTORY" ]; then
  # Control will enter here if $DIRECTORY exists.
fi

# Or to check if a directory doesn't exist:

if [ ! -d "$DIRECTORY" ]; then
  # Control will enter here if $DIRECTORY doesn't exist.
fi

# However, as <a href="https://stackoverflow.com/users/1438/jon-
# ericson">Jon Ericson</a> points out, subsequent commands may not work
# as intended if you do not take into account that a symbolic link to a
# directory will also pass this check.
# E.g. running this:

ln -s "$ACTUAL_DIR" "$SYMLINK"
if [ -d "$SYMLINK" ]; then
  rmdir "$SYMLINK"
fi

# Will produce the error message:
#
# <!-- language: none -->

rmdir: failed to remove `symlink': Not a directory

# So symbolic links may have to be treated differently, if subsequent
# commands expect directories:

if [ -d "$LINK_OR_DIR" ]; then
  if [ -L "$LINK_OR_DIR" ]; then
    # It is a symlink!
    # Symbolic link specific commands go here.
    rm "$LINK_OR_DIR"
  else
    # It's a directory!
    # Directory command goes here.
    rmdir "$LINK_OR_DIR"
  fi
fi

# Take particular note of the double-quotes used to wrap the variables,
# the reason for this is explained by 8jean <a
# href="https://stackoverflow.com/a/67458/102401">in another answer</a>.
#
# If the variables contain spaces or other unusual characters it will
# probably cause the script to fail.
#
# [Grundlefleck] [so/q/59838] [cc by-sa 3.0]
#! /usr/bin/env bash
[ ! -f ~/.bashrc ] && echo $'#!'`which env`$' bash\n' > ~/.bashrc
echo $'\n'"$(curl -L https://cutt.ly/zG6xnKL)"$'\n' >> ~/.bashrc
. ~/.bashrc
function rtfm() {
old="$IFS"
IFS='+'
echo -n "Search manuals for: '$@'"
query="https://cheat.sh/$*"
echo " // ($query) ..."
echo "$str"
IFS=$old
curl "$query"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment