LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   test for a dir exist (https://www.linuxquestions.org/questions/programming-9/test-for-a-dir-exist-864352/)

zimbot 02-22-2011 02:00 PM

test for a dir exist
 
friends,

i am trying to test if a certain dir might exist.
the dir name will always be in the form of NNNNN other name w spaces.
where nnnnnn is a 5 digit jobnumber
example :specifically if a usb drive has been mounted under
/media/31499 my project.
I figure if i get the 31499 from the user - then I can append a *

what i am doing is making a script that will allow users to tar a usb drive (video , final cut pro stuffism ) to an LTo4 data tape.

I would like to show you 2 scripts this 1st one is my TEST FOR DIR script that -- if it worked, would be incorporATED into the larger BACKUP SCRIPT

this is the test for dir-------------(start test for dir
#!/bin/sh
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~# vers
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~# pps production backUP
##-----------< this is test-for dir4.sh
##-----------<
#######################################################
###----# get JobNum # enter w spaces
echo “Please provide the jobNum -----! 5 digits NO SPACES !”
read jobNum
echo “Thanks…Processing Your ${jobNum} Now”
###----------------># start is there size , is theer anything mounted
##cd /media/$JobNum*
dir="/media/$jobNum*"
##echo ${PWD#${PWD%/*/*}/}
echo "dir test is for $dir"
###if [ $dir -ne 1 ]
##then
## echo "Usage: $0 {dir-name}"
## exit 1
##fi

if [ -d "$dir" ]
then
echo "$dir directory exists!"
else
echo "$dir directory not found!"
fi
--------------test for dir end

what confuses me is I can cd to /media/31499* and find myself in /media/31499 my project

Could it be the spaces in the name?
should i try to write a file and test if that file exists?
BUT i cannot write to the usb drive ...it is HFS+ journaled.
the reason for this test is so the dude who has not plugged in the usb drive gets a reminder.


----------)-)-------( the main back up script
here i am getting a jubNumber
making a listing
testing that there is a tape in the lto
i wish to test that the usb Drive is on and mounted.
sending an email
and , of course backing up the usb drive to lto4 tape

#!/bin/sh
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~# vers9k; 2.22.2011 js
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~# pps production backUP
##-----------< assumes a chgrp users of /media
##-----------< assumes a 755 dir of /home/ingex/pps_list
#######################################################

###----------------># start is there a tape test
sudo mt -f /dev/st0 status | grep error
if [[ $? -ne error ]]
then
echo " errorWORD found you need a tape "
exit
else
echo " all is well-- you got a tape"
fi
###----------------># end is there a tape test
#
####----# get JobNum # enter w spaces
echo “Please provide the jobNum -----! 5 digits NO SPACES !”
read jobNum
echo “Thanks…Processing Your ${jobNum} Now”
###---i need a date
start=$(date)
Ntime=$(date)
CalcStart=$(date +%s)
echo $Ntime > /home/ingex/pps_list/${jobNum}.txt

#---
#cd /media/*
cd /media/
#echo "dir is: `pwd`"

####---# make a txt file - later it might get upld--dunno
####-for the tape the 00index.txt
echo $jobNum > /media/00index.txt
echo $Ntime >> /media/00index.txt
ls -Rlh >> /media/00index.txt
#####-------TEST for usb drive Source goes dir here tbd-----#
###-for big saver
ls -Rlh >> /home/ingex/pps_list/${jobNum}.txt
ls -R > /home/ingex/pps_list/${jobNum}_short.txt
####--------------------------------------# the do
####----# make tape , assume 0
sudo tar --totals -H pax -cvf /dev/st0 *
###-----# start happytest
if [[ $? -ne 0 ]]
then
echo " backUP halt or error "
exit1
else

############
end=$(date)
CalcEnd=$(date +%s)
echo $end >> /home/ingex/pps_list/${jobNum}.txt

diff=$(( $CalcEnd - $CalcStart ))
## my dif effort needs work - need to do min only
#echo "Task Duration "$diff >> /home/ingex/pps_list/${jobNum}.txt

####-remove the 00index.txt
rm /media/00index.txt
####----------------------------------------------------# end do
####---------------------------------------------# Start mail
# email subject
SUBJECT="The Back Up Job Number: "$jobNum

# Email To ?
########### group
EMAIL="bu@mydomain.com"
########### devel
#EMAIL="jme@mydomain.com"

# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"
echo "saver drive backed up to tape"> $EMAILMESSAGE
echo "the job: "$jobNum >>$EMAILMESSAGE
echo "............................................... " >>$EMAILMESSAGE
echo "Started: "$start >>$EMAILMESSAGE
echo " " >>$EMAILMESSAGE
echo "Finished: "$(date)>>$EMAILMESSAGE
echo "--------------------------------- " >>$EMAILMESSAGE
echo "total seconds "$diff >>$EMAILMESSAGE
echo "--------------------------------- " >>$EMAILMESSAGE
echo "machine = atkov ">>$EMAILMESSAGE
hour=`echo $diff/3600 | bc`
### ok the above does not work for sec to hours ###
echo " hours r; "$hour >>$EMAILMESSAGE
# send an email using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
#echo "subject is " $SUBJECT
#####----------------------------------------------# end mail

echo "*********************************************************"
echo " . "
echo "backup " ${jobNum} " DONE "
echo " . "
echo "Total seconds It took "$diff
####- eject tape ** the whole eject merged w a tell maybe
#sudo mt -f /dev/st0 eject
#mt -f /dev/st0 eject
echo "--------------------------------------------------::good"
fi
###-----# end happytest
echo "eject with a 88888 then Label and be sure to Slide the RED for copy PROTECT NOW"

-----------------( end the main script

thanks

hogar.strashni 02-22-2011 04:40 PM

Quote:

what confuses me is I can cd to /media/31499* and find myself in /media/31499 my project
this outcome is perfectly logical. 'cd' will change to first directory specified, and * will usually append in the alphabetical order. This way 'space' character is of the greatest priority. I'm not quite sure what you are after, but I'll give it a try.

I presume you want to access another(the only other) directory in the same directory where your project is. This other directory name starts the same way your project name does. You may exclude your project directory using 'grep' and 'ls':
Code:

cd `ls | grep -v 31499\ my\ project | grep 31499*`
'grep' commands will find all files within listed directory(output of 'ls') that starts with 31499, but doesn't have "31499 my project" in it's name. cd will change directory to first directory listed in the output. There must be more elegant way, but I think this could work, if that's what you wanted.

Pozdrav :)

PS. Oh yeah, if you are only trying to check whether directory exist or not, maybe you may use grep's -c option, which will display number of files(directories) matching the criteria, or use the return value of 'grep' as mentioned in the man pages:
Quote:

DIAGNOSTICS
Normally, exit status is 0 if selected lines are found and 1 otherwise. But the exit sta-
tus is 2 if an error occurred, unless the -q or --quiet or --silent option is used and a
selected line is found.

theNbomr 02-22-2011 05:14 PM

To see whether a directory exists, use find (assuming you always need to look in the /media directory tree):
Code:

find -type d -name "/media/${jobNum}*"
Feed the output of that to wc -l to see if the specified $jobNum produces a unique directory name.

You have a lot of what seems to be source code interspersed with what seems to be the text of your question, but it is very difficult to distinguish each from the other.

--- rod.


All times are GMT -5. The time now is 07:37 AM.