Most shell scripts are like black box; they just do their magic without giving the slightest hint to the user about what just happened. Nevertheless, sometimes it is useful to show the commands that a shell script will execute. In Unix/Linux lingo, this is often called as dry run. Dry run is not available out of the box for custom shell scripts but can be easily implemented as show below.
# dry is a variable that indicates whether we want to do dry run or not
# Currently dry run is enabled. To disable it, set the dry variable to any other string
# Note: define the dry variable above the execute function below so
# that its available in the execute function
dry="y"
# Function: execute
function execute(){
# Irrespective of whether dry run is enabled or not, we display
# the command on the screen
echo "COMMAND: ${@}"
# if dry run is enabled then simply return
if [ $dry == "y" ]; then
return 0
fi
# if dry run is disabled, then execute the command
eval "$@"
}
# Now just append execute to all your commands
# This will cause them to print irrespective of whether we are doing any dry run or not
# Commands are only executed if dry run is disabled
execute "ls ~/"
execute "echo \"hello\""
Enjoy
Related posts: