LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-21-2008, 05:06 AM   #1
hukcjv
LQ Newbie
 
Registered: Mar 2008
Posts: 2

Rep: Reputation: 0
Testing for member exists in shell script


Using the following to test existence of member in a directory

export MEMBER=HHO500
export TEMPDIR=/usr/temp

if [ -e $TEMPDIR/$MEMBER.* ]
then
rm -f $TEMPDIR/MEMBER.*
fi

There could be a number of members in the TEMPDIR with the same MEMBER name. If a single entry then no probmem, but if several I get

+ '[' -e /usr/temp/HEO005.cbl /usr/temp/HEO005.dir /usr/temp/HEO005.idy ']'
testjob1.sh: line 11: [: too many arguments

This code was picked from our aix box which works fine.

I did find a thread similar here and the suggestion was that the test condition worked differently under linux.

any suggestions

thanks

first post so hope its gone to the correct forum
 
Old 03-21-2008, 05:58 AM   #2
smus
Member
 
Registered: Nov 2005
Location: Turkey
Distribution: Suse
Posts: 104

Rep: Reputation: 16
hi,

may be you can count the items with ls HHO500 | wc -l then you can check the result if the number is bigger than your decide you can delete it.

SM
 
Old 03-21-2008, 06:21 AM   #3
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Quote:
Originally Posted by hukcjv View Post
Using the following to test existence of member in a directory

export MEMBER=HHO500
export TEMPDIR=/usr/temp

if [ -e $TEMPDIR/$MEMBER.* ]
then
rm -f $TEMPDIR/MEMBER.*
fi

There could be a number of members in the TEMPDIR with the same MEMBER name. If a single entry then no probmem, but if several I get

+ '[' -e /usr/temp/HEO005.cbl /usr/temp/HEO005.dir /usr/temp/HEO005.idy ']'
testjob1.sh: line 11: [: too many arguments
[...]

any suggestions
[...]
Do you really need the test?

Code:
rm -rf "$TEMPDIR/$MEMBER".*
 
Old 03-21-2008, 09:06 AM   #4
simplicissimus
Registered User
 
Registered: Mar 2008
Posts: 104

Rep: Reputation: 15
branch conditions

If you want delete every reference to that username than, as already suggested,
Code:
rm -rf "$TEMPDIR/$MEMBER".*
will do the job.

If there is some special reason to distinguish between files and directories or links or permissions then extend your branch condition with any of these:

Code:
-e	File exists	 	
-f	File is a regular file	 	 	 
-d	File is a directory	 	
-h	File is a symbolic link	 	
-L	File is a symbolic link
-s	File is not zero size
-r	File has read permission
-w	File has write permission
Hope this helps,
Regards,
SIMP

Fedora Development

Last edited by simplicissimus; 04-02-2008 at 04:52 AM.
 
Old 03-22-2008, 07:38 AM   #5
hukcjv
LQ Newbie
 
Registered: Mar 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks

Hey

Thanks for the prompt reply. I did in the end perform the rm -rf $TMPDIR/$MEMBER*.*.

We had written this script to run on AIX as part of a porting piece, and the real reason for the Q was to check whether it was our coding or just difference between the OS.

We will factor that in out scripts.

Once again thanks for the reply
 
Old 03-22-2008, 09:35 AM   #6
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Quote:
Originally Posted by radoulov View Post
[...]

Code:
rm -rf "$TEMPDIR/$MEMBER".*

This should be safer:

Code:
rm -r "${TEMPDIR?}/${MEMBER?}".*
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script to Delete line if pattern exists topcat Programming 22 08-23-2011 04:58 AM
Shell script problem. check file already exists sinister1 Linux - Server 8 11-20-2007 03:13 PM
Shell not seeing file that exists? rsheridan6 Linux - General 10 06-17-2007 03:45 PM
Shell Script Testing for Directory Non-existance binarybob0001 Programming 5 04-01-2006 02:15 PM
shell script for stress testing blackzone Programming 3 09-14-2004 12:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration