I'm adapting this script to insert one file's content into another file. This is what I have now:
#!/bin/sh
# Check if first and second parameters exist
if [ ! -z "$2" ]; then
STRING=$(cat $1)
# Check if the supplied file exist
if [ -e $2 ]; then
sed -i -e "2i$STRING" $2
echo "The string \"$STRING\" has been successfully inserted."
else
echo "The file does not exist."
fi
else
echo "Error: both parameters must be given."
fi
I run it with: ./prepend.sh content.txt example.txt
The content.txt
file:
first_line
second_line
The example.txt
file:
REAL_FIRST_LINE
REAL_SECOND_LINE
The script's output:
sed: -e expression #1, char 24: unterminated `s' command
The string "first_line
second_line" has been successfully inserted.
And the content of example.txt
file remains the same, when I want it to be like this:
REAL_FIRST_LINE
first_line
second_line
REAL_SECOND_LINE