3

I'm deploying my project to production server using scp command. As part of the deployment process I upload to server the .htaccess file. Locally, the environment is set to DEVELOPMENT:

#Set environment
#[PRODUCTION, STAGING, TESTING, DEVELOPMENT] 

SetEnv KOHANA_ENV 'DEVELOPMENT'

However, when uploaded to server it needs to be changed to PRODUCTION. Is there any way I can accomplish that with ssh?

2
  • Why can't you change the file locally and then copy it?
    – jobin
    Jun 20, 2014 at 12:03
  • I don't mind changing it locally as long as it is done automatically by script. Jun 20, 2014 at 12:05

3 Answers 3

7

You can do it with a sed command and the -i flag to edit the file in place:

sed -i "s/SetEnv KOHANA_ENV 'DEVELOPMENT'/SetEnv KOHANA_ENV 'PRODUCTION'/g" /path/to/.htaccess

Via ssh I would look like this:

ssh user@host "sed -i \"s/SetEnv KOHANA_ENV 'DEVELOPMENT'/SetEnv KOHANA_ENV 'PRODUCTION'/g\" /path/to/.htaccess"

Working with sed to replace strings in files is quite simple:

sed -i 's/search_string/replace_string/g' file

Replaces all occurences of search_string with replace_string of the gives file. -i causes sed to edit the file in place, instead of writing to standard output (stdout).

If you only want the first occurence to be replace use this sed command:

sed -i '0,/search_string/s/search_string/replace_string/' file

For more information about sed use man sed.

2
  • Thanks, that works! I assume this command will replace all instances in the file, correct? Although it works for me know, can you please provide example of replacing text on a particular? It seems that it is possible based on the description of the sed command, however, I can't figure out how I can do that. Thanks in advance! Jun 20, 2014 at 12:33
  • 1
    @Maximus yes it replaces all instances, see my edit for replacing strings in file in general.
    – chaos
    Jun 20, 2014 at 12:42
2

You can directly write to the file locally as follows:

echo "#Set environment
#[PRODUCTION, STAGING, TESTING, DEVELOPMENT] 

SetEnv KOHANA_ENV 'PRODUCTION'" > /path/to/.htaccess

Remember to replace /path/to/ with the path to .htaccess and then copy the file using scp. This will replace the contents of the file.

4
  • Thanks for you suggestion. The problem is that the .htaccess file contains a lot of text besides the line that I need to change. The solution that you offered will erase everything else, correct? Jun 20, 2014 at 12:12
  • Do you know the line number of the file which you want to modify?
    – jobin
    Jun 20, 2014 at 12:16
  • Yes, it's 28th, which is SetEnv KOHANA_ENV 'PRODUCTION' Jun 20, 2014 at 12:19
  • @Maximus: chaos' answer should work now.
    – jobin
    Jun 20, 2014 at 12:21
2

If you want to convert just the 28th line, you can do:

sed '28s/DEVELOPMENT/PRODUCTION/' file

(combinable with -i option, of course)

If you want to avoid modifying a local file, you can do:

< /local/path/to/.htaccess sed '28s/DEVELOPMENT/PRODUCTION/' | ssh user@host "cat > /server/path/to/.htaccess"
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .