file typ of a file

how can i detect the file typ of a file ?

the command "file" is not so useful because it shows only strings (in the example in GERMAN), here some examples :

Test for normal file with ASCII strings:

file with size zero:
create of file: touch touch-zero
test of file: file touch-zero
output of file: touch-zero: leer

file with one line:
create of file: echo line > touch-one-line
test of file: file touch-one-line
output of file: touch-one-line: Ascii-Text

character device-file :
test of file: file /dev/null
output of file: /dev/null: zeichenorientiert (3/2)

test of file: file /dev/async
output of file: /dev/async: zeichenorientiert (101/0)

test of file: file /dev/rac/bt6lto3picker
output of file: /dev/rac/bt6lto3picker: zeichenorientiert (231/11534336)

test of file: file /dev/vg00/rlvol1
output of file: /dev/vg00/rlvol1: zeichenorientiert (64/1)

block device-file :
test of file: file /dev/vg00/lvol1
output of file: /dev/vg00/lvol1: blockorientiert (64/1)

so i think it is better to check the file typ with the command "test" :

test -f , test -b , test -c and so on.

but how can i detect if the file exists ( at that time i don't know the file typ)

test -s is not the recommended way ?

test -s /dev/null => isn't true

test -s touch-zero => isn't true

test -s touch-one-line => is true

after the first check, i can begin to check the file typ of the file.

i think it is better to create a little script to to cover the exceptions.

does anyone have some useful tips.

regards,Tom

In one ksh script I wrote, I needed a portable way to detect if a file exists. After trying a few things I settled on:

#
# Function to determine if a file system object exists
#
# neither -a nor -e is really portable  8(
  
exists() {
	[[ -f $1 || -d $1 || -L $1 || -p $1 || -S $1 || -b $1 || -c $1 ]]
	return $?
	}

Another idea is to run "ls -l" on the object.

i tested your function and it works without problems.
thank you, it is very helpful for me
tom

That is not portable. [[ ... ]] is non-standard syntax.

There is no need for return $?; the exit status of the last command is automatically returned.

Remember that I wrote a portable ksh script, I did not intend it to work on all posix shells. I have run it on Solaris, HP-UX, and Windows XP. It has been downloaded hundreds of times and been run on several other OS's. However, it depends on several features not available in posix shells,for example, ksh co-processes. I would not attempt a script like this in any shell besides ksh. This includes bash and zsh.

You do have a point about the return statement though. I probably should have written it that way.