how to check existence of any file in csh script?
Asked Answered
C

2

14

For checking the existence of any file in csh script I am using

if [ -f /var/opt/temip/conf/.temip_config ]

but I am getting below error

if [ -f /var/opt/temip/conf/.temip_config ]

if: Expression Syntax.

Can anyone tell me how to do this?

Conservatoire answered 7/1, 2015 at 7:55 Comment(3)
That is not valid Csh syntax at all. Which is a good thing, because you should probably not be doing your scripting in Csh. Your syntax is fine for sh which is probably what you should be using anyway. Without the context of the rest of your script, we can't really tell whether changing the first line to #!/bin/sh would be feasible, though.Yarkand
@Yarkand can you pls simply tell me how to check file existence in csh script?Conservatoire
Give up csh. Code your script in POSIX sh (for portability), or in GNU bash or in Python or in zshAftersensation
G
18

From the manpage:

f
    Plain file

You use it with a if statement:

if ( -f file.txt ) then
    echo Exists
else
    echo No such file
endif

Based on this question and your previous question, you seem to be rather clueless as to how csh syntax works, since you keep using POSIX shell syntax. I would strongly suggest that you either familiarise yourself with the csh syntax, or just use a POSIX shell (which is probably better for scripting anyway).

Germen answered 7/1, 2015 at 10:56 Comment(0)
L
16

In CShell for checking of the existence of a file use -e option

The name of the file does not have to be "hard coded" into the if statement, but may be a parameter like this:

 if (-e "$filePath") then

Here is a full list of the Cshell file queries.

-e file           file merely exists (may be protected from user)
-r file           file exists and is readable by user
-w file           file is writable by user
-x file           file is executable by user
-o file           file is owned by user
-z file           file has size 0
-f file           file is an ordinary file
-d file           file is a directory
Lawless answered 7/1, 2015 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.