Skip to content

Instantly share code, notes, and snippets.

@krasnobaev
Last active August 29, 2015 13:56
Show Gist options
  • Save krasnobaev/8948344 to your computer and use it in GitHub Desktop.
Save krasnobaev/8948344 to your computer and use it in GitHub Desktop.
bash use cases
command -v foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; }
type foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; }
hash foo 2>/dev/null || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; }
# http://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a-bash-script
read -p "Are you sure? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
# do dangerous stuff
fi
# http://stackoverflow.com/questions/1885525/how-do-i-prompt-a-user-for-confirmation-in-bash-script
#!/bin/bash
while :
do
echo "Press [CTRL+C] to stop..."
read -p "dict>" -r
dict $REPLY
done
# oneliner
while :; do echo "Press [CTRL+C] to stop..."; read -p "dict>" -r; dict $REPLY; done
# silent oneliner
while :; do read -p "dict>" -r; dict $REPLY; done
# chronic oneliner (save prompted words in bash_history)
while :; do read -p "dict>" -r; dict $REPLY; history -s "dict $REPLY"; done
# $# chronic oneliner example
# $while :; do read -p "dict>" -r; dict $REPLY; history -s "dict $REPLY"; done
# dict>sex
# dict>^C
# $ history
# 1 # chronic oneliner example
# 2 dict sex
# 3 dict
# 4 history
# $
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment