emptying a file and keeping its ownership

John Poplett john at cstreet.com
Tue Jan 1 07:19:51 AEST 1991


In article <1990Dec30.220722.29050 at jarvis.csri.toronto.edu> ckchee at dgp.toronto.edu (Chuan Chee) writes:
>I have SCO Xenix 2.2.3.  What's the easiest way to "empty" a file
>while keeping its ownership (owner,group) and access permissions the
>same?  Actually I only care about permissions (rw-rw-rw).
>I would like this done in Bourne shell (or possibly CSH).
>One other thing, this shell script is run under root.
>

The simplest way to truncate a file using the Bourne shell
is:

> file

Here's a short Bourne shell script that truncates files. Cut it
out and save it to trunc.sh and run "make trunc" to get an executable
script file.

John

------ cut here -------- cut here -------- cut here --------
: 
# @(#)trunc.sh -- truncate or create empty files. Optionally, setting 
# owner or group ID.

usage()
{
	echo "usage: $0 [-o owner] [-g group] file1 [file2...]" 1>&2
	exit 1
}

if [ $# -lt 1 ]
then
	usage
fi

set -- `getopt g:o: $*`

if [ $? != 0 ]
then
	usage
fi

for i in	$*
do
	case $i in
	-g)	      group=$2;	shift; shift;;
	-o)	      owner=$2;	shift; shift;;
	--)       shift; break;;
	esac
done

for file in $*
do
	> $file
done

if [ $group ]
then
	chgrp $group $*
fi

if [ $owner ]
then
	chown $owner $*
fi

exit 0
-- 
John Poplett @ C Street Software     | Never make forecasts, especially
312 Wolff St. Oxnard, Ca. 93033 USA  | about the future.
(805) 486-7807 / john at cstreet.com    |  ~ Sam Goldwyn



More information about the Comp.unix.xenix.sco mailing list