Little Girl's Mostly Linux Blog

BeforeYouBeginScript

Before You Begin A Bash Script

This page was last updated on September 07, 2013.
  1. Write the concept of your script in plain language, either as numbered steps or in outline form. Think about what you want the script to do. Some things to keep in mind when doing this are:

    • Will the user need to provide information to the script?
    • Will your script need to create files or directories on the user’s computer?
    • Will your script use common enough commands that they’re likely to already be on the user’s computer, or will the user have to install specific packages to run the script?
    Example:
    1. Ask the user if he/she would like to update a specific program.
    	a. If the user says yes:
    	   - Go to Step 2.
    	b. If the user says no:
    	   - Alert the user that the program has not been updated.
    	   - Exit the script.
    2. Update the program with the new kernel.
    3. Alert the user that the program has been updated.
    4. Exit the script.
    
  2. Choose a name for your script. To be sure the name you want to use for your script isn’t already being used on your system, substitute the name you’ve chosen for scriptname in all three of these commands:
    which -a scriptname
    whereis scriptname
    locate scriptname
  3. Use the list or outline you created as a guide when writing the script. If you run into trouble, you’ll be able to use the steps in the guide to get help on how to accomplish certain tasks.
  4. Choose one of these methods to run your script, replacing scriptname with your script’s name:

    • Run the script in a subshell of the current shell:
      bash scriptname
    • Run the script in the current shell:
      source scriptname
    • Run the script if it’s not on the PATH:
      ./scriptname
    • Run the script if it’s on the PATH:
      scriptname
  5. If the script doesn’t run properly, try running it in debug mode. This will show you each command before executing it. Replace scriptname with your script’s name:
    bash -x scriptname
  6. If you know that most of the script is written properly, but you have a section you’re unsure of, insert this line directly above the questionable section:
    set -x
    Then insert this line directly below the questionable section:
    set +x

    Run the script normally, and the questionable section will run in debug mode. Once you’ve figured out how to fix the questionable section, remove the two debug lines from the script.

Obligatory Happy Ending

And they all lived happily ever after. The end.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Comment:

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

Create a free website or blog at WordPress.com.

%d bloggers like this: