简体   繁体   English

[ncftp]如何将值传递给bash脚本

[英][ncftp]how to pass value to command into a bash script

I have a bash script and I want to use ncftp to do something. 我有一个bash脚本,我想使用ncftp做某事。 if I have this: 如果我有这个:

#!/bin/sh
HOST='my_IP_FTP_HOST'
USER='username'
PASSWD='password'
ncftp -u $USER -p $PASSWD $HOST <<END_SCRIPT
pwd 
quit
END_SCRIPT

I get this error: 我收到此错误:

Syntax error in parameters or arguments

I don't understand why. 我不明白为什么。 If I give it only the value and not the variables it works... 如果我只给它值而不是变量,它就起作用了...

if I launch 如果我启动

$ sh -x script.sh

I get: 我得到:

+ HOST=$'xxx.x.xx.xx\r'
+ USER=$'username\r'
+ PASSWD=$'password\r'
+ ncftp -u $'username\r' -p $'password\r' $'xxx.x.xx.xx\r'
NcFTP 3.2.1 (Jul 29, 2007) by Mike Gleason (http://www.NcFTP.com/contact/).
Welcome to FTP server
Syntax error in parameters or arguments

hmmm.... \\r creates problems sure. hmmm .... \\ r肯定会造成问题。

Are you editing this file on Microsoft Windows? 您是否正在Microsoft Windows上编辑此文件? It doesn't works because the original file uses Windows carriage returns, which are passed to variables and then to ncftp, which then halts. 它不起作用,因为原始文件使用Windows回车,这些回车先传递给变量,然后传递给ncftp,然后ncftp停止。

Fix it by using another text editor , or by using dos2unix . 使用其他文本编辑器dos2unix对其进行修复。 Ideally you'd first use dos2unix once and then change the text editor. 理想情况下,您将首先使用dos2unix一次,然后更改文本编辑器。

Alternatively (but really, really not recommended) you could just parse the variables and remove the extra \\r , like below, especially when the problem arises at some input reading , which doesn't seems to be the case here. 另外(但实际上,实际上不建议这样做),您可以仅解析变量并删除多余的\\ r ,如下所示,尤其是当某些输入读取出现问题时,似乎不是这种情况。

HOST=${HOST%\\r}
USER=${USER%\\r}
PASSWD=${PASSWD%\\r}

Take care when copying text from Windows, or when editing without appropriate software. 从Windows复制文本或在没有适当软件的情况下进行编辑时,请当心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM