1833870 Members
1689 Online
110063 Solutions
New Discussion

Shell script question

 
Jeff Picton
Regular Advisor

Shell script question

Hi

I need to create a new file within a shell script so that the new file may be then manipulated. I know the following :-

cat > filename but this hangs. Can anyone suggest another means so that filename may then be copied or moved ?

Thanks

Jeff
5 REPLIES 5
Geoff Wild
Honored Contributor

Re: Shell script question

# setup the log file
LOG=/tmp/some.log
if [ -f $LOG ]
then
mv $LOG $LOG.old
fi
cat /dev/null > $LOG


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
RAC_1
Honored Contributor

Re: Shell script question

The easiest way I could think is doing something as follows.

touch file_name
echo "something" >> file

Anil
There is no substitute to HARDWORK
Muthukumar_5
Honored Contributor

Re: Shell script question

You can use touch command to create a file. But it will be used to update the file informations.

touch filename

touch filename
By simple you can create as,

> filename

or

cp /dev/null filename


More you can create, file as,

>| filename

Where,

>| used for when the file exists there. By default if you user > filename on

set -o noclobber

> filename

will make error messages for existing files.

So use

>| filename

to avoid error to update informations of file.

REgards
Muthu
Easy to suggest when don't know about the problem!
John Palmer
Honored Contributor

Re: Shell script question

Hi Jeff,

'touch filename' will create a file if it doesn't exist already.

Simply '> filename' will create a file and also empty it if it already exists.

If you know what data you want it to contain, you can use cat with a 'here' document as follows:
cat > filename << EOF
your
data
EOF

EOF is an arbitrary terminator, you can pretty much use whatever you like. See man sh-posix for full details.

Regards,
John
Muthukumar_5
Honored Contributor

Re: Shell script question

why are you trying cat > filename there,

It may be used as,

cat filename > newfilename
cat filename >| newfilename (noclobber mode)

or

cat filename >> newfilename

It will append content.


Else as,

echo "test" | cat - > filename

- --> collect all stdoutput message which got from pipe redirection.
Easy to suggest when don't know about the problem!