Need help on shell scripting

I need a help on my shell script. Below is my shell script which transfers the file from one server to another.

#!/bin/bash
echo "Please enter id no : "
read input_variable
echo "You entered: $input_variable"
HOST=xxxx
USER=xx
PASSWORD=xxx
ftp -inv $HOST <<EOF
user $USER $PASSWORD
cd /wpath/to/copy/files/$input_variable/inputs/final/
mput x.csv
bye
EOF

The shell script is working fine and it is copying the file X.csv once we enter id no which is present in remote server. However I have certain issues which is listed below -

1.id no should be of 6 characters (like M000001 ).
2.When I enter the correct id, the script is copying the file.However when i enter wrong id eg. M000050 which is not present in remote server, then also it is showing the file is copied. But I don't have any idea where it is copying the file.
3.I need to create the directory /final/ if it doesn't exit in remote machine. the path till inputs will be present in remote server.

1) The id no. shown is 7 chars in length. Do you just need to test on its length or also impose some structure (like "start with an upper case "M")?
2) It will copy to /wpath/to/copy/files//inputs/final/ and return an error if that is not available. The double slash will be interpreted as a single one on most systems. Do you have a list of allowed target directories present on the local node?
3) Use ftp 's mkdir command.

1 Like

Maybe you can cd to a write-protected directory before.

...
cd /
cd /wpath/to/copy/files/$input_variable/inputs/final/
...

If the latter fails, it will upload to / (and fail, i.e. not upload anything.)

1 Like

Thanks RduiC and MadeInGermany. Very helpful.

I want to create the directory with 777 permission.

mkdir /wpath/to/copy/files/$input_variable/inputs/final/

The directory final should have 777 permission. Do you have any suggestion

Use ftp 's chmod command.

Pertaining to your problem at hand you already got some sound advice.

Anyways, i would like to question your construction as a whole:

I think you should not use ftp any more and you definitely should not put passwords into scripts AT ALL!

You can do all you have shown here using either "sftp" or - IMHO even better - "scp" and avoid many of the obstacles you try to overcome from the beginning.

What you need for "scp" to work is an installed "ssh" suite (scp is usually part of this) and exchanged keys. How to do that, how to generate the keys and store them in the appropriate key files is described in many threads here, just search for it.

Having set up this here is the sketch of a script you could use:

#!/bin/bash
typeset input_variable=""
typeset host="XXX"
typeset user="xx"

echo "Please enter id no : "
read input_variable
echo "You entered: $input_variable"

# if the directory does not exist ls (and ssh in turn) will return a non-zero RC
if ! ssh -nqo 'BatchMode = yes' ${user}@${host} ls /wpath/to/copy/files/$input_variable/inputs/final >/dev/null 2>&1 ; then
     echo "Directory $input_variable does not exist on host $host."
     exit 1
fi

if ! scp x.csv ${user}@${host}:/wpath/to/copy/files/$input_variable/inputs/final ; then
     echo "Failed to transfer the file."
     exit 2
fi

echo "File transfer successful."
exit 0

Of course you can put a lot more testing into this sketch, create better error-handling, maybe test for the IP-connectivity first before trying the ssh-connection (ping), etc.. Scripting is not about doing things but to plan in advance what possibly could go wrong and cover for that.

I hope this helps.

bakunin

Some ftp servers allow additional site commands.
Try to set permissions with

site chmod 777 /wpath/to/copy/files/$input_variable/inputs/final/

You can check the allowed site commands in an interactive ftp session

ftp> remotehelp site