Echo example scripts

Versions of echo scripts

echo1:
#!/bin/bash

echo 'set terminal svg size 400 300
set output "fig1.svg"
plot "fig1.data" with points pointtype 6, 2**x'

This script contains a single echo command with one argument in single quotes. The resulting output is three lines, since there are two returns in the string.

Demonstration

$ ./echo1
set terminal svg size 400 300
set output "fig1.svg"
plot "fig1.data" with points pointtype 6, 2**x
$ ./echo1 >commands
$ wc -l commands
3 commands

This demonstrates executing script in the current working directory with output to the terminal window. The script is also run with the output redirected to the file commands. The wc -l command is used to display the number of lines in the file.

Notes

Output redirect
The redirect <commands puts the output in a file. If the file exists, the old contents is replaced by the new contents. To append to the end, use a double >>, to force overwriting existing file, if noclobber is set, use >|.
Word count
The wc command will count lines, words and characters in the input stream. If the file name is on the command line then the file name appears in the output after the counts. If the command does not have a file name then just the counts of from stdin is written. With the option wc -l command just counts lines.
echo2:
#!/bin/bash

imageFile='fig2.svg'
dataFile='fig2.data'
function_x='0.83*2.046**x'

echo -n "\
set terminal svg size 400 300
set output \"$imageFile\"
plot \"$dataFile\" with points pointtype 6, $function_x
"

This script has a single echo command with one argument in double quotes. The output results in three lines. The echo is preceded by three string assignments. The assigned values are expanded in the double quoted string using the $ parameter expansion. The lines between echo -n "\ and the line with " will be in the file just as they are in the script with variable expansions. This is one way of doing what is called "here document" output.

Demonstration

$ ./echo2
set terminal svg size 400 300
set output "fig2.svg"
plot "fig2.data" with points pointtype 6, 0.83*2.046**x
$ ./echo2 | wc -l
3 

This demonstrates executing script in the current working directory with output to the terminal window. The first escaped return (A \ at then end or the line) does not insert a blank line at the beginning of the file. The values of the variables are inserted in the output, and the escape double quotes do not terminal the string. Then the script is run with the output piped to the the wc -l command is used to display the number of lines in the file.

Notes

Shell Variable assignment
The assignment is name=value with no spaces in the name or value. To assign a string with spaces, or other special characters use single quotes. Double quoted strings will treat the following characters as special:
  1. $ parameter expansion
  2. ` command expansion
  3. " termination of the string
  4. \ quote the next character, as in \$ \` \"
Strings with single quotes are called Strongly quoted strings, while strings with double quotes are called weakly quoted strings.
Shell Variable names
Variable names should be descriptive and not shell keywords, such as "function". Compound names can be used to avoid keywords. Here we illustrated two common ways to make compound names more readable.
  • Upper case to introduce a new word.
  • Underscore to separated words (You can not have a space in a variable name)
You can usually get away with using bash keywords as variables names, but you may confuse the parser and get some hard to understand error messages.
Pipes
The character | sets up a pipe between two processes. In this demonstration the stdout of the ./echo2 command is piped to the stdin of the wc -l command. This illustrates a common use of pipes - to avoid needing to write and then read an intermediate file.
echo3:
#!/bin/bash

source .echorc

if [ "$figTitle" ]; then
  echo -n "\
set title \"$figTitle\"
"
fi

echo -n "\
set terminal svg size $imageWidth $imageHeight
set output \"$imageFile\"
plot \"$dataFile\" with points pointtype 6, $function_x
"

This script has two echo commands with one argument in double quotes. The first is conditional on the variable figTitle containing data. The script begins with a source command. The file on the source command will contain variable assignment statements for variables needed to construct the 3 or 4 gnuplot command lines.

Demonstration

$ ./echo3
./echo3: line 3: .echorc: No such file or directory
set terminal svg size  
set output ""
plot "" with points pointtype 6, 
$ cat figrc > .echorc 
$ cat .echorc
imageWidth=400
imageHeight=300
imageFile='fig.png'
dataFile='fig.data'
$ ./echo3
set terminal svg size 400 300
set output "fig.png"
plot "fig.data" with points pointtype 6, 
$ cat fig1rc > .echorc 
$ cat .echorc
imageWidth=500
imageHeight=400
imageFile='fig1.svg'
dataFile='fig1.data'
commandFile='fig1commands'
function_x='0.83*2.046**x'
figTitle='Data with fitted exponential'
$ ./echo3
set title "Data with fitted exponential"
set terminal svg size 500 400
set output "fig1.svg"
plot "fig1.data" with points pointtype 6, 0.83*2.046**x

This demonstrates executing the script in the current working directory with output to the terminal window. The error message is because there is no file to source, and the following output shows the results of undefined variables. They are interpreted as nulls (0 length strings). There are example files which can be used as a run control file. The two commands cat commands will copy an example file to the .echorc file and display to contents. This way you can see the run control file and the resulting output.

Notes

Sourcing a file
The assignment statements can now be in the file .echorc. By sourcing the file, these variable assignments will be available to the original shell. The statements in a source file are executed in the same environment as the original shell.
if... fi structure
The if test command begins an if/then structure, that is terminated by the fi command. The then is a separate command, and if it is on the same line you must separate the commands with a ; character. Spaces are important to make sure the commands a properly recognized by the parser.
Test for empty string
The test [ -z "$myvar" ] evaluates as true if the myvar has zero length or is undefined.
The test [ -n "$myvar" ] evaluates as true if the string is defined with content. The -n is optional, so just a variable in double quote is a test for a string with data.
cat filename
The cat command will read from standard in write each line to standard out. When the output is redirected to a file this is just like the cp command. However, the cat command is more general. It can write to file, the terminal or to a pipe.
echo4:
#!/bin/bash

[ -r .echorc ] && source .echorc

case "$imageFile" in
  *.png )
     echo -n "\
set terminal png transparent size $imageWidth,$imageHeight
set output \"$imageFile\"
"
      ;;
 *.svg)
     echo -n "\
set terminal svg size $imageWidth $imageHeight dynamic
set output \"$imageFile\"
"
esac

echo -n "\
plot \"$dataFile\" with points pointtype 6${function_x:+, }$function_x
"

The source command is the second part of the compound command:

test command && command

The test command [ -r .echorc ] evaluates as true if the file .echorc is readable. With lazy evaluation with second command after && is not executed if the first evaluates as true, so the source .echorc is executed only if it can read the file. This script has two echo commands with one argument in double quotes. The first is conditional on one of two cases that depend on the pattern of the string in variable imageFile. The second echo uses the alternate value expansion ${function_x:+, }. This expands to ", " if the variable function_x has data and null otherwise. This way there will be no dangling comma when the function is not supplied, or is null.

Demonstration

Example using expand to expand the tabs
$ ./echo4
plot "" with points pointtype 6
$ cat figrc | tee .echorc
imageWidth=500
imageHeight=400
imageFile='fig.png'
dataFile='fig.data'
$ ./echo4
set terminal png transparent size 500,400
set output "fig.png"
plot "fig.data" with points pointtype 6
$ cat fig1rc | tee .echorc
imageWidth=500
imageHeight=400
imageFile='fig1.svg'
dataFile='fig1.data'
commandFile='fig1commands'
function_x='0.83*2.046**x'
figTitle='Data with fitted exponential'
$ ./echo4
set terminal svg size 500 400 dynamic
set output "fig1.svg"
plot "fig1.data" with points pointtype 6, 0.83*2.046**x

The first example in figrc is written based on the *.png pattern of the imageFile. Notice that there is no dangling comma on the plot command. The second example in fig1rc is written based on the *.svg case. Notice that for format of the commands are slightly different.

Notes

tee filename
The tee command will pipe everything from standard in to standard out and copy the lines to the file on the command. In this case we use it do display the line being stored in the .echorc file.
Lazy evaluation of && and || compound commands
With lazy && evaluation, the second command is skipped if the first fails. (If the first fails the the compound command and fails and there is not need to evaluate the second.) This a convient way to do one-line if statement. With lazy || evaluation, the second command is skipped if the first evaluates as true. (If the first is true the the compound command and true and there is not need to evaluate the second.) This a convient way to do one-line unless statement.
case... esac structure
The case string command begins an case structure, that is terminated by the esac command. The ( pattern begins the individual cases. The commands up to the ;; are executed if the string matches the pattern.
Alternate and default values parameter expansion
The parameter ${myvar:-foo bar expands as $myvar if there is data in it's value( not empty), or to "foo bar" otherwise. The string after the ":-" is the default value.
The parameter ${myvar:+foo bar expands as an empty string if there is data in it's value( not empty), or too "foo bar" otherwise. The string after the ":-" is the alternate value.
IT Help Center
University of Delaware
Last updated: August 11, 2010
Copyright © 2010 University of Delaware