LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-22-2011, 02:00 PM   #1
zimbot
Member
 
Registered: Nov 2005
Location: cincinnati , ohio . USA
Distribution: ubuntu , Opensuse , CentOS
Posts: 122

Rep: Reputation: 17
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
 
Old 02-22-2011, 04:40 PM   #2
hogar.strashni
Member
 
Registered: Dec 2007
Distribution: cp6
Posts: 44

Rep: Reputation: 2
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.

Last edited by hogar.strashni; 02-22-2011 at 05:00 PM.
 
Old 02-22-2011, 05:14 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.

Last edited by theNbomr; 02-22-2011 at 05:17 PM.
 
  


Reply

Tags
shell script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] line that checks if dir "TEST" exists. If so, nothing, of not mkdir Mike_V Programming 5 08-26-2010 08:44 AM
Web Root Dir Change and New Dir Added issue in CentOS 5 Ginnyz Linux - Server 2 06-01-2009 08:29 PM
Command to display /dir, /dir/sub, /dir/sub/files knockout_artist Linux - Newbie 9 10-25-2007 02:57 PM
Bash Script test for empty dir uopjohnson Linux - Software 3 10-07-2005 06:45 PM
Home dir dosen't appear to exist when booting new kernel JLDohm Linux - Newbie 13 02-01-2005 06:31 PM


All times are GMT -5. The time now is 11:06 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration