Pls correct the "if" syntax


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pls correct the "if" syntax
# 1  
Old 07-29-2006
Java Pls correct the "if" syntax

Iam a learner of UNIX. Fortunately I got this site.
I want to check the file for its existance.

if [ ! -f filename_$Odate.dat ]
echo " Not present"
else
echo "Present"
fi

The above code is working fine. But I also want to check for the files which are compressed.
I tried the following code and it is not working:

if [ ! -f filename_$Odate.dat -o filename_$Odate.dat.Z ]
echo " Not present"
else
echo "Present"
fi

Please correct the syntax.

Last edited by ganapati; 07-30-2006 at 04:17 AM..
# 2  
Old 07-29-2006
if [ ! -f filename_$Odate.dat || filename_$Odate.dat ]

try this one....
# 3  
Old 07-29-2006
Add another '-f' test.

Code:
if [ ! -f filename_$Odate.dat -o -f filename_$Odate.dat ] ; then
echo " Not present"
else
echo "Present"
fi

# 4  
Old 07-30-2006
Java One more Option for "if"

I found one more logic, which will find both compressed and uncompressed file. Could any one check and advise me please?

File_Name=`ls -lrt filename_$Odate.dat.* | tail -1`
if [ ! -f $File_Name ]
echo " Not present"
else
echo "Present"
fi

Many Thanks and Regards,
Ganapati
# 5  
Old 07-30-2006
This will list output in long format.

Quote:
File_Name=`ls -lrt filename_$Odate.dat.* | tail -1`
You just want the filename.

Instead, use ' ls -1rt '.
# 6  
Old 07-31-2006
Bug

Thanks to all.
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. OS X (Apple)

"Permission denied" when trying to SSH my iPhone though password is correct

Hi, I hope this is the correct section in the forum to post as I'm trying to SSH from my MacBook. I was looking to see whether ssh on my jailbroken iPhone 6s (10.3.1) still works fine and was following this old reddit guide. I installed OpenSSH&OpenSSL from Cydia and changed the password using... (7 Replies)
Discussion started by: hss1
7 Replies

3. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

4. Shell Programming and Scripting

print system("uname -n") is not working .Pls help

awk '{if ($1 == "State:" && $2 == "Okay") {print system("uname -n")}}' ---------- Post updated at 01:20 AM ---------- Previous update was at 01:19 AM ---------- it is printing uname -n instead of printing the output of the command (8 Replies)
Discussion started by: rishiraaz
8 Replies

5. Shell Programming and Scripting

Compare file names and select correct elements to include in "for each loop"

Hi everyone, I`ll try to be most clear I can explaining my help request. I have 2 folders Folder A-->This folder receives files through FTP constantly Folder B-->The files from Folder A are unzipped and then processed in Folder B Sometimes Folder A doesn`t contain all... (2 Replies)
Discussion started by: cgkmal
2 Replies

6. Shell Programming and Scripting

Need Correct syntax for "if" in UNIX

I am trying to write a simple if statement but that driving me crazy:eek: with syntactical erorrs. This is what I managed to come up #!/bin/ksh USERNAME=`whoami` if ;then echo " you have logged in as report user" elif ; then echo " you have logged in as extract user" else " I dont... (3 Replies)
Discussion started by: kkb
3 Replies

7. Shell Programming and Scripting

What's the correct syntax to use "if-else"

hi, i'm a beginner in Linux, actually i use andLinux, i have a data file with 11 columns 170 N SER T 25 56.962 42.701 -133.423 1.00 87.04 N 171 CA SER T 25 57.076 41.790 -132.280 1.00 86.65 C 172 C SER T 25 57.766 40.492 -132.692 1.00 87.52 C 173 O SER T 25 58.653 39.988 -131.992 1.00 86.59... (6 Replies)
Discussion started by: elsa
6 Replies

8. UNIX for Dummies Questions & Answers

correct syntax for using "grep regex filename" ?

I'm trying to grep a long ls by looking at the beginning of each filename for example: Many files begin with yong_ho_free_2005... Many files begin with yong_ho_2005... I can't just use "grep yong_ho" otherwise It'll display both files. So I'm trying to use a regex but my syntax is wrong. ... (2 Replies)
Discussion started by: yongho
2 Replies
Login or Register to Ask a Question