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 />

# This is a simple script that you can use for converting text into HTML.<br />

# First we take out all newline characters, so that the appending only happens<br />

# once, then we replace the newlines.<br />

echo "converting $1..."<br />

SCRIPT="/home/sandy/scripts/script.sed"<br />

NAME="$1"<br />

TEMPFILE="/var/tmp/sed.$PID.tmp"<br />

sed "s/\n/^M/" $1 | sed -f $SCRIPT | sed "s/^M/\n/" > $TEMPFILE<br />

mv $TEMPFILE $NAME<br />

echo "done."<br />

sandy ~><br />

$1 holds the first argument to a given command, in this case the name of the file to convert:<br />

sandy ~> cat test<br />

line1<br />

line2<br />

line3<br />

More on positional parameters in Chapter 7.<br />

sandy ~> txt2html.sh test<br />

converting test...<br />

done.<br />

sandy ~> cat test<br />

<br />

sed generated html<br />

<br />

<br />

line1<br />

line2<br />

line3<br />

<br />

<br />

<br />

sandy ~><br />

This is not really how it is done; this example just demonstrates sed capabilities. See Section 6.3 for a more<br />

decent solution to this problem, using awk BEGIN and END constructs.<br />

Easy sed<br />

Advanced editors, supporting syntax highlighting, can recognize sed syntax. This can be a great help if<br />

you tend to forget backslashes and such.<br />

5.4. Summary<br />

The sed stream editor is a powerful command line tool, which can handle streams of data: it can take input<br />

lines from a pipe. This makes it fit for non-interactive use. The sed editor uses vi-like commands and accepts<br />

regular expressions.<br />

The sed tool can read commands from the command line or from a script. It is often used to perform<br />

Chapter 5. The GNU sed stream editor 67

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

Saved successfully!

Ooh no, something went wrong!