Verifying if a file exist (script shell)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Verifying if a file exist (script shell)
# 1  
Old 10-20-2012
Verifying if a file exist (script shell)

Hello,

This is my code:
Code:
nb_lignes=`wc -l $1 | cut -d " " -f1`
 for i in $(seq $(($nb_lignes - 1)) )
 do
 machine=`head $1 -n $i | tail -1`
 machine1=`head $1 -n $nb_lignes | tail -1`
 ssh  root@$machine -x " scp /home/file.txt root@$machine1:/home && rm -r /home/file.txt"
 done
 fi

I'd like to verify if file.txt exist in such machine before the scp et rm ,Please i ask how can i modify this script ?

Thank you.

Last edited by Scott; 10-20-2012 at 08:19 AM.. Reason: Code tags, please...
# 2  
Old 10-20-2012
Hi ,
You can do that using

Code:
if [ -f file.txt ]; then

Regards,
Haris
# 3  
Old 10-20-2012
Did you mean ?!

Code:
ssh  root@$machine -x " if [ -f file.txt ] scp /home/file.txt root@$machine1:/home && rm -r /home/file.txt"


Last edited by Scott; 10-20-2012 at 09:18 AM.. Reason: CODE TAGS
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to check files if exist else touch the file

Hi All, Thanks in Advance I wrote the following code if then echo "version is 1.1" for i in "subscriber promplan mapping dedicatedaccount faflistSub faflistAcc accumulator pam_account" do FILE="SDP_DUMP_$i.csv" echo "$FILE" ... (5 Replies)
Discussion started by: aealexanderraj
5 Replies

2. Shell Programming and Scripting

Propagate exist status from a child shell script to a parent.

Hi All, I have a parent shell script A and a child shell script B. 1). If a command i.e a mysqdump fails in shell script B fails then I trap the error with this code if ] then func_exit "Failed to export the cleaned DB1.${MYDBNAME} database to the ${MYARCHIVEDIR} directory"... (1 Reply)
Discussion started by: daveu7
1 Replies

3. Shell Programming and Scripting

Verifying oracle connection from shell script

Hi, Oracle 9.2 Solaris 10 From the shell script ........How can we verify whether oracle connection is successful or not ? Shell script will prompt the user to enter the oracle schema username, password and tns name. So, *how to verify whether oracle connection has been established or... (14 Replies)
Discussion started by: milink
14 Replies

4. Shell Programming and Scripting

file does not exist or unreadable in an FTP script

I will appreciate any help with this... I have a file in this directory that looks like: this 07210900.SUP, I am getting the following error: Activities for Tue Jul 21 07:29:14 EDT 2009: File 07210900.SUP does not exist or unreadable End of activities The idea is to capture the file in... (1 Reply)
Discussion started by: rechever
1 Replies

5. UNIX for Dummies Questions & Answers

Expect Script - While file exist

Hi: Can anyone help me with the while loop in expect script. Following is my scenario: 1. one of my process is running and looking for a file 2. as soon as the file is dropped the proces then processes the file and creates a control file ".ctl" to indicate end of process for that file ... (5 Replies)
Discussion started by: skal5
5 Replies

6. Shell Programming and Scripting

Verifying SSH connectivity in a script

Hi everyone, I'm trying to write a korne shell script that takes the IP address of a remote server as an argument and logs into that server if RSA key has been setup properly, otherwise exits if RSA key is not set for that server. I don't want the script to get stuck with Password prompt... (1 Reply)
Discussion started by: omd
1 Replies

7. Shell Programming and Scripting

Verifying if the shell command executed or not?

Hi, I am working on a shell script that would verify if the mount command has executed or not. So , i have been doing this. mount /dev/cdrom /mnt/cdrom echo "$?" if ; then echo " Mount succesful" else echo " Mount unsuccessful" fi (3 Replies)
Discussion started by: eamani_sun
3 Replies

8. UNIX for Dummies Questions & Answers

How to check path exist or not in UNIX shell script

UNIX Shell Script I'm in /home/suneel dirctory in that directory need to check for a path for example com/src/resources If Path exists need to copy the files from one directory If path not exist need to create the folders and copy the files UNIX shell script help required (3 Replies)
Discussion started by: suneelc
3 Replies

9. UNIX for Dummies Questions & Answers

Need Script to check file exist and compare

I need a script that will check for the existence of new files that FTP'd in the morning, results go to log file. The 2nd step is to compare the new file with the previous days file. If the new file size is 30% or more smaller in size then previous day this needs to also be sent to log. This... (1 Reply)
Discussion started by: rbknisely
1 Replies

10. Shell Programming and Scripting

Have a shell script check for a file to exist before processing another file

I have a shell script that runs all the time looking for a certain type of file and then it processes the file through a series of other scripts. The script is watching a directory that has files uploaded to it via SFTP. It already checks the size of the file to make sure that it is not still... (3 Replies)
Discussion started by: heprox
3 Replies
Login or Register to Ask a Question