Operating System - HP-UX
1821870 Members
3311 Online
109638 Solutions
New Discussion юеВ

I need to replace a file when it exists

 
SOLVED
Go to solution
Manuales
Super Advisor

I need to replace a file when it exists

Hi ...
my user is using .cshrc

if there is a file created and if i want to replace it appears the following:
unix1: cat > test
test: File exists.

how can i avoid it? it does not mind if it exists, it has to be replaced.
how can i avoid it?
please let me know.

Regards.
7 REPLIES 7
Hein van den Heuvel
Honored Contributor

Re: I need to replace a file when it exists

I just means je have no right ro remove x.
Do an ls -l test
Is it a directory?
What is the mode?

I could not quickly reproduce the exact message, but here is a similar situation:

$ cat > x
blah blah
$ cat > x
this works
$ mkdir x
mkdir: cannot create x: File exists


hth,
Hein
Manuales
Super Advisor

Re: I need to replace a file when it exists

no .. test is a file ..

this is the way it is working:
server1 38: ls -l test
test not found
server1 39: cat > test
bla bla
server1 40: ls -l test
-rw-rw-rw- 1 user1 sapsys2 9 Sep 28 22:04 test
server1 41: cat > test
test: File exists.
server1 42:

what do i have to modify into .cshrc for user1 can replace files when i a script is using ">" .
thanks.
Bill Hassell
Honored Contributor
Solution

Re: I need to replace a file when it exists

Just like POSIX shells like ksh, the noclobber option prevents > from changing an existing file. This is to prevent destroying the contents of an existing file. You can override this setting by using:

>!test

Someone has set this option in your environment, perhaps in /etc/cshrc or your .cshrc file.

In POSIX shells, you override noclobhber with | rather than ! but otherwise, the form is the same. Most script writers find this behavior annoying and unexpected when noclobber has been set.


Bill Hassell, sysadmin
Dennis Handly
Acclaimed Contributor

Re: I need to replace a file when it exists

>how can i avoid it? it does not mind if it exists, it has to be replaced. how can i avoid it?

Besides Bill's noclobber solution, you can just remove the file first:
% rm -f test
% cat > test
James R. Ferguson
Acclaimed Contributor

Re: I need to replace a file when it exists

Hi Manuales:

Don't name a script (or any file) "test". This can create ambiguous situations for the shell which has a built-in 'test' or for that matter with the '/usr/bin/test' command!

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: I need to replace a file when it exists

>JRF: Don't name a script (or any file) "test".

Yes, I've had that happen with executables. :-(
Peter Nikitka
Honored Contributor

Re: I need to replace a file when it exists

Hi,

remembering one of your last posts, I guess we have to to with a CSH.
There is a variable 'noclobber', which controls the behavior in cases, where a redirection would overwrite an existing file.
Additionally there is another redirection symbol, which ignores an existing file.
% touch hu
% cat >hu
hu: File exists
% echo a >&hu # works
% set noclobber
% echo b >hu # works as well

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"