我正在尝试使用家庭网络中的SFTP协议将文件发送到我的手机。虽然我可以很容易地使用FTP中的put命令将文件发送到我的手机,但我想自动执行这项任务。所以我写了这个脚本:
#! /bin/bash
#Capture and share screenshot to my phone
gnome-screenshot
cd /home/prm/Pictures
FILE="$(ls -Art | tail -n 1)" #To get the last created file
sftp sftp://192.168.1.2:1753/primary/DCIM/Screenshots
put /home/prm/Pictures/$FILE我可以连接到我的手机和所需的目录,但我不知道如何上传。请帮帮我!
将代码更新为:
#! /bin/bash
#Capture and share screenshot to my phone
gnome-screenshot
cd /home/prm/Pictures
FILE="$(ls -Art | tail -n 1)" #To get the last created file
echo $FILE
sftp sftp://192.168.1.3:1761/primary/DCIM/Screenshots -b <<<"put /home/prm/Pictures/$FILE"我得到了以下输出:
prm@prm-2018-02:~/Documents/Anubhav/Bash$ ./capture.sh
Screenshot from 2020-06-04 22-38-27.png
Connected to 192.168.1.3.
Fetching /primary/DCIM/Screenshots/ to -b/Screenshots
Cannot download non-regular file: /primary/DCIM/Screenshots/我还尝试添加-r标志:
sftp -r sftp://192.168.1.3:1761/primary/DCIM/Screenshots -b <<<"put /home/prm/Pictures/$FILE"但这把我手机上的截图复制到了本地系统。
prm@prm-2018-02:~/Documents/Anubhav/Bash$ ./capture.sh
Screenshot from 2020-06-04 22-51-51.png
Connected to 192.168.1.3.
Fetching /primary/DCIM/Screenshots/ to -b/Screenshots
Retrieving /primary/DCIM/Screenshots
/primary/DCIM/Screenshots/Screenshot_20200604-225146.jpg 100% 176KB 549.6KB/s 00:00 发布于 2020-06-04 23:05:24
put /home/prm/Pictures/$FILE命令由shell“执行”,您希望它由sftp命令执行。
sftp支持使用-b.Something的批处理文件,如下所示:
[sorin@localhost ~]$ sftp -b- sftp://test/ <<< "put $FILE"
Connected to test.
sftp> put test.txt
Uploading test.txt to /home/sorin/test.txt
test.txt 100% 0 0.0KB/s 00:00请注意,-b需要非交互式身份验证,任何提示都会卡住。
注意:以前的变体sftp sftp://test/ -b<<< "put $FILE"是错误的!-b被忽略,选项应该在连接字符串之前。它似乎可以工作,因为sftp会检查stdin是否是终端,并正确处理这种情况。
但是也有一些问题:在批处理模式下,sftp会在第一个错误时终止,并设置一个非零的退出代码;在“交互模式”下,它会忽略错误,因此您无法进行任何错误处理。
[sorin@localhost ~]$ sftp sftp://test/ -b<<<"put fkdkd
put test.txt"
Connected to test.
sftp> put fkdkd
stat fkdkd: No such file or directory
sftp> put test.txt
Uploading test.txt to /home/sorin/test.txt
test.txt 100% 0 0.0KB/s 00:00
[sorin@localhost ~]$ echo $?
0
[sorin@localhost ~]$ sftp -b- sftp://test/ <<<"put fkdkd
put test.txt"
sftp> put fkdkd
stat fkdkd: No such file or directory
[sorin@localhost ~]$ echo $?
1
[sorin@localhost ~]$ 发布于 2020-06-05 12:36:26
感谢Sorin,你让我离解决问题更近了一步,最终解决了这个问题。
#! /bin/bash
#Capture and share screenshot to my phone
gnome-screenshot
cd /home/prm/Pictures
FILE="$(ls -Art | tail -n 1)" #To get the last created file
echo $FILE
sftp sftp://192.168.1.3:1761/primary/DCIM/Screenshots <<EOF
put "$FILE"
bye
EOFhttps://stackoverflow.com/questions/62197714
复制相似问题