1

I have a simple Bash shell Script to loop through each file in a directory and check is the copyright message is at the top of the file. If it's not there is should add it.


The script is giving an error when I try to use a variable in the sed command. I have looked at other similar questions and tried double quotes "", I have tried using a different operator but still can't find the solution. Can someone possibly point out what I am doing wrong?

Msg='/*------------------------------------------------------------------------------
*******************************************************************************
* Copyright message here
*******************************************************************************
*----------------------------------------------------------------------------*/'

for file in * 
  do
    if grep -Fxq "$Msg" $file
      then
        echo Marvel Message already exist: $file
    else
      if test -f "$file" 
        then
         echo "Adding Message to file: $file"
         sed -i "1s/^/${Msg}\n/" $file
      fi
    fi
done
sorak
  • 2,627
  • 2
  • 15
  • 24
Ms_Marvel
  • 13
  • 2
  • 1
    You need to use delimiters that aren't included in the variable, i.e not `/` – 123 Mar 13 '18 at 13:59
  • I have tried setting the delimiter to `|` as in `sed -i "1s|^|${Msg}\n|" $file` but still issues – Ms_Marvel Mar 13 '18 at 14:08
  • Possible duplicate of [sed replace with variable with multiple lines](https://stackoverflow.com/questions/6684487/sed-replace-with-variable-with-multiple-lines) – kvantour Mar 13 '18 at 16:29
  • Have a look at [sed replace with variable with multiple lines](https://stackoverflow.com/questions/6684487/sed-replace-with-variable-with-multiple-lines). There you see that you should replace your `sed` line with `sed -i "1s/^/${Msg//$'\n'/\\n}\n/" $file` – kvantour Mar 13 '18 at 16:30
  • 2
    No, no, no, no. Don't use sed at all for this. Just do: `{ echo "$Msg"; cat "$file"; } > $file.tmp; mv $file.tmp $file` Add some traps for cleanup if you want, and stop fooling yourself that `sed -i` will magically protect you from accidentally leaving tmp files on your filesystem. – William Pursell Mar 13 '18 at 16:49
  • @WilliamPursell you are correct, I was just trying to point out why the sed line failed. Your answer is the simplest. – kvantour Mar 13 '18 at 19:06

2 Answers2

2

I would not use sed for this. I would use ed:

ed "$file" <<END
1i
$Msg
.
wq
END

Or, using sponge from the moreutils package

{ echo "$Msg"; cat "$file"; } | sponge "$file"
glenn jackman
  • 223,850
  • 36
  • 205
  • 328
0

Simply you can do it with marge like this, here with echo "$Msg" > tmp.txt you are creating a file with your $Msg as header of the tmp file. And with cat $file >> tmp.txt you are merging the file.

for file in * 
do
if grep -Fxq "$Msg" "$file"
  then
    echo Marvel Message already exist: "$file"
else
  if test -f "$file" 
    then
     echo "Adding Message to file: $file"
     #sed -i "1s/^/${Msg}\n/" $file
     echo "$Msg" > tmp.txt
     cat "$file" >> tmp.txt
     mv tmp.txt "$file"
   fi
 fi
done

Hope this will help you.

tripleee
  • 158,107
  • 27
  • 234
  • 292
Abhijit Pritam Dutta
  • 5,284
  • 2
  • 8
  • 17