Last active
August 29, 2015 13:56
-
-
Save krasnobaev/8948344 to your computer and use it in GitHub Desktop.
bash use cases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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