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
6
  • 1
    You need to use delimiters that aren't included in the variable, i.e not / Commented Mar 13, 2018 at 13:59
  • I have tried setting the delimiter to | as in sed -i "1s|^|${Msg}\n|" $file but still issues Commented Mar 13, 2018 at 14:08
  • Possible duplicate of sed replace with variable with multiple lines Commented Mar 13, 2018 at 16:29
  • Have a look at 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 Commented Mar 13, 2018 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. Commented Mar 13, 2018 at 16:49

2 Answers 2

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"
Sign up to request clarification or add additional context in comments.

1 Comment

I ended up using the pipe sponge, but also used @William Pursell approach mentioned above. Thanks guys
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.