Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Howdie,

I have a bash script that installs phppgadmin and replaces the alias phppgadmin by !phppgadmin.

This is my script:

#installing phppgadmin and tell my Apache conf file to include it
apt-get install --force-yes phppgadmin && echo 'Include /etc/phppgadmin/apache.conf' >> /my/conf/apache2/apache2.conf

# the string manipulation
phppgadmin=`cat /etc/phppgadmin/apache.conf`
phppgadmin=${phppgadmin/Alias \/phppgadmin/Alias \/!phppgadmin}

#saving result
echo -e $phppgadmin > /etc/phppgadmin/apache.conf

This works well except line breaks get lost along the way, so of course Apache complains that file is not readable. I tried with cat -E, no success.

Any idea?

EDIT: Ok, it seems that i did not get what cat -E was doing. It actually substitutes line breaks by a dollar sign, so this is what I got now up and working:

phppgadmin=`cat -E /etc/phppgadmin/apache.conf`
phppgadmin=${phppgadmin/Alias \/phppgadmin/Alias \/!phppgadmin}
phppgadmin=${phppgadmin//$/'\n'}

echo -e $phppgadmin > /etc/phppgadmin/apache.conf

So, problem solved!

share|improve this question
    
If you were able to solve your problem, please use the "Answer your question" button below and provide an answer for the community to see. You can accept this answer in two days, and you'll even get reputation for it if it is upvoted! –  slhck Jun 12 '11 at 8:58

1 Answer 1

up vote 0 down vote accepted

To modify configuration files, sed is more reliable than bash:

sed -i 's|^Alias /phppgadmin|Alias /!phppgadmin|' /etc/phppgadmin/apache.conf

You are losing newlines because you passed $phppgadmin to echo unquoted, which means the shell splits the variable's contents at whitespace. Your original script would have worked if you used "$phppgadmin" instead.

Additionally:

  1. Do not use `cat`, use `<file` or $(<file) instead.

  2. ${phppgadmin//$/'\n'} will destroy any real $'s the original configuration file might have had.

Rewritten script, still not as reliable as the sed command above:

conf=$(< /etc/phppgadmin/apache.conf)
conf=${conf/Alias \/phppgadmin/Alias \/!phppgadmin}
echo "$conf" > /etc/phppgadmin/apache.conf
share|improve this answer
    
Thanks for the good advice! –  François ッ Vespa ت Jun 12 '11 at 19:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.