How to check directory exist on servers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check directory exist on servers
# 1  
Old 03-05-2008
How to check directory exist on servers

There are many servers and their directory structer should be exactly the same. To check the directory path for all servers, I wrote a script.

Code:
#! /bin/ksh

ARRAY_DIRECTORIES[1]="/c/dev/custom/bin"
ARRAY_DIRECTORIES[2]="/c/dev/db/custom/src"

ARRAY_ENV[1]="remoteName200" 
ARRAY_ENV[2]="remoteName201"
ARRAY_ENV[3]="remoteName202"

integer DIR_INDEX=0
integer ENV_INDEX=0

while(($ENV_INDEX<3))
do
	ENV_INDE=`expr $ENV_INDE+1`
	
	ssh "${ARRAY_ENV[$ENV_INDE]}"

	while (($DIR_INDEX<2))
	do
		DIR_INDEX=`expr $DIR_INDEX + 1`
	
		if [ ! -d "${ARRAY_DIRECTORIES[$DIR_INDEX]}" ]
		then
			#do something
		fi
	done
done

I am new to Shell Scripting, maybe I am doing something really stupid and need your help.

The script does ssh to the server without asking for password (I put a ssh key to .ssh directory.)

Thanks
Mike
# 2  
Old 03-05-2008
expr requires space around the terms (eg `expr $ENV_INDE + 1` not `expr $ENV_INDE+1`)
You are refering to ENV_INDE and ENV_INDEX seemingly interchangably - it looks like a typo or three there
ksh isn't required for this - you are restricting yourself without needing to, just use sh
# 3  
Old 03-06-2008
If you want to go for ksh (i would recommend that, sorry, Smiling Dragon), you do not need the "`expr ....`"-constructs. Further, you terminate your loops based on your knowledge how many array entries there are (3 in your case). You could make that dynamic so you wouldn't have to change the code there if you add more entries to your arrays.

Notice that "${#arr[*]}" gives you the number of elements in the array "arr[]". Inside double brackets you can do integer math: "(( var3 = var1 + var2 ))". You have to surround the brackets with spaces, though. "((var1..." is wrong, "(( var1..." is ok.

Code:
typeset    arr[1]="first"
typeset   arr[2]="second"
typeset   arr[3]="third"
typeset   arr[4]="fourth"
typeset -i index=1

(( index = 1 ))
while [ $index -le ${#arr[*]} ] ; do
     print - "element to work on: ${arr[$index]}"
     (( index =+ 1 ))
done

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to check file exist in a directory or not

HI folks, can any one tell me how to check whether the file is existed in a directory or not . let me tell you my requirement : if the file is existed i should display a one message or else i have to send a mail .. i have the mail logic .. but I'm failed to check file existence .. please... (5 Replies)
Discussion started by: sravan008
5 Replies

2. Shell Programming and Scripting

Check if file exist

Hi, I created following script to check if file exist: #!/bin/bash SrcDir=$1 SrcFileName=$2 SrcTimePeriod=$3 if ;then echo 1 else echo 0 fi I ran it like: /apps/Scripts/FileExist.sh /apps/Inbox file1 2nd_period_2010 Even file exist at that location, my above command is... (4 Replies)
Discussion started by: palak08
4 Replies

3. Shell Programming and Scripting

Check if file exist

Hi, I am trying to create a bash script which will check if file exist then remove that file else do nothing. I have to do same process for three files in same script. I have written code for one file and trying to run it. if then rm -r /user1/abc/File1 fi When I run this code it... (1 Reply)
Discussion started by: palak08
1 Replies

4. Shell Programming and Scripting

Check if file exist

Hi Does anybody know how I can check if a file exists i.e. see bellow, this doesn't work by the way and if tried countless variations on this file1=$one/file111.txt if then echo "Present" else echo "Not present" fi result : Not present (file is already present, eventhough its... (3 Replies)
Discussion started by: gksenthilkumar
3 Replies

5. Shell Programming and Scripting

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 then echo "the directory exist !!" ... (7 Replies)
Discussion started by: ust
7 Replies

6. Shell Programming and Scripting

To check whether a directory is exist and if it is not, create it

Hi, I want to write a shell script to check whether a directory (say A) is existing in a given location and if it is not, create it. (3 Replies)
Discussion started by: sabya
3 Replies

7. Shell Programming and Scripting

Check if certain files exist in a directory, if not add name to a textfile

We recieve some logs on our windows box via FTP on a daily basis, in the same directory. I would like to check for missing logs files and add their name to a text file. Something like... Check if C:\logs\file1_currentdate exists (if not, add file1_currentdate to... (1 Reply)
Discussion started by: SunnyK
1 Replies

8. Programming

how to check if directory/file exist using c/c++

Hi there, how to check if directory/file exist using c/c++ under linux/unix. Thanks. Steven (2 Replies)
Discussion started by: steven88
2 Replies

9. Shell Programming and Scripting

how to check if directory/file exist using c/c++

Hi there,, how to check if directory/file exist using c/c++ under unix/linux? I can use access() under Window MFC. Thanks. Steven (1 Reply)
Discussion started by: steven88
1 Replies

10. UNIX for Dummies Questions & Answers

how to check if the file exist or not?

say i would like to check if the file is existed before i use rm command. How can i do it? i know if i can use find, but i would like to have a good interface (in a shell script) thks (3 Replies)
Discussion started by: gusla
3 Replies
Login or Register to Ask a Question