mirror of https://github.com/tLDP/LDP
36 lines
731 B
Bash
36 lines
731 B
Bash
#!/bin/bash
|
|
# prepend.sh: Add text at beginning of file.
|
|
#
|
|
# Example contributed by Kenny Stauffer,
|
|
#+ and slightly modified by document author.
|
|
|
|
|
|
E_NOSUCHFILE=85
|
|
|
|
read -p "File: " file # -p arg to 'read' displays prompt.
|
|
if [ ! -e "$file" ]
|
|
then # Bail out if no such file.
|
|
echo "File $file not found."
|
|
exit $E_NOSUCHFILE
|
|
fi
|
|
|
|
read -p "Title: " title
|
|
cat - $file <<<$title > $file.new
|
|
|
|
echo "Modified file is $file.new"
|
|
|
|
exit # Ends script execution.
|
|
|
|
from 'man bash':
|
|
Here Strings
|
|
A variant of here documents, the format is:
|
|
|
|
<<<word
|
|
|
|
The word is expanded and supplied to the command on its standard input.
|
|
|
|
|
|
Of course, the following also works:
|
|
sed -e '1i\
|
|
Title: ' $file
|