if test for empty and non-existing file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if test for empty and non-existing file
# 1  
Old 10-20-2005
if test for empty and non-existing file

How to write this condition in ksh?

if myfile is empty or myfile does not exist
then
do action1
fi

is this OK?

if [[ -s "$myfile" ]] -o [[ -f "$myfile" ]] then
then
do action1
fi

Thanks.
# 2  
Old 10-20-2005
Quote:
Originally Posted by GNMIKE
How to write this condition in ksh?

if myfile is empty or myfile does not exist
then
do action1
fi

is this OK?

if [[ -s "$myfile" ]] -o [[ -f "$myfile" ]] then
then
do action1
fi

Thanks.

Code:
if [[ -s "$myfile" -o -f "$myfile" ]] ; then
#do action1
fi

Look at man test.

vino
# 3  
Old 10-20-2005
Actually this did not work on HP-UX
However replacing the -o with || works

[[ cond1 || cond2 ]]

why is ksh so strange???

Last edited by GNMIKE; 10-20-2005 at 04:29 PM..
# 4  
Old 10-21-2005
From man ksh

Code:
       [[ expression ]]
              Similar to the test and [ ... ] commands (described later), with
              the following exceptions:
                o    Field splitting and file name  generation  are  not  per-
                     formed on arguments.
                o    The  -a  (and) and -o (or) operators are replaced with &&
                     and ||, respectively.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change existing variable value only user enters non-empty string.

I haven't checked any installation script to see how this is done.. But I could not even do following simple task. How do I Change existing variable value only when user enteres non-empty string. ? #!/usr/bin/ksh uid="scott" # Assign new value user enters to uid, else leave it... (7 Replies)
Discussion started by: kchinnam
7 Replies

2. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

3. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

4. Shell Programming and Scripting

test file empty

hi, I test number on a file, but I have a problem when my file is empty, because the test isn't realized , do you have a idea to realize treatment when file is empty my test script shell VALUE=5000 for i in `cat /tmp/list | awk ' FS="|" { print $2 } '` do if then ... (13 Replies)
Discussion started by: francis_tom
13 Replies

5. Shell Programming and Scripting

insert pipes for existing and non-existing records

I have a source file like this, L4058S462 34329094 F51010141TK1070000483L4058S462 34329094 0232384840 381892 182 5690 L4058S462 34329094 F51020141FIRST CLEARING, LLC A/C 3432-9094 L4058S462 34329094 F51030141JOHAN HOLMQVIST ... (1 Reply)
Discussion started by: saravanamr
1 Replies

6. Shell Programming and Scripting

Bash copy file contents into an existing file at a specific location

Hi all I need to copy the entire contents of one file into an existing file at a specific location. I know the exact line number where I need to put it. It appears I would use either sed or awk to do this, but I have been unsuccessful so far: File A line 1 line 2 line 3 line 4 ... (6 Replies)
Discussion started by: gshepherd7
6 Replies

7. Solaris

Add existing user into an existing group

Pre: no gpasswd/adduser there is just usermod can be used, also there is no -a option for usermod. How should I add a user into a group? (4 Replies)
Discussion started by: a2156z
4 Replies

8. UNIX for Dummies Questions & Answers

Getting same exit status for empty and non empty file

Hi All, I am checking for a empty input file to do some further action , but I am getting exit status 0 in both the cases , for empty and non empty file both. The value of $? is coming 0 in if part also and else part too. #!/bin/ksh if ]; then echo "data" # exit 0 echo "$?" else... (4 Replies)
Discussion started by: mavesum
4 Replies

9. Shell Programming and Scripting

folder existing and file existing

I want to look into a folder to see if there are any folders within it. If there are, I need to check inside each folder to see if it contains a .pdf file So If /myserver/myfolder/ contains a folder AND that folder conatins a .pdf file do X Else do Z I may have multiple folders and... (4 Replies)
Discussion started by: crowman
4 Replies

10. UNIX for Dummies Questions & Answers

What's wrong with this line: if ${TEST:?} ; then echo empty; fi

I want to print "empty" if $TEST is empty. TEST= if ${TEST:?} ; then echo empty; fi But it doesn't work. What's wrong? (2 Replies)
Discussion started by: meili100
2 Replies
Login or Register to Ask a Question