写一个linux脚本,用sftp远程mput上传文件,统计并echo本地总共有多少文件,上传并echo了多少文件,如果上传文件为0则echo返回“上传文件失败”,并echo上传失败的原因
时间: 2024-09-27 15:05:45 浏览: 29
你可以创建一个简单的bash脚本来完成这个任务。首先确保你安装了`sftp`客户端,并且有权限访问远程服务器。这里是一个基础的示例脚本:
```bash
#!/bin/bash
# 设置远程服务器信息
REMOTE_USER="your_username"
REMOTE_HOST="remote_host.com"
REMOTE_DIR="/path/to/destination"
# 当前目录下的所有文件列表
LOCAL_FILES=$(find . -type f)
# 初始化文件上传计数
UPLOADED_FILES=0
# 遍历本地文件并尝试上传
for file in $LOCAL_FILES; do
echo "Uploading $file..."
sftp -o 'StrictHostKeyChecking=no' $REMOTE_USER@$REMOTE_HOST "$file" $REMOTE_DIR/
if [ $? -eq 0 ]; then
echo "Uploaded successfully."
((UPLOADED_FILES++))
else
echo "Upload failed for $file. Reason: SFTP command returned error code $?"
fi
done
# 统计总数并输出结果
echo "Local files: ${#LOCAL_FILES[@]}"
echo "Uploaded files: $UPLOADED_FILES"
if [ $UPLOADED_FILES -eq 0 ]; then
echo "Upload files failed. No files were uploaded."
fi
阅读全文