Alternate Link

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

# 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
        # 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\"" 


About Ritesh Agrawal

I am a applied researcher who enjoys anything related to statistics, large data analysis, data mining, machine learning and data visualization.
This entry was posted in Programming, Tips and tagged , . Bookmark the permalink.


  1. Verry good blog post. I definitely love this website.

    Keep writing!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s