The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
check if remote file exists hcclnoodles Shell Programming and Scripting 2 08-27-2008 06:53 PM
Check File Exists and compare to previous day file script rbknisely Shell Programming and Scripting 3 02-07-2008 11:53 AM
check if file exists on remote system ? hcclnoodles Shell Programming and Scripting 2 10-26-2006 05:08 AM
check if exists a .ZIP file and unzip it using ftp DebianJ Shell Programming and Scripting 1 05-05-2005 05:46 PM
perl ftp check file exists methos Shell Programming and Scripting 2 06-18-2003 09:21 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-19-2008
raoscb raoscb is offline
Registered User
  
 

Join Date: May 2008
Posts: 16
Question Check if a file exists with certain prefix

Hi guys,
How would i check a file exists with certainprefix? i have a directory with some files:
ABC1
ABC2
ABC3
etc..

and want to do:
please note i am using the korn shell environment.As when i gone through some stuff on then net i came to know some of the options will work differently based on the working shell environment.tht's wy i am specially mentioning my envrionment.


Code:
if [file exists with prefix ABC*] then
do something
else
do something else
fi

Any one could help me on this!!!!!


Advanced Thanks
Narasimharao.
  #2 (permalink)  
Old 08-19-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,406
use find command.. go through man page..try to do it your self
  #3 (permalink)  
Old 08-19-2008
redoubtable redoubtable is offline
Registered User
  
 

Join Date: Aug 2008
Location: Portugal
Posts: 242
vidyadhar's solution is also a good way to go but if you want something in ksh:


Code:
#!/bin/ksh

if [ -f ABC* ]
then
	echo file exists.
else
	echo not
fi

  #4 (permalink)  
Old 08-19-2008
raoscb raoscb is offline
Registered User
  
 

Join Date: May 2008
Posts: 16
Thumbs down

Hi redoubtable,
the below code is working for only exact file names(i mean the -f option).could you please tell me the way for finding files with certain prefix.I know the find command.but i need another way by using simple if command and along with their options.

Hi can any one please help me on this..........

Advanced Thanks
Narasimharao.
  #5 (permalink)  
Old 08-19-2008
redoubtable redoubtable is offline
Registered User
  
 

Join Date: Aug 2008
Location: Portugal
Posts: 242
The code I gave you works for filenames starting with "ABC".
ABC1, ABC2, ABC3.

Code:
redoubtable@Tsunami ~ $ ls -l ABC*
-rw-r--r-- 1 root root 0 2008-08-19 11:42 ABC1
-rw-r--r-- 1 root root 0 2008-08-19 11:42 ABC2
-rw-r--r-- 1 root root 0 2008-08-19 11:42 ABC3
redoubtable@Tsunami ~ $


Code:
redoubtable@Tsunami ~ $ ./s.ksh 
file exists.
redoubtable@Tsunami ~ $ cat s.ksh 
#!/bin/ksh

if [ -f ABC* ]
then
	echo file exists.
else
	echo not
fi
redoubtable@Tsunami ~ $

The '*' provides the capability of matching not only exact file names but also every file name starting with ABC. Remember that you must not use " in ABC.

Example:
This is ok:

Code:
if [ -f ABC* ]

This is wrong:

Code:
if [ -f "ABC*" ]

  #6 (permalink)  
Old 08-19-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,330
You can do something like this:


Code:
for file in *; do
  prefix=echo ${file:0:3}
  if [ "$prefix" = "ABC" ];  then
    # do something
  else
    # do something else
  fi
done

Regards

Last edited by Franklin52; 08-19-2008 at 10:18 AM.. Reason: ${file:1:3} must be ${file:0:3}
  #7 (permalink)  
Old 08-19-2008
gnsxhj gnsxhj is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 14
Hi Franklin52,
what i remember is the bash counts the number of the variable from 0, isn't it ?
so, I think that should be:

Code:
prefix=echo ${file:0:3}

Regards
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0