The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to check directory exist on servers weonpc Shell Programming and Scripting 2 03-06-2008 06:29 AM
Check if certain files exist in a directory, if not add name to a textfile SunnyK Shell Programming and Scripting 1 02-07-2008 09:21 AM
how to check if directory/file exist using c/c++ steven88 High Level Programming 2 01-03-2006 02:55 AM
how to check if directory/file exist using c/c++ steven88 Shell Programming and Scripting 1 01-02-2006 10:45 PM
how to check if the file exist or not? gusla UNIX for Dummies Questions & Answers 3 03-27-2002 10:56 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-02-2005
ust ust is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 98
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
  #2 (permalink)  
Old 07-02-2005
sssow sssow is offline
Registered User
  
 

Join Date: Aug 2001
Posts: 179
You can directly do it with if

if [ -d abc ]
then
echo Directory found
else
echo Directory not found
fi
  #3 (permalink)  
Old 07-02-2005
ust ust is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 98
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 (permalink)  
Old 07-02-2005
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
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 ]
  #5 (permalink)  
Old 07-02-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Even more concise (in ksh)

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

Cheers
ZB
  #6 (permalink)  
Old 07-04-2005
mbb mbb is offline
Registered User
  
 

Join Date: Aug 2001
Location: UK
Posts: 104
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
  #7 (permalink)  
Old 09-22-2008
Sara-sh Sara-sh is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 9
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0