Sponsored Content
Full Discussion: Need help on shell scripting
Top Forums Shell Programming and Scripting Need help on shell scripting Post 302972186 by bakunin on Monday 2nd of May 2016 09:42:58 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
escape(1)							Mail Avenger 0.8.3							 escape(1)

NAME
escape - escape shell special characters in a string SYNOPSIS
escape string DESCRIPTION
escape prepends a "" character to all shell special characters in string, making it safe to compose a shell command with the result. EXAMPLES
The following is a contrived example showing how one can unintentionally end up executing the contents of a string: $ var='; echo gotcha!' $ eval echo hi $var hi gotcha! $ Using escape, one can avoid executing the contents of $var: $ eval echo hi `escape "$var"` hi ; echo gotcha! $ A less contrived example is passing arguments to Mail Avenger bodytest commands containing possibly unsafe environment variables. For example, you might write a hypothetical reject_bcc script to reject mail not explicitly addressed to the recipient: #!/bin/sh formail -x to -x cc -x resent-to -x resent-cc | fgrep "$1" > /dev/null && exit 0 echo "<$1>.. address does not accept blind carbon copies" exit 100 To invoke this script, passing it the recipient address as an argument, you would need to put the following in your Mail Avenger rcpt script: bodytest reject_bcc `escape "$RECIPIENT"` SEE ALSO
avenger(1), The Mail Avenger home page: <http://www.mailavenger.org/>. BUGS
escape is designed for the Bourne shell, which is what Mail Avenger scripts use. escape might or might not work with other shells. AUTHOR
David Mazieres Mail Avenger 0.8.3 2012-04-05 escape(1)
All times are GMT -4. The time now is 03:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy