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.

2
  • $project_name and $projects_name are different variables? Commented Sep 14, 2016 at 6:01
  • sorry, $projects_name will $project_name Commented Sep 14, 2016 at 6:33

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, it is working.
@Engr.TanbirHasan: Happy to help!
It works as double quotes around the sed command preserve single quotes in 'db.sqlite3'. But please note that variable expansion is not the issue here.
@Kenavoz: totally agree to it! Added a note for the same.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.