Need help on shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help on shell scripting
# 1  
Old 05-02-2016
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.

Code:
#!/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.

Last edited by RudiC; 05-02-2016 at 07:24 AM.. Reason: Added code tags (AGAIN!)
# 2  
Old 05-02-2016
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.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-02-2016
Maybe you can cd to a write-protected directory before.
Code:
...
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.)
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 05-02-2016
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
# 5  
Old 05-02-2016
Use ftp's chmod command.
# 6  
Old 05-02-2016
Pertaining to your problem at hand you already got some sound advice.

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

Quote:
Originally Posted by chandraprakash
I need a help on my shell script. Below is my shell script which transfers the file from one server to another.

Code:
#!/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

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:

Code:
#!/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
# 7  
Old 05-02-2016
Quote:
Originally Posted by chandraprakash
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
Some ftp servers allow additional site commands.
Try to set permissions with
Code:
site chmod 777 /wpath/to/copy/files/$input_variable/inputs/final/

You can check the allowed site commands in an interactive ftp session
Code:
ftp> remotehelp site

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

2. Shell Programming and Scripting

help me in Shell Scripting

Hi there please have a look at the code..i want to create Using a named pipe. Run a find in the background starting in the working directory While this is happening wait for input from the user to ask him which file to find. If the user does not enter any data in 10 seconds ask the user again.... (1 Reply)
Discussion started by: kattak1511
1 Replies

3. Shell Programming and Scripting

Shell scripting

Hi, if in a network there are lots of PCs connected with either windows or linux as operating system.Then what will be the shell script for the same and also if the PC has linux in it then we have to find if it is occupied or unoccupied. If the PC has windows in it then we have to find if it is... (6 Replies)
Discussion started by: akansha singh
6 Replies

4. UNIX for Dummies Questions & Answers

Shell Scripting

Hey I have a data in the file named as outputFile.txt. The data is in the format 123456,12345678912345,400,09/09/09,INACTIVE. I want this output without commas ie 12345612345678912345400090909INACTIVE. Please tell me what to do and clear explain all the terms, as I am new to it. (6 Replies)
Discussion started by: sampandey31
6 Replies

5. Web Development

Perl scripting or shell scripting?

i am going to study any one of the scripting languages mentioned above(shell 0r perl scripting) . Which is having more scope for a fresher? (1 Reply)
Discussion started by: Anna Hussie
1 Replies

6. What is on Your Mind?

Shell Scripting vs Perl scripting

Gents, I have been working in a Solaris/Unix environment for about 9 months. I took some linux classses online before getting the job. But, I am not very good at scripting. I want to learn how to script. Do you think that I should start with Shell scripting or Perl? I wanted to continue with... (2 Replies)
Discussion started by: Pouchie1
2 Replies

7. Android

Android Scripting Environment: Shell Scripting and Android

I just upgraded to Android 2.2 from 2.1. The GPS issue that was troublesome in 2.1 seems to have been fixed. Some of web browsing seems faster, but it could just be my connection is better today ;) Flash works in some browsers but not very good and it is too slow for Flash apps designed for... (0 Replies)
Discussion started by: Neo
0 Replies

8. What is on Your Mind?

Shell scripting vs Perl scripting

Hi all, I would like to start developping some good scripting skills. Do you think it would be best to start with shell scripting or Perl? I already got a fundation, really basics, in perl. but I am wondering what would be best to be good at first. Can you please help me determine which one to... (14 Replies)
Discussion started by: Pouchie1
14 Replies

9. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

10. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies
Login or Register to Ask a Question