I want to write a shell script to automate the ftp session, in that it should take the inputs like the server ip , username , password from a different file who path i'll pass.
and also the validations that if the server path exits or not.

Dani AI

Generated

The root cause is that the FTP client expects FTP commands in the here‑document, not raw answers to its prompts. Sending the username or password on their own lines (or embedding AWK backticks directly into the heredoc) makes the client try to execute those lines as commands. Assign the parsed config values to shell variables first, then send a proper user command (or use a non‑interactive tool).

A simple, robust pattern (after you parse config.cfg into HOST, USER, PASS, REMOTE_DIR, FILE and FILE_MODE) is:

ftp -inv "$HOST" <<EOF
user $USER $PASS
$FILE_MODE
cd $REMOTE_DIR
get $FILE
bye
EOF

Alternatives that avoid interactive ftp and are usually easier to script and debug:

  • curl (one line, good for single files):
    curl -u "$USER:$PASS" "ftp://$HOST/$REMOTE_DIR/$FILE" -o "$FILE"
  • .netrc for automatic logins (store machine example.com login myuser password mypass in ~/.netrc and chmod 600 ~/.netrc), or use lftp for richer scripting.

Quick troubleshooting and tips

  • Make sure your config file is parsed once into variables before the heredoc; that prevents awkward command substitutions inside the here‑doc.
  • Fix small shell mistakes (use exit 0 not exit0).
  • Use verbose/interactive flags (-v or run the client manually) to see server replies. To check a remote path exists, try an ls or cd in the script and test the client’s response.
  • Protect credentials: limit file permissions (600) or prefer tokenized/secured methods when possible.

For : parse the config into variables, use the user $USER $PASS line inside the heredoc (or switch to curl/.netrc), and that will stop the FTP client from treating the password as an invalid command.

Like I have a file config.cfg and contains the following data:-

server_IP =
username = nikita
password = nikita
file_path = ${HOME}/tools/
file_name =

I have used this script, but its not working :

ftp awk '$1 ~ /server_IP/ { print $3 }' config.cfg  
        ser awk '$1 ~ /user/ { print $3 }' config.cfg
        assword awk '$1 ~ /password/ { print $3 }' config.cfg
        awk '$1 ~ /file_mode/ { print $3 }' config.cfg
        cd awk '$1 ~ /file_path/ { print $3 }' config.cfg
        get awk '$1 ~ /file_name/ { print $3 }' config.cfg
        echo file downloaded
        echo end
        bye

I have finally written this code, but the prob with this is that when it is prompting for password it doesnt send any password to the ftp machine, we have to do it manually, but after typing it manually, it take the password line as command and shows that that is invalid command.
please tell me a way to pass the password automatically

CONFIG_FILE="${HOME}/c/config.cfg"
Password=`awk '$1 ~ /password/ { print $3 }' $CONFIG_FILE`
echo $Password
if [ ! -r $CONFIG_FILE ]
then
        echo "File not readable";exit0;
fi

ftp -i `awk '$1 ~ /server_IP/ { print $3 }' $CONFIG_FILE` <<END
`awk '$1 ~ /user/ { print $3 }' $CONFIG_FILE`
`awk '$1 ~ /password/ { print $3 }' $CONFIG_FILE`
`awk '$1 ~ /file_mode/ { print $3 }' $CONFIG_FILE`
cd `awk '$1 ~ /file_path/ { print $3 }' $CONFIG_FILE`
get `awk '$1 ~ /file_name/ { print $3 }' $CONFIG_FILE`
quit
END

the config file is as

server_IP       =       10.18.14.2
user            =       scpyogesh
password        =       scpyogesh
file_mode       =       binary
file_path       =       sample/test/
file_name       =       test_ftp.txt
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.