0

I am writing a bash script for installing django with postgresql database automatically. Now , I want to replace database name by command ,since it is using sqlite3 database file path.

I wrote this command:

sudo sed -i 's/os.path.join(BASE_DIR, 'db.sqlite3'),/'$project_name',/g' $projects_name/settings.py

But this is not changed while I am making the django project.

How to change this , please help me.

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
HM Tanbir
  • 990
  • 12
  • 30

2 Answers2

2

Use double-quotes around sed for variable expansion & to preserve single quotes.

sed -i "s/os.path.join(BASE_DIR, 'db.sqlite3'),/$project_name,/g" "$project_name/settings.py"

tested working fine on GNU sed version 4.2.1

Inian
  • 80,270
  • 14
  • 142
  • 161
1

You should escape the quotes in 'db.sqlite3':

sudo sed -i 's/os\.path\.join(BASE_DIR, '\''db\.sqlite3'\''),/'$project_name',/g' $projects_name/settings.py

Your sed pattern is os.path.join(BASE_DIR, db.sqlite3), because quotes in 'db.sqlite3' are not interpreted as literal '. Instead they close and reopen the sed command.

SLePort
  • 15,211
  • 3
  • 34
  • 44