Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This is currently deleting the whole contents of the file in question:

#!/bin/bash
rpc_port="9201";
dir="/etc/apache2/ports.conf"
cat $dir | grep -v "Listen $rpc_port" > $dir

The file contents:

Listen 9200
Listen 9201
Listen 9202
Listen 9203
Listen 9204

Could someone please either fix this or give me a working alternative? :)

share|improve this question
2  
The pipe from cat to grep is a bit useless here, as you could have done grep pattern $dir –  Bernhard Aug 6 '12 at 6:17

1 Answer 1

up vote 4 down vote accepted

You cannot write (=redirect) to the same file you are reading from. Either redirect to a different file, or use sed.

sed -i "/$rpc_port/d" $dir
share|improve this answer
    
ah, I see :P tyvm for your help, that works great :D –  King Aug 6 '12 at 5:40

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.