LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Testing for member exists in shell script (https://www.linuxquestions.org/questions/linux-newbie-8/testing-for-member-exists-in-shell-script-629622/)

hukcjv 03-21-2008 05:06 AM

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

smus 03-21-2008 05:58 AM

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

radoulov 03-21-2008 06:21 AM

Quote:

Originally Posted by hukcjv (Post 3095776)
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".*

simplicissimus 03-21-2008 09:06 AM

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

hukcjv 03-22-2008 07:38 AM

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

radoulov 03-22-2008 09:35 AM

Quote:

Originally Posted by radoulov (Post 3095808)
[...]

Code:

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


This should be safer:

Code:

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


All times are GMT -5. The time now is 09:26 AM.