If statement to check file transfer


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers If statement to check file transfer
# 1  
Old 11-25-2019
If statement to check file transfer

Hello Guys,

I am trying scp few file within if statement, but getting error, can someone please help to understand, what mistake I am making ?
Code:
if [ [ scp $StatusFile $user@$log02:$scppath |grep '100%' > /dev/null ] && [ scp $StatusFile $user@$log01:$scppath |grep '100%' > /dev/null ] ] ;
then
        echo " Files transferred to Log servers successfully. "
else
        echo " One or more file transfer failed over network. "
fi

I

Last edited by vbe; 11-25-2019 at 04:55 AM.. Reason: code tags please
# 2  
Old 11-25-2019
The best way to get help is if you include all your variables, since any of your variables:

Code:
$StatusFile $user@$log02:$scppath

... could be the potential source of any error.

Also, you did not include your error message. You said you were

Quote:
... but getting error....
But you did not post the exact error. Why not?
# 3  
Old 11-25-2019
ERROR is something like below. And variables doesn't have any error as without if statement, commands runs properly.
Code:
line 25: [: missing `]'
grep: ]: No such file or directory
+ echo ' One or more file transfer failed over network. '
 One or more file transfer failed over network.

# 4  
Old 11-25-2019
The if command needs a list of commands executed. man bash:
Quote:
if list; then list; [ elif list; then list; ] ... [ else list; ] fi
The if list is executed. If its exit status is zero, the then list is executed.
In the if list, you can use normal commands (e.g. scp), the [[ compound command, or the test and [ builtin commands which in turn need "conditional expressions".


You don't deploy either. [ [ is not a valid construct, and your shell will have said so. Even if using the correct [[ , it would interpret scp as a string, not as a command to be executed.
Try (untested)

Code:
if scp ...  &&  scp ... ; then ... fi

# 5  
Old 11-25-2019
I need to transfer both the files & if both gets copied correctly, it should return success message, I had tried to use suggested pattern "if scp ... && scp ... ; then ... fi" but even though both files gets copied, its executing else statement, that's why I tried to use [[

Below is what I tried, can you please take a look if I am missing something ? Must be something very silly which i am not paying attention to.


Code:
if scp $StatusFile $user@$log02:$scppath |grep '100%' > /dev/null  &  scp $StatusFile $user@$log01:$scppath |grep '100%' > /dev/null ;
then
        echo " Files transferred to Log servers successfully. "
else
        echo " One or more file transfer failed over network. "
fi


Last edited by UnknownGuy; 11-25-2019 at 06:16 AM..
# 6  
Old 11-25-2019
Did you use a single & or a double && ?
# 7  
Old 11-25-2019
I have used single '&' because I want to transfer both files irrespective of result of first transfer.

The idea is to send both files & if any of the transfer fails, trigger else statement that one of the transfer is failed or else success message.
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 Multiple conditions in IF statement?

I wish to check two conditions inside the if statement Condition 1: The two file contents should be identical // using cmp command for this. Condition 2: The two filenames should NOT be the same. This is what i did in vain. if ]; then where entry1 and entry2 are ls *.txt | while... (7 Replies)
Discussion started by: mohtashims
7 Replies

2. UNIX for Dummies Questions & Answers

Ftp - file transfer and check successful delivery

In shell script, I want to transfer files continuously and make sure transfer is successful. Please advise... how to make sure ftp transfer is successful? Also is there any option such as sftp -b where I can pass multiple put <file name> commands to ftp Thanks! (1 Reply)
Discussion started by: vegasluxor
1 Replies

3. Homework & Coursework Questions

Script to check transfer was successful

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a script that performs a post processing action: The run time environment of the script has the... (2 Replies)
Discussion started by: kenrowe
2 Replies

4. Shell Programming and Scripting

check if some file is in copy process, then transfer it

my user copy large files, and it's take 10min for file to be copied to the server (/tmp/user/ files/), if in the meantime start my scheduled script, then it will copy a part of some file to server1 my idea is to check the file size twice in a short period (1-2 seconds) of time, then compare, if... (5 Replies)
Discussion started by: waso
5 Replies

5. Shell Programming and Scripting

IF statement to check file exists

Hi All, If i run below copy command, it works absolutely fine, /opt/csw/bin/scp axetlxyz01:/opt/data/test/QURIES* ./input I want to make the above line better, by adding an IF statement, want to check if there is any file exists with name QURIES*.* then i need to copy that. if ... (7 Replies)
Discussion started by: rkrgarlapati
7 Replies

6. Shell Programming and Scripting

using if statement to check file size

Hi, Am trying to execute certain commands if the condition satisfied, but i feel i am making some mistakes in the usage of if statement here is the code #!/bin/ksh SIZE=$(ls -ltr /aemu/ws/DN.txt | tr -s ' ' | cut -d ' ' -f 5) filename=`TZ=CST+24 date +%Y%m%d` ZERO=0 if then cp... (5 Replies)
Discussion started by: aemunathan
5 Replies

7. Shell Programming and Scripting

check file is there not in linux using if statement

How to check file is there not in linux using if statement ? Please do put one example if possible. (4 Replies)
Discussion started by: nskbalu
4 Replies

8. Shell Programming and Scripting

How to check if a file exists using the if statement

Hi, I'm trying to write a bit of code that will check if a file exists and then archives the file Im trying to use the following if statement without success.. if then mv filename archive/filename else echo "no filename exists" fi Should the file name be... (3 Replies)
Discussion started by: Jazmania
3 Replies

9. Shell Programming and Scripting

How to get path and check in if statement

Hi, I was wondering if it possible to get the path of a variable and compare that to something. Basically I want to write a script that checks if my $JAVA_HOME is correct and if not then it sets it. So far I have... if ] then export JAVA_HOME='/pathhere' echo JAVA_HOME='/pathhere' fi ... (6 Replies)
Discussion started by: eltinator
6 Replies

10. UNIX for Dummies Questions & Answers

check a statement -- yes/no

this statement "Be sure to have /usr/bin before /usr/local/bin in your $PATH" is because, if u have any files in bin directory, /usr/local/bin will be the first place to look at instead of /usr/bin. BUT, /usr/local/bin has one more descending directory to go , therefore /usr/bin has to come before... (2 Replies)
Discussion started by: yls177
2 Replies
Login or Register to Ask a Question