curl 删除命令在脚本化 for 循环中不起作用

curl 删除命令在脚本化 for 循环中不起作用

有什么原因导致curl命令不能使用变量吗?

这是我的设置 - 我没有使用实际值,因为我无法公开客户端数据并且没有测试环境。

您会注意到我正在使用两个curl命令,是的,我知道它可以组合成一个命令,但我无法使这一工作起作用,所以我将其分成两个单独的命令进行测试。

在这里,我设置了系统上入站文件夹的路径:

[root@acme logs]# act="roadrunner"
[root@acme logs]# content="xml"
[root@acme logs]# inbndfldr='/ftp/'"$act"'/inbound'
[root@acme logs]# destinationFolder="$inbndfldr"/"$content"/
[root@acme logs]# logFolder="$inbndfldr"/logs

在这里,我设置了远程系统上出站文件夹的路径:

[root@acme logs]# targetHost="SFTP://welcometotheroad.net"
[root@acme logs]# THFolderName="OUTBOUND"
[root@acme logs]# THFolder='/'"$THFolderName"
[root@acme logs]# THSftpAddress=$targetHost$THFolder
[root@acme logs]# THDeleteSftpAddress=$targetHost$THFolder'/'

这里我在远程系统上设置身份验证:

[root@acme logs]# username=mrRoadRunner
[root@acme logs]# password=Beep-8eep!
[root@acme logs]# sessionAuthentication="$username":"$password"

这里我设置了文件引用:

[root@acme logs]# fileref="openroad2019.xml"

这里我验证该文件是否存在于远程服务器上:

[root@acme logs]# THFiles=$(curl -sSk -u $sessionAuthentication $THSftpAddress | grep $fileref | awk '{print $9}')
[root@acme logs]# echo $THFiles
[root@acme logs]# openroad2019.xml

我在这里开始我的循环:

[root@acme logs]# for targetFile in $THFiles
> do
>   dateTimeStamp=`date +%Y%m%d%H%M%S`
>   traceLog="$logFolder"/"$targetFile"trace"$dateTimeStamp".log
>   testLog="$logFolder"/test"$dateTimeStamp".log
>   deltraceLog="$logFolder"/del"$targetFile"trace"$dateTimeStamp".log
>   deltestLog="$logFolder"/deltest"$dateTimeStamp".log
>   deleteString='"-rm '"$THFolderName"'/'"$targetFile"'"'
>   masterGetStr=`echo curl \-k \-\-trace "$traceLog" \-\-user "$sessionAuthentication" \-O "$THSftpAddress"\/"$targetFile" \-\-ftp\-ssl \&\>\>"$testLog" 2\>\&1`
>   masterDeleteStr=`echo curl \-sSk \-\-trace $deltraceLog \-u $sessionAuthentication $THDeleteSftpAddress \-Q$deleteString  \&\>\> $deltestLog 2\>\&1`
>   echo
>   echo \-\-\-\-\-
>   echo "$masterGetStr"
>   echo \-\-
>   echo "$masterDeleteStr"
>   echo \-\-\-\-\-
>   echo
>   curl -k --trace "$traceLog" --user "$sessionAuthentication" -O "$THSftpAddress"/"$targetFile" --ftp-ssl &>> "$testLog" 2>&1
>   curl -sSk --trace $deltraceLog -u $sessionAuthentication $THDeleteSftpAddress -Q$deleteString  &>> $deltestLog 2>&1
> done

这是输出:

-----
curl -k --trace /ftp/roadrunner/inbound/logs/openroad2019.xmltrace20190628180349.log --user mrRoadRunner:Beep-8eep! -O SFTP://welcometotheroad.net/OUTBOUND/openroad2019.xml --ftp-ssl &>>/ftp/roadrunner/inbound/logs/test20190628180349.log 2>&1
--
curl -sSk --trace /ftp/roadrunner/inbound/logs/delopenroad2019.xmltrace20190628180349.log -u mrRoadRunner:Beep-8eep! SFTP://welcometotheroad.net/OUTBOUND/ -Q"-rm OUTBOUND/openroad2019.xml" &>> /ftp/roadrunner/inbound/logs/deltest20190628180349.log 2>&1
-----

第一个curl命令工作得很好,将文件放在我需要的地方。

第二个curl命令应该删除远程系统上的文件,但它不起作用。我在这里查看相关日志:

::::::::::::::
delopenroad2019.xmltrace20190628180349.log
::::::::::::::
== Info:   Trying 0.0.0.0...
== Info: TCP_NODELAY set
== Info: Connected to welcometotheroad.net (0.0.0.0) port 22 (#0)
== Info: SSH MD5 fingerprint: Th3re1$4WarpDr1v3N$+4r$#1P!
== Info: SSH authentication methods available: publickey,password
== Info: Using SSH private key file '/root/.ssh/id_rsa'
== Info: SSH public key authentication failed: Username/PublicKey combination invalid
== Info: Initialized password authentication
== Info: Authentication complete
== Info: Sending quote commands
== Info: Syntax error in SFTP command. Supply parameter(s)!
== Info: Connection #0 to host welcometotheroad.net left intact
== Info: getaddrinfo(3) failed for OUTBOUND:80
== Info: Couldn't resolve host 'OUTBOUND'
== Info: Closing connection 1
::::::::::::::
deltest20190628180349.log
::::::::::::::
curl: (21) Syntax error in SFTP command. Supply parameter(s)!
curl: (6) Couldn't resolve host 'OUTBOUND'
::::::::::::::
test20190628180349.log
::::::::::::::
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  4290  100  4290    0     0   3248      0  0:00:01  0:00:01 --:--:--  3252
100  4290  100  4290    0     0   3247      0  0:00:01  0:00:01 --:--:--  3247
[root@acme logs]# 

然而,当我手动输入命令时 - 它会起作用并从远程系统中删除该文件。

curl -sSk --trace /ftp/roadrunner/inbound/logs/delopenroad2019.xmltrace20190628180349.log -u mrRoadRunner:Beep-8eep! SFTP://welcometotheroad.net/OUTBOUND/ -Q"-rm OUTBOUND/openroad2019.xml" &>> /ftp/roadrunner/inbound/logs/deltest20190628180349.log 2>&1

[root@rh1 logs]# more *20190628180546*.log

::::::::::::::
openroad2019.xmltrace20190628180546.log
::::::::::::::
Info:   Trying 0.0.0.0...
== Info: TCP_NODELAY set
== Info: Connected to welcometotheroad.net  (0.0.0.0) port 22 (#0)
== Info: SSH MD5 fingerprint: Th3re1$4WarpDr1v3N$+4r$#1P!
== Info: SSH authentication methods available: publickey,password
== Info: Using SSH private key file '/root/.ssh/id_rsa'
== Info: SSH public key authentication failed: Username/PublicKey combination invalid
== Info: Initialized password authentication
== Info: Authentication complete
<= Recv data, 2000 bytes (0x7d0)
0000: ef bb bf 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e ...<?xml version
...
... (details removed)
...
0110: 6e 73 74 72 75 63 74 69 6f 6e 73 48 65 61 64 65 nstructionsHeade
0120: 72 3e                                           r>
== Info: Connection #0 to host welcometotheroad.net  left intact

::::::::::::::
del_openroad2019.xmltrace20190628180546.log
::::::::::::::
== Info:   Trying 0.0.0.0...
== Info: TCP_NODELAY set
== Info: Connected to welcometotheroad.net  (0.0.0.0) port 22 (#0)
== Info: SSH MD5 fingerprint: Th3re1$4WarpDr1v3N$+4r$#1P!
== Info: SSH authentication methods available: publickey,password
== Info: Using SSH private key file '/root/.ssh/id_rsa'
== Info: SSH public key authentication failed: Username/PublicKey combination invalid
== Info: Initialized password authentication
== Info: Authentication complete
=> Send data, 55 bytes (0x37)
0000: 64 72 77 78 72 2d 78 2d 2d 2d 20 32 20 66 74 70 drwxr-x--- 2 ftp
0010: 61 64 6d 69 6e 20 66 74 70 61 64 6d 69 6e 20 20 admin ftpadmin  
...
... (details removed)
...
0050: 30 30 2d 32 36 37 36 39 36 37 33 5f 44 49 5f 4c 00-26769673_DI_L
0060: 45 30 2e 58 4d 4c 0a                            E0.XML.
== Info: Sending quote commands
== Info: Connection #0 to host welcometotheroad.net  left intact

::::::::::::::
delopenroad2019.xmltrace20190628180546.log
::::::::::::::
== Info:   Trying 0.0.0.0...
== Info: TCP_NODELAY set
== Info: Connected to welcometotheroad.net  (0.0.0.0) port 22 (#0)
== Info: SSH MD5 fingerprint: Th3re1$4WarpDr1v3N$+4r$#1P!
== Info: SSH authentication methods available: publickey,password
== Info: Using SSH private key file '/root/.ssh/id_rsa'
== Info: SSH public key authentication failed: Username/PublicKey combination invalid
== Info: Initialized password authentication
== Info: Authentication complete
== Info: Sending quote commands
== Info: Syntax error in SFTP command. Supply parameter(s)!
== Info: Connection #0 to host welcometotheroad.net  left intact
== Info: getaddrinfo(3) failed for OUTBOUND:80
== Info: Couldn't resolve host 'OUTBOUND'
== Info: Closing connection 1

::::::::::::::
del_test20190628180546.log
::::::::::::::
drwxr-x--- 2 ftpadmin ftpadmin        0 Jun 28 23:38 .
drwxr-x--- 2 ftpadmin ftpadmin        0 Apr 26 2019 ..
-rwxr-x--- 1 ftpadmin ftpadmin     7780 Jun 27 14:36 NextFile20190620172738.XML
-rwxr-x--- 1 ftpadmin ftpadmin     2440 Jun 27 14:36 NextFile20190620172740.XML
-rwxr-x--- 1 ftpadmin ftpadmin     7980 Jun 27 14:36 NextFile20190624111531.XML
-rwxr-x--- 1 ftpadmin ftpadmin     2441 Jun 27 14:36 NextFile20190624111533.XML
-rwxr-x--- 1 ftpadmin ftpadmin     7858 Jun 27 14:36 NextFile20190626165728.XML
-rwxr-x--- 1 ftpadmin ftpadmin     2500 Jun 27 14:36 NextFile20190626165729.XML
-rwxr-x--- 1 ftpadmin ftpadmin    15424 Jun 27 14:36 NextFile20190627091426.XML
-rwxr-x--- 1 ftpadmin ftpadmin     4104 Jun 27 14:37 NextFile20190627091427.XML
-rwxr-x--- 1 ftpadmin ftpadmin    10417 Jun 27 14:37 NextFile20190627092525.XML
-rwxr-x--- 1 ftpadmin ftpadmin     3071 Jun 27 14:37 NextFile20190627092527.XML
-rwxr-x--- 1 ftpadmin ftpadmin    15594 Jun 27 14:37 NextFile20190627093345.XML
-rwxr-x--- 1 ftpadmin ftpadmin     4279 Jun 27 14:37 NextFile20190627093347.XML
-rwxr-x--- 1 ftpadmin ftpadmin    17237 Jun 27 14:37 NextFile20190627094528.XML
-rwxr-x--- 1 ftpadmin ftpadmin     4290 Jun 27 14:37 NextFile20190627094530.XML
-rwxr-x--- 1 ftpadmin ftpadmin    15608 Jun 27 14:37 NextFile20190627100222.XML
-rwxr-x--- 1 ftpadmin ftpadmin     4288 Jun 27 14:37 NextFile20190627100224.XML
-rwxr-x--- 1 ftpadmin ftpadmin     2455 Jun 27 14:37 NextFile20190627102026.XML
-rwxr-x--- 1 ftpadmin ftpadmin     7858 Jun 27 15:18 NextFile20190627111647.XML
-rwxr-x--- 1 ftpadmin ftpadmin     2500 Jun 27 15:18 NextFile20190627111648.XML
-rwxr-x--- 1 ftpadmin ftpadmin     7858 Jun 27 18:56 NextFile20190627145327.XML
-rwxr-x--- 1 ftpadmin ftpadmin     2500 Jun 27 18:56 NextFile20190627145328.XML
-rwxr-x--- 1 ftpadmin ftpadmin    12797 Jun 27 20:08 NextFile20190627160344.XML
-rwxr-x--- 1 ftpadmin ftpadmin     3609 Jun 27 20:08 NextFile20190627160345.XML

我的问题是“为什么它不能在循环中工作?”

答案1

不要(尝试)在变量中添加引号。

Shell 命令解析以明确的、固定的顺序进行,并且变量替换(正式的参数扩展)完成(好吧!)处理命令行引用。当你这样做时

 deleteString='"-rm '"$THFolderName"'/'"$targetFile"'"'
 ...
 curl ... -Q$deleteString ...

它将值放入"-rm OUTBOUND/openroad2019.xml"变量中,当该值被替换时,它会在空格(或一般的 IFS)处分割并传递卷曲的参数(除了其他参数)如下:

 ...
 -Q"-rm
 OUTBOUND/openroad2019.xml"
 ...

其中第一个会使远程服务器感到困惑并产生错误

 Syntax error in SFTP command. Supply parameter(s)!

第二个被curl视为第二个要访问的URL,导致它正确地抱怨 OUTBOUND 不是有效的主机名。

相反,将空格引用到变量值中,然后用双引号引起来代换保留该空间,但不要添加任何类型的引号价值:

 deleteString="-rm $THFolderName/$targetfile" 
 ...
 curl ... -Q"$deleteString" ...
 # or if you prefer
 curl ... "-Q$deleteString" ...
 # (-Q are not special to shell so it doesn't matter if they're quoted)

 # or more simply drop the extra variable and just
 curl ... -Q"-rm $THFolderName/$targetfile" ...

它与 for 循环没有任何关系仅与变量有关。

仅供参考,您的复杂而笨重的echo命令显然试图“重建”shell 的功能,但实际上并没有这样做,并且可能通过误导您来浪费您的时间。如果您想知道 shell 如何扩展并执行命令,请设置选项x它会告诉你。对于特定命令(或此处两个),请执行set -x之前和set +x之后的操作。对于脚本,您只需运行即可调试整个脚本,sh -x script args...无需编辑。

相关内容