0

FTP is connecting to the server but i am getting an error -

Enter if the env is dev or test or prod:
test
Please enter the id no :
xxxxxxx
Connected to xxxx
220 (vsFTPd 2.2.2)
331 Please specify the password.
230 Login successful.
**?Invalid command
?Invalid command
?Invalid command
?Invalid command
?Invalid command**
200 PORT command successful. Consider using PASV.

Below is shell script -

#!/bin/bash
echo "Enter if the env is dev or test or prod:"
while :
do
read -r INPUT_STRING
case $INPUT_STRING in
    test | TEST)
        echo "Please enter id no : "
        read -r input_variable
        if [[ ${#input_variable} -ne "7" ]]
        then
            echo "Please check id no given"
            exit 1
        fi
        HOST=XXX
        USER=XXX
        PASSWORD=XX
        ftp -inv $HOST <<- EOF
                user $USER $PASSWORD
                mypath='/test/$input_variable/destination/'
                if ! cd "$mypath"
                then
                    exit 1
                fi
                mput x.csv
EOF
                exit 1
    ;;
esac
done
2
  • Please fix the formatting so we can focus on the actual errors besides the ones caused by the bad formatting. Commented May 4, 2016 at 5:48
  • @ Julie - updated now.Do have a look Commented May 4, 2016 at 6:18

3 Answers 3

1

Your main problem is that you think you're setting a variable with your line mypath='/test/$input_variable/destination/' but it is actually run inside the FTP session.

You need to move it above the FTP command. You also check conditions after it that can not be checked there for the same reason.

4
  • the mypath directory is present in the fp location and i am passing the id no with input_variable. If the particular path is not available i need to exit the program Commented May 4, 2016 at 6:34
  • Then you will need to do something more sophisticated than merely redirecting an input string to the FTP client. Commented May 4, 2016 at 6:43
  • @ Julie - thanks for your help. script is working fine if i don't assign the variable to the directory and just give cd <path>. i am getting error only when i assign the variable to a directory Commented May 4, 2016 at 8:13
  • I know. That is because you were assigning it in a place where you can not assign, you are simply redirecting that as an input. Commented May 4, 2016 at 15:01
1

Your problem is that you are trying to set a variable inside a here-doc:

ftp -inv $HOST <<- EOF
    user $USER $PASSWORD
    mypath='/test/$input_variable/destination/'
    if ! cd "$mypath"
    then
        exit 1
    fi
    mput x.csv
EOF

That can not work. Change that part to this:

mypath="/test/$input_variable/destination/"
ftp -inv $HOST <<-_EOF_
    user $USER $PASSWORD
    cd "$mypath"
    mput x.csv
_EOF_
4
  • That fixes the variable, but || exit 1 isn't valid syntax in any commandline ftp client I've seen; to check for error you need to look at the output with expect or similar, or instead use a more-or-less programmed method like perl or python. Commented May 4, 2016 at 19:37
  • @dave_thompson_085 And you are exactly right, I don't know what was I Thinking. Thanks. I just removed it.
    – user79743
    Commented May 4, 2016 at 20:22
  • Thanks BinaryZebra .. that was helpful. my script is working fine now.But i have to exit the program if the mypath directory is not available in the ftp location. Commented May 5, 2016 at 11:23
  • @chandraprakash Then you need to use commands that work inside an ftp connection. And test the "exit code" of ftp in the shell probably with the command "expect". .... .... In any case that falls outside the scope of this question, please ask a different question.
    – user79743
    Commented May 5, 2016 at 16:17
0

Change your single quotes to double quotes.

Instead of:

mypath='/test/$input_variable/destination/'

Use:

mypath="/test/$input_variable/destination/"
1
  • Thanks Wildcard..i made the changes but i am getting same error Commented May 4, 2016 at 6:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .