Skip to content

Instantly share code, notes, and snippets.

@minhnhdo
Created April 15, 2012 15:51
Show Gist options
  • Save minhnhdo/2393520 to your computer and use it in GitHub Desktop.
Save minhnhdo/2393520 to your computer and use it in GitHub Desktop.
answer to a friend's friend's question
#!/bin/bash
error () {
# USAGE: error <message> <is_fatal>
echo -n "!!ERROR!! " 1>&2
echo "$1" 1>&2
if (( "$2" )); then
exit 1
fi
}
write_menu () {
echo "1. Current date"
echo "2. Show content of file"
echo "3. Compile a C++ program"
echo "0. Exit"
}
clear
while true; do
write_menu
read -p "Your choice? (0-3) "
echo # add a blank line
case $REPLY in
1)
date +%d-%b-%Y | tr [:lower:] [:upper:]
;;
2)
read -p "Input filename: " filename
if [ -e "$filename" ]; then
echo "$filename exists"
if [ -r "$filename" ]; then
echo "<Begin content of $filename>"
cat "$filename"
echo "<End content of $filename>"
else
error "$filename is not readable" 0
fi
else
error "$filename does not exist" 0
fi
;;
3)
read -p "Input filename: " filename
# redirect errors to where? I redirect them to /dev/null here
g++ "$filename" 2>/dev/null
if [ $? -eq 0 ]; then
echo "Successfully built the program"
mv -i a.out "${filename%\.*}"
else
error "Something went wrong on the g++ side" 0
fi
;;
0)
echo "Quitting..."
exit
;;
*)
error "Invalid choice" 0
;;
esac
echo # add a blank line
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment