Versions of echo
scripts
#!/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.
$ ./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.
<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 >|
.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.
#!/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.
$ ./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.
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:
$
parameter expansion
`
command expansion
"
termination of the string
\
quote the next character, as in \$ \` \"
|
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.
#!/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.
$ ./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.
.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
structureif 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.
[ -z "$myvar" ]
evaluates as true if the myvar
has
zero length or is undefined. [ -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
filenamecat
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.
#!/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.
$ ./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.
tee
filenametee
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.
&&
and ||
compound commands&&
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
structurecase 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.
${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.${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.