file typ of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting file typ of a file
# 1  
Old 07-31-2007
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
# 2  
Old 07-31-2007
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:

Code:
#
# 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.
# 3  
Old 07-31-2007
i tested your function and it works without problems.
thank you, it is very helpful for me
tom
# 4  
Old 07-31-2007
Quote:
Originally Posted by Perderabo
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:

Code:
#
# 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 $?
	}


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

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

# 5  
Old 07-31-2007
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I add a log file path to a vi file to monitor all the changes made to the file?

I'm curious to know how do I add an empty log file (test1.log) to an existing text file to monitor all the changes made to a.txt. Is this expression export PATH=$PATH:/home/test1.log right to be added to the text file a.txt? (5 Replies)
Discussion started by: TestKing
5 Replies

2. UNIX for Beginners Questions & Answers

Output file name and file contents of multiple files to a single file

I am trying to consolidate multiple information files (<hostname>.Linux.nfslist) into one file so that I can import it into Excel. I can get the file contents with cat *Linux.nfslist >> nfslist.txt. I need each line prefaced with the hostname. I am unsure how to do this. --- Post updated at... (5 Replies)
Discussion started by: Kentlee65
5 Replies

3. Shell Programming and Scripting

ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped. First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes. I have a text file (file_source) of terms, each line... (3 Replies)
Discussion started by: Brusimm
3 Replies

4. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

5. Shell Programming and Scripting

Systemd errors of missing file “No such file or directory” inspite of file being present

The contents of my service file srvtemplate-data-i4-s1.conf is Description=test service for users After=network.target local-fs.target Type=forking RemainAfterExit=no PIDFile=/data/i4/srvt.pid LimitCORE=infinity EnvironmentFile=%I . . . WantedBy=multi-user.target (0 Replies)
Discussion started by: rupeshkp728
0 Replies

6. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

7. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

8. Shell Programming and Scripting

Split File by Pattern with File Names in Source File... Awk?

Hi all, I'm pretty new to Shell scripting and I need some help to split a source text file into multiple files. The source has a row with pattern where the file needs to be split, and the pattern row also contains the file name of the destination for that specific piece. Here is an example: ... (2 Replies)
Discussion started by: cul8er
2 Replies

9. Solaris

Before I delete any file in Unix, How can I check no open file handle is pointing to that file?

I know how to check if any file has a unix process using a file by looking at 'lsof <fullpath/filename>' command. I think using lsof is very expensive. Also to make it accurate we need to inlcude fullpath of the file. Is there another command that can tell if a file has a truely active... (12 Replies)
Discussion started by: kchinnam
12 Replies

10. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies
Login or Register to Ask a Question