Help with script checking for a file in various servers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with script checking for a file in various servers
# 1  
Old 02-24-2011
Help with script checking for a file in various servers

I am trying to write a script that checks whether or not, a file exists on multiple servers.
My code / logic so far is:

HTML Code:
#!/usr/bin/ksh

print "Enter File name to be checked"
read MYFILE

ssh server1 "
              cd /var/opt/logs ;

              if [ -f $MYFILE ]
              then
                  print "$MYFILE exists!"
                  else
                  print "$MYFILE does not exist"
                  fi
              "
ssh server2 "
              cd /var/opt/logs ;

              if [ -f $MYFILE ]
              then
                  print "$MYFILE exists!"
                  else
                  print "$MYFILE does not exist"
                  fi
              "
It's not working.... any help please?
# 2  
Old 02-24-2011
Hi.

It works for me. What exactly isn't working? Error meesages / output / says "file doesn't exist" when it does / ... ?
# 3  
Old 02-24-2011
Code:
#!/usr/bin/ksh

print "Enter File name to be checked"
read MYFILE

for server in server1 server2
do 
  ssh $server " 
              cd /var/opt/logs ;

              if [ -f $MYFILE ]
              then
                  print \"$MYFILE exists!\"
                  else
                  print \"$MYFILE does not exist\"
                  fi
              "
done

# 4  
Old 02-28-2011
Thanks ... will try and update Smilie
# 5  
Old 02-28-2011
Yep.. putting in a for loop will help...
Also for good coding standards try some variable like MYDIR="/var/opt/logs".

And then check like
Code:
if [ -f ${MYDIR}/${MYFILE} ]
then
  print \"${MYDIR}/${MYFILE} exists!\"
else
  print \"${MYDIR}/${MYFILE} does not exist\"
fi

Its not a good practice to hard code filenames and directories inside the core of the code where you are doing checks, data manipulations kind of stuff.

Moderator's Comments:
Mod Comment It's also good practice to use code tags Smilie

Last edited by Scott; 02-28-2011 at 12:45 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command script for checking a file existence

Hello, I have a directory where sometimes appear a certain file name - and I'd like to be notified by email when that happens... so what command or script I may use? e.g. if there's a file named "adam" in the directory named "dir1" then send a mail to "abc@abc.com".. it needs to permanently... (5 Replies)
Discussion started by: netrom
5 Replies

2. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

HI, I have a file serverlist in that all host names are placed. i have written a small script #./testping #! /bin/bash for i in `cat serverlist` do ping $i >> output.txt done so now it creates a file output.txt till here fine.. now each time i run this script the output file... (4 Replies)
Discussion started by: madhudeva
4 Replies

3. Shell Programming and Scripting

File checking script need help

Hi, Gurus, I need a scripts to check specified file if it exists or not at certain time (say every month between 5th and 7th). if file exists do something otherwise do another thing. can anybody help this? Thanks in advance :wall: (3 Replies)
Discussion started by: ken002
3 Replies

4. UNIX for Advanced & Expert Users

Checking OS Type on Servers

is there a way to do this through snmp? i'm writing a shell script that will be run against several servers, but when i run the script, the most i get back from snmp is information that is simple. the snmp in the script tells me if the box is linux or solaris. but it doesn't tell me the... (7 Replies)
Discussion started by: SkySmart
7 Replies

5. Shell Programming and Scripting

Script check for file, alert if not there, and continue checking until file arrives

All, Is there a way to keep checking for a file over and over again in the same script for an interval of time? Ie If { mail -user continue checking until file arrives file arrives tasks exit I don't want the script to run each time and email the user each time a file... (4 Replies)
Discussion started by: markdjones82
4 Replies

6. Shell Programming and Scripting

script to get all ip addresses of servers into a file

Hi all i need to create a script that pings every server in my range (0-254) adn then returns the values to a file? can anyone please help. i am working in the tcsh ( and yes i know how to ping ) but i dont know how to ping them all in one script without copying and pasting a 254 times? ... (1 Reply)
Discussion started by: brian112
1 Replies

7. UNIX for Dummies Questions & Answers

Checking file sizes in script

Hi, I'm trying to check a filesize within a script and then excute a relevant action. An example is below: if then rm $filename rm $filename2 elif then rm $filename2 fi Basically if $filename2 has a filesize of 0 then I want both files to be removed, but... (6 Replies)
Discussion started by: chris01010
6 Replies

8. Shell Programming and Scripting

.sh file syntax checking script

This isn't a question--its a solution! Below is a script that I wrote for my own script file development which does what the title says. Its the closest that you can get to compiling what are otherwise purely interpreted script files. I offer it here simply for the benefit of anyone else writing... (12 Replies)
Discussion started by: fabulous2
12 Replies

9. Shell Programming and Scripting

Simple file checking script

Hi, I have a really, what I hope is, simple question. I'm looking for a simple way to see whether a file exists or not and then perform an action based on whether it exists or not. An example of what I tried is as follows: if then { echo "File mysql exists" ... (1 Reply)
Discussion started by: _Spare_Ribs_
1 Replies

10. AIX

Script to ping servers in a file

I would like to ping a list of servers in a text file. Can anyone help? (1 Reply)
Discussion started by: gbarkhor
1 Replies
Login or Register to Ask a Question