About
Various testing on conditional events or variables (exit test, variable defaults).
Last Command Return Value
Make sure you have spaces inside the [] otherwise bash will sit the dummy!
Check for Success
if [ $? -eq 0 ]; then echo "Success" else echo "Failure"
Check for Failure
if [ $? -ne 0 ]; then echo "Failure" else echo "Success"
Conditionally Set Variables
Set if not already defined
: ${var=value}
This one is obscure....<TODO> explain!
Set if not defined OR set to the empty string
: ${var:=value}
Copy Selected Files
Recursively Copy Selected Files with Folder Structure
cp --parents `find -name .pydevproject` /target_directory/
Install Packages
Install packages via sudo only if it is not already installed
install_package () { PACKAGE_NAME=$1 dpkg -s ${PACKAGE_NAME} > /dev/null if [ $? -ne 0 ]; then sudo apt-get -q -y install ${PACKAGE_NAME} > /dev/null else pretty_print " $(padded_message ${PACKAGE_NAME} "found")" return 0 fi if [ $? -ne 0 ]; then pretty_error " $(padded_message ${PACKAGE_NAME} "failed")" return 1 fi pretty_warning " $(padded_message ${PACKAGE_NAME} "installed")" return 0 } # usage install_package ansible