Write a program called mysed that applies the sed script given as the first argument against the file given as the second. If the sed succeeds (that is, exit status of zero), replace the original file with the modified one. Thus:
mysed '1,10d' text
will use sed to delete the first 10 lines from text, and, if successful, will replace text with the modified file
A script like this needs some tests to ensure that the user isn't going to break anything, so this is more complex, as is appropriate for the task. It also uses a variable called tempfile to store the contents of the sed invocation temporarily.
tempfile="mysed.temp"
if [ $# -ne 2 ] ; then
echo "Usage: mysed SEDARG filename" ; exit 1
fi
if [ ! -r "$2" ] ; then
echo "File $2 not found or not readable." ; exit 2
fi
sed "$1" $2 > $tempfile
if [ $? -eq 0 ] ; then
mv $tempfile "$2"
else
rm $tempfile
echo "file contents not updated due to sed failure"
fi
You might also like to view...
What is displayed on the right side of the Start menu?
A) pinned and recently used programs B) name of the current user and list of installed programs C) programs always displayed on the Start menu D) personal folders and common functions and commands available in Windows
When inserting a graphic, you need not worry about splitting up a paragraph as Word's default setting automatically places the graphic you insert one line below the end of the closest paragraph
Indicate whether the statement is true or false