check the directory exist | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

check the directory exist

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-02-2005
ust ust is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 13 February 2014, 10:50 PM EST
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
check the directory exist

I have the below script to check whether directory is exist or not , now I sure the directory /abc NOT exist , but when run the script , it still pop the result is "the directory exist" , could suggest what is wrong ? thx

ll -d /abc > /dev/null 2>&1
if [ $? = 0 ]
then
echo "the directory exist !!"

else
echo "the directory not exist !!"
fi
Sponsored Links
    #2  
Old 07-02-2005
sssow sssow is offline
Registered User
 
Join Date: Aug 2001
Last Activity: 17 December 2008, 6:03 PM EST
Posts: 179
Thanks: 0
Thanked 0 Times in 0 Posts
You can directly do it with if

if [ -d abc ]
then
echo Directory found
else
echo Directory not found
fi
Sponsored Links
    #3  
Old 07-02-2005
ust ust is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 13 February 2014, 10:50 PM EST
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
thx , I know your script is simpler and good, but I also want to know why my script has problem , can point out what is wrong ? thx
    #4  
Old 07-02-2005
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
AFK
 
Join Date: Dec 2004
Last Activity: 28 July 2014, 9:39 AM EDT
Location: UK
Posts: 2,351
Thanks: 0
Thanked 6 Times in 6 Posts
Quote:
Originally Posted by ust
I have the below script to check whether directory is exist or not , now I sure the directory /abc NOT exist , but when run the script , it still pop the result is "the directory exist" , could suggest what is wrong ? thx

ll -d /abc > /dev/null 2>&1
if [ $? = 0 ]
then
echo "the directory exist !!"

else
echo "the directory not exist !!"
fi
You need to change the line in red to
Code:
if [ $? -eq 0 ]

Sponsored Links
    #5  
Old 07-02-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 24 Times in 23 Posts
Even more concise (in ksh)

( [[ -d "/this/dir" ]] && echo "The directory exists" ) || echo "Does not exist"

Cheers
ZB
Sponsored Links
    #6  
Old 07-04-2005
mbb mbb is offline
Registered User
 
Join Date: Aug 2001
Last Activity: 25 October 2010, 3:31 AM EDT
Location: UK
Posts: 104
Thanks: 0
Thanked 0 Times in 0 Posts
FYI ust:

if [ -d $dir ]
then
...
fi

is actually using the 'test' command. To find out what you can do with the test command then type: 'man test'

You may also write the above as:

if test -d $dir
then
...
fi

or even use the test command on the comand line thus:

>test -d $dir
>echo $?

Also I would recommend shell variables to be quoted in this context:

if [ -d "$dir" ] ...

to avoid any command line errors with the test command!

i.e. if $dir were emtpy and you did if [ -d $dir ], then you would really be running: 'test -d' and the test command would complain.

Hope this helps.

MBB
Sponsored Links
    #7  
Old 09-22-2008
Sara-sh Sara-sh is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 17 November 2009, 5:45 PM EST
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Check if a directory exists

Hello:

Can someone please help me figure out what is wrong here, my script does not move on to the "else" part even though there is no .ssh directory on my remote server:

$more putkey.sh
#!/bin/ksh

for server in `cat list`
do
if [ -d /.ssh ]; then
cat $HOME/.ssh/id_rsa.pub |ssh $server ' cat >> .ssh/authorized_keys && echo "key copied"'
else
cat $HOME/.ssh/id_rsa.pub |ssh $server ' mkdir .ssh && cat >> .ssh/authorized_keys && echo "key copied"'
fi
done


$./putkey.sh
Password:
sh: .ssh/authorized_keys: cannot create


The script works if the else part is executed:

$cat $HOME/.ssh/id_rsa.pub |ssh myserver ' mkdir .ssh && cat >> .ssh/authorized_keys && echo "key copied"'
Password:
key copied
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to check file exist in a directory or not sravan008 Shell Programming and Scripting 5 07-21-2011 01:57 AM
To check whether a directory is exist and if it is not, create it sabya Shell Programming and Scripting 3 09-20-2008 02:23 PM
How to check directory exist on servers weonpc Shell Programming and Scripting 2 03-06-2008 05:29 AM
how to check if directory/file exist using c/c++ steven88 Programming 2 01-03-2006 01:55 AM
how to check if directory/file exist using c/c++ steven88 Shell Programming and Scripting 1 01-02-2006 09:45 PM



All times are GMT -4. The time now is 01:08 AM.