PDA

View Full Version : [bash]



detorresrc
March 19th, 2009, 06:28 AM
Is there a way to detect when i copy a file from client to server. Here's the sample.



# NO NEED TO INPUT SERVER PASSWORD
#!/bin/sh

if [ scp <myfilename> user@xxx.xxx.xxx:/home/ ]
echo "Failed";
else
echo "Success";
fi

movieman
March 19th, 2009, 07:32 AM
There's a way to check the return code of the program you ran, I think it's something like '$?' So if scp returns an error when it can't copy, you should be able to check it that way.

Yeah, if I run:

#!/bin/bash
scp foo someone@somewhere:
echo $?

Then it prints out an error because it can't connect, followed by a 1 for the $? variable.

detorresrc
March 19th, 2009, 07:36 AM
Tnx tnx tnx...

nelskurian
March 19th, 2009, 07:39 AM
Just FYI...May be some others in need of..

Code:


#!/bin/bash

ftp_site=ftp://yoursite.com
username=roger
passwd=abc123
backupdir=$HOME
filename="backup-$(date '+%F-%H%M').tar.gz"

echo "Creating a backup file $filename of $backupdir."

# Make a tar gzipped backup file
tar -cvzf "$filename" "$backupdir"

ftp -in <<EOF
open $ftp_site
user $username $passwd
bin
put $filename
close
bye
EOF

Thanks moma

geirha
March 19th, 2009, 08:52 AM
Your first attempt was very close. Just remove those []'s, add a then, and switch the messages.


if scp <myfilename> user@xxx.xxx.xxx:/home/
then
echo "Success";
else
echo "Failed";
fi

detorresrc
March 20th, 2009, 04:12 AM
Tnx to all of you guys.!!!!!