24.07.2018 Views

Bash-Beginners-Guide

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />

Comments also make your own life easier. Say that you had to read a lot of man pages in order to achieve a<br />

particular result with some command that you used in your script. You won't remember how it worked if you<br />

need to change your script after a few weeks or months, unless you have commented what you did, how you<br />

did it and/or why you did it.<br />

Take the script1.sh example and copy it to commented-script1.sh, which we edit so that the<br />

comments reflect what the script does. Everything the shell encounters after a hash mark on a line is ignored<br />

and only visible upon opening the shell script file:<br />

#!/bin/bash<br />

# This script clears the terminal, displays a greeting and gives information<br />

# about currently connected users. The two example variables are set and displayed.<br />

clear<br />

# clear terminal window<br />

echo "The script starts now."<br />

echo "Hi, $USER!"<br />

echo<br />

# dollar sign is used to get content of variable<br />

echo "I will now fetch you a list of connected users:"<br />

echo<br />

w<br />

# show who is logged on and<br />

echo<br />

# what they are doing<br />

echo "I'm setting two variables now."<br />

COLOUR="black"<br />

VALUE="9"<br />

echo "This is a string: $COLOUR"<br />

echo "And this is a number: $VALUE"<br />

echo<br />

# set a local shell variable<br />

# set a local shell variable<br />

# display content of variable<br />

# display content of variable<br />

echo "I'm giving you back your prompt now."<br />

echo<br />

In a decent script, the first lines are usually comment about what to expect. Then each big chunk of commands<br />

will be commented as needed for clarity's sake. Linux init scripts, as an example, in your system's init.d<br />

directory, are usually well commented since they have to be readable and editable by everyone running Linux.<br />

2.3. Debugging <strong>Bash</strong> scripts<br />

2.3.1. Debugging on the entire script<br />

When things don't go according to plan, you need to determine what exactly causes the script to fail. <strong>Bash</strong><br />

provides extensive debugging features. The most common is to start up the subshell with the -x option, which<br />

will run the entire script in debug mode. Traces of each command plus its arguments are printed to standard<br />

output after the commands have been expanded but before they are executed.<br />

This is the commented-script1.sh script ran in debug mode. Note again that the added comments are<br />

not visible in the output of the script.<br />

willy:~/scripts> bash -x script1.sh<br />

+ clear<br />

+ echo 'The script starts now.'<br />

The script starts now.<br />

Chapter 2. Writing and debugging scripts 25

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!