Bash SED Unterminated `s' command

Liam Moy

I am a newbie at Bash scripting and have ran into a problem. I have searched and it looks like the issue may be the contents of my variable $MODIFIED.

Was wondering if anybody could help me find a way around this?

I am trying to fetch the last 5 modified files from a directory and then import them into a second script by replacing the first line in the second script.

My code so far:

#Pipe last 5 modified files within Students and output to script2
#Checks if script2 exists to avoid overwriting.
cd ~/Students/Stu5
MODIFIED=$(ls -1t | head -5)


if [ -f ~/Documents/OSShellScripts/OSScript2.sh ]; then
        cd ~/Documents/OSShellScripts
        echo "OSScript2 already exists."
        sed -i "1s/.*/$MODIFIED/" ~/Documents/OSShellScripts/OSScript2.sh
fi

This returns the error: sed: -e expression #1, char 17: unterminated `s' command

If i change the MODIFIED variable to say "hello" it works perfectly.

Any help with pointing me where I am going wrong would be great, thank you.

Kubator

Firstly properly quote bash variable when passing to sed.

Secondly remember that You are passing array into sed.

If that's on purpose You would need to do something like this:

sed -i '1s/.*/'"$(echo ${MODIFIED})"'/' ~/Documents/OSShellScripts/OSScript2.sh

I would also change sed's substitute command s to change line command c, since your variable may contain slashes:

sed -i '1c\'"$(echo ${MODIFIED[@]})"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related