Skip to content

Instantly share code, notes, and snippets.

@xiorcal
Last active January 4, 2019 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiorcal/4d1da3e01fbf3db8916f211305d28419 to your computer and use it in GitHub Desktop.
Save xiorcal/4d1da3e01fbf3db8916f211305d28419 to your computer and use it in GitHub Desktop.
Bash #cheatsheet #tips

Bash

Language convention

tooling

CLI tools

beautysh (github)

This program takes upon itself the hard task of beautifying Bash scripts (yeesh).

install with pip install beautysh

shellcheck (github)

A shell script static analysis tool

install with cabal update ; cabal install ShellCheck

relevant atom packages

  • atom-beautify
  • linter
  • linter-shellcheck
#!/bin/bash
### Run in parallel
for c in $commands_to_run
do
c &
done
FAIL=0
echo "start waiting at $(date +%s)"
for job in $(jobs -p)
do
echo "$job"
wait "$job" || let "FAIL+=1"
done
echo "$FAIL"
echo "done wait (TS : $(date +%s))"
### process a file line by line
in="${1:-filename}"
[ ! -f "$in" ] && { echo "$0 - File $in not found."; exit 1; }
while IFS= read -r line
do
# avoid comments
[[ $line = \#* ]] && continue
echo "Process $line"
done < "${in}"
### Get pipestatus
echo "running a piped command" | sort | uniq | tee out.file
pipe_status_to_check=( ${PIPESTATUS[*]} ) # save it to prevent overiding
if [ ${pipe_status_to_check[0]} -ne "0" ];
then
echo "echo failed! WTF"
fi
### find cheatsheet
find -L -type f -empty # -L for follow symlink
find -type f ! -empty # negate the empty condition
### create symlink for all executable files in $home/bin
for f in ./*; do
if [[ -x "$f" ]]; then
ln -sf "$(pwd)/$(basename "$f")" "$HOME/bin/$(basename "$f")"
fi
donel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment