1827006 Members
2367 Online
109712 Solutions
New Discussion

read only cat

 
SOLVED
Go to solution
Donny Jekels
Respected Contributor

read only cat

I am looking for a tool that does not have the power of cat.

cat > myfile, will flush the contents of myfile.

Is there anything other than more, more does'nt cut it as it stops when the terminal X limit.
"Vision, is the art of seeing the invisible"
14 REPLIES 14
A. Clay Stephenson
Acclaimed Contributor

Re: read only cat

The problem is not cat but rather the shell. Change your ">" to ">>" which will append rather than create/truncate.

If it ain't broke, I can fix that.
Bill McNAMARA_1
Honored Contributor

Re: read only cat

try:

for $i in filename
do
echo $i
done
It works for me (tm)
Massimo Bianchi
Honored Contributor

Re: read only cat

Hi,
the problem in not with cat but with myfile.

if you use that command, and you can trim the file, it's a choice of yours.


If you want a command to view the content of the file, just use "view FILENAME", it should do the work.

There are other commands, like "page", if you want read-only mode.

If the problem is the terminal, ou must first specify which kind of terminal you have, woth a sintax like

"export TERM=xterm"

and then

"resize"


HTH,
Massimo

John Meissner
Esteemed Contributor
Solution

Re: read only cat

also If you permissions are setup correctly then
cat > myfile
will not overwrite myfile

you could also use grep

grep . myfile
this will display the contents of myfile

however if you redirect any output using > to a file it will overwrite it
All paths lead to destiny
Kevin Wright
Honored Contributor

Re: read only cat

What are you really trying to do here?

view the file
cat file

append to the file
cat >> file

clear the file
> file
Bill McNAMARA_1
Honored Contributor

Re: read only cat

if your looking to cature STDIN,STDOUT and STDERR consider using script.
It works for me (tm)
A. Clay Stephenson
Acclaimed Contributor

Re: read only cat

Another useful command is "tee".

tee -a myfile will echo stdin to stdout and append the stdin to myfile. You can actually see what you are doing with this command. Man tee for details.

If it ain't broke, I can fix that.
Donny Jekels
Respected Contributor

Re: read only cat

thanks all,

The "grep . " is what I was looking for.

maybe my question was to poetic.

but I want to remsh in to a bunch of machines and send the contents of any file to STDOUT.

cat is way too dangerous.

peace
Donny
"Vision, is the art of seeing the invisible"
James R. Ferguson
Acclaimed Contributor

Re: read only cat

Hi:

If you are interested in protecting your file from inadvertant overwrite, you can set the "noclobber" option. See the man pages for 'sh_posix' for more details.

Consider:

#!/usr/bin/sh
exec 2> /dev/null
trap 'echo Attempted overwrite denied' ERR
echo "bang! at `date`" > /tmp/mylog
exit 0

Call the script "my.sh" and run it like:

# ./my.sh

Now run it with the 'noclobber' option:

# sh -C ./my.sh

Regards!

...JRF...
Kevin Wright
Honored Contributor

Re: read only cat

I don't see what you mean by "cat is too dangerous".

Cat simply 'concatenates' a file. Your redirection is what can be dangerous. Whether you use cat or your grep ., if you want to save STDOUT into a file, you still need to redirect the output!

remsh host "cat file"

Bill Hassell
Honored Contributor

Re: read only cat

As mentioned, cat is not a problem, it is redirection that is the problem. When you remsh to another machine, it is important to understand just where redirection will go. Consider these two commands:

remsh cpu1 cat /etc/passwd > /var/tmp/pw
remsh cpu1 cat /etc/passwd \> /var/tmp/pw

This will list the contents of /etc/passwd from the remote system called cpu1 and place in the local file /var/tmp/pw. The second line, however, will copy the file inside the remote server and the copy stays on the remote server. There is only one character different and that is the escape for >. The shell sees > and interprets it as a redirection symbol. With \>, the shell is told to ignore the redirection and the entire string is sent to the remote system.

It sounds like you need to write specific scripts to accomplish the needed tasks and debug how the data is being redirected. grep or any other command is just as dangerous as cat when you don't get redirection properly defined.


Bill Hassell, sysadmin
Donny Jekels
Respected Contributor

Re: read only cat

Kevin, yes you are absolutely correct. cat is just cat rihgt.

however been burnt before with remsh and executing the most obvious commands such as

for x in serv1 serv2 ... serv100
do
echo $x
remsh $x "cp /etc/passwd /tmp/passwrd.orig"
done

from anyones viewpoint this is a strait forward copy.

however for some unknown reason 10 of the boxes lost the password contents - and no-one could explain why.

one bitten twice shy

peace
Donny
"Vision, is the art of seeing the invisible"
Kevin Wright
Honored Contributor

Re: read only cat

Well, that is an interesting issue. I don't know what happened to cause your passwd file to be nulled out.

You may want to log all input/output/error of your commands

remsh $x 'cp passwd passwd.orig > /tmp/cp.log 2>&1' or something to that effect

Or like Bill mentioned, log all STDOUT and ERR to a local log file.
Donny Jekels
Respected Contributor

Re: read only cat

well, you can't blame for not trusting remsh now can you.

anyway, thanx for the advise. personnaly I like the grep method.

peace
Donny
"Vision, is the art of seeing the invisible"