Simple bash script help?

Simple bash script help?

Post by L J Bayu » Tue, 20 Oct 1998 04:00:00




> Hi

> I'm trying to write a simple bash script to ftp a file at regular intervals
> to another site.

> My question is, how do I pass parameters to ftp when it's at the ftp>
> prompt?

> That is, I can do:-

> #!/bin/sh
> ftp remote_site

> which works

> but then how do I pass the cd, cp etc commands to ftp?

> I've read all I can find about shell programming but I must be missing
> something!!

What you are missing is called a "Here-document":
  ftp remote_site <</END/
  cd /pub/whatever
  get whatsit
  bye
  /END/
Actually any redirection will do, e.g. ftp site < file_of_commands.
 
 
 

Simple bash script help?

Post by Bob » Wed, 21 Oct 1998 04:00:00


Hi

I'm trying to write a simple bash script to ftp a file at regular intervals
to another site.

My question is, how do I pass parameters to ftp when it's at the ftp>
prompt?

That is, I can do:-

#!/bin/sh
ftp remote_site

which works

but then how do I pass the cd, cp etc commands to ftp?

I've read all I can find about shell programming but I must be missing
something!!

Thanks

Bob

 
 
 

Simple bash script help?

Post by Brian F. G. Biduloc » Wed, 21 Oct 1998 04:00:00



> #!/bin/sh
> ftp remote_site

> but then how do I pass the cd, cp etc commands to ftp?

Ok.  First, edit your .netrc file (in hour home dir)
and add something like:

   thehostidumpto login yourlognamethere password yourpasswdthere

make sure that .netrc has mode 600

   chmod 600 .netrc

When you execute

   ftp thehostidumpto

your will automatically be logged in.  This is necessary
because ftp will always prompt the controlling terminal
for a username and password otherwize.

Once you've done that, you can list the commands you
want like:

   cd /where/i/dump
   lcd /from/which/i/dump
   put dumpfile
   quit

in a file, say `ftp_commands'.  Then when you
execute:

   ftp thehostidumpto < ftp_commands

The commands will get executed.

To do this in the background in a cron sort of way,
you should detach from the controlling terminal by
redirecting both input and output.  With bash use:

   ftp thehostidumpto < ftp_commands > ftp_logfile 2>&1

When used with cron, make sure to specify full path
names for both ftp and the files.  Also make sure to
execute the cron job from the user for which you set
up the .netrc file.

Good luck.

--
Brian F. G. Bidulock, P. Eng.,
Group Leader - Innovation Center
Bell Emergis - Sonaris
613-725-6582

 
 
 

Simple bash script help?

Post by Stev » Wed, 21 Oct 1998 04:00:00



Quote:

> Ok.  First, edit your .netrc file (in hour home dir)
> and add something like:

> thehostidumpto login yourlognamethere password yourpasswdthere

There are better ways, .netrc is a big security hole (your
password is sitting in an unencrypted text file!). I'm sorry
I can't help you to do it properly, but I thought I'd mention
that the above mentioned method is not safe.

Steve

 
 
 

Simple bash script help?

Post by Brian F. G. Biduloc » Wed, 21 Oct 1998 04:00:00



> There are better ways, .netrc is a big security hole (your
> password is sitting in an unencrypted text file!). I'm sorry
> I can't help you to do it properly, but I thought I'd mention
> that the above mentioned method is not safe.

Not much of a security hole if your userid is
`anonymous' and your password is you e-mail id.

--
Brian F. G. Bidulock, P. Eng.,
Group Leader - Innovation Center
Bell Emergis - Sonaris
613-725-6582

 
 
 

Simple bash script help?

Post by JD Weine » Wed, 21 Oct 1998 04:00:00



> There are better ways, .netrc is a big security hole (your
> password is sitting in an unencrypted text file!). I'm sorry
> I can't help you to do it properly, but I thought I'd mention
> that the above mentioned method is not safe.

    ESR has mentioned the same issue WRT the unencrypted
password in a .fetchmailrc.  His points are mainly that anyone
could still easily get the encryption routine from the program,
and they could then just sit there and try to force the password
- IF they can read the dotfile in question, which they shouldn't be
able to anyway.  He regards adding encryption as another layer
of complexity that only gives a false sense of security.  YMMV.

JD

 
 
 

Simple bash script help?

Post by Stefan David » Wed, 21 Oct 1998 04:00:00



>I'm trying to write a simple bash script to ftp a file at regular intervals
>to another site.

Bash, or indeed any shell that I know of, can't interact with subprocesses
in this way.

Quote:>My question is, how do I pass parameters to ftp when it's at the ftp>
>prompt?
>That is, I can do:-
>#!/bin/sh
>ftp remote_site
>which works
>but then how do I pass the cd, cp etc commands to ftp?
>I've read all I can find about shell programming but I must be missing
>something!!

You're not missing anything - it can't be done :-)

Either look into expect which is based on tcl and can interact with
subprocesses, or perl's Net::Ftp module which comes as part of the libnet
distribution from your favourite CPAN mirror.

You can find an example of using perl to upload a mirror of an entire
directory heirarchy to a remote ftp site on my home site.

HTH

Stefan

--
perl -we 'map { eval q( sub ).$_.q( { $_ = shift; $_ .= "). $_ .q("; print
qq($_\n); return($_); }) }((A..Z),(q(a)..q(z))); sub _ {return $_[0]." ";}
r(E(k(C(a(H(_(l(R(e(P(_(R(e(H(t(O(n(A(_(t(S(u(J())))))))))))))))))))))));'
             http://www.maths.nott.ac.uk/personal/smd/

 
 
 

Simple bash script help?

Post by Mengmeng Zha » Thu, 22 Oct 1998 04:00:00


: >I'm trying to write a simple bash script to ftp a file at regular intervals
: >to another site.

: Bash, or indeed any shell that I know of, can't interact with subprocesses
: in this way.

: >My question is, how do I pass parameters to ftp when it's at the ftp>
: >prompt?

: >That is, I can do:-

: >#!/bin/sh
: >ftp remote_site

: >which works

: >but then how do I pass the cd, cp etc commands to ftp?

: >I've read all I can find about shell programming but I must be missing
: >something!!

: You're not missing anything - it can't be done :-)

Hrm? I use a script which ftp's to one of my ftp accounts and transfers
a file with my IP address to it. Of course, you have to do something like
chmod 600 on it, or other people will see your password. If I remember
correctly (I'm not on my home box right now), the syntax is something like:

#!/bin/bash
ftp -ni blah.com <<eof
user blah
pass blah
get blah
put blah
...
...
eof

I think that's about how it works... not sure on the details.

HTH,
MZhang

--
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GM/CS d- s+: a--->? C++(+++) UL+(++) P+ L++(+++) E- W+(+++) N++ o+(++) K?

!r y?
------END GEEK CODE BLOCK------
Get your own Geek Code at http://www.geekcode.com
Visit the Z at http://www.math.swt.edu/~mz33062/

 
 
 

Simple bash script help?

Post by Stefan David » Thu, 22 Oct 1998 04:00:00


[ftping automatically from the shell]

Quote:>: You're not missing anything - it can't be done :-)
>Hrm? I use a script which ftp's to one of my ftp accounts and transfers
>a file with my IP address to it. Of course, you have to do something like
>chmod 600 on it, or other people will see your password. If I remember
>correctly (I'm not on my home box right now), the syntax is something like:
>#!/bin/bash
>ftp -ni blah.com <<eof
>user blah
>pass blah
>get blah
>put blah
>...
>...
>eof

Ok fair enough I didn't realise ftp would accept commands on stdin.

Quote:>I think that's about how it works... not sure on the details.

The problem with this approach is in the details :-) Because you don't
have two way communication with the ftp process all you're doing is
blindly shoving commands at it. You can't check the response code to any
of them.

With perl or expect you can. So if the ftp site is busy (for instance) you
can sleep() and try again later automatically. The above approach is
probably ok in certain simple circumstances; but IMHO The Right Way[tm] is
with perl or expect.

regards

Stefan

--
perl -we 'map { eval q( sub ).$_.q( { $_ = shift; $_ .= "). $_ .q("; print
qq($_\n); return($_); }) }((A..Z),(q(a)..q(z))); sub _ {return $_[0]." ";}
r(E(k(C(a(H(_(l(R(e(P(_(R(e(H(t(O(n(A(_(t(S(u(J())))))))))))))))))))))));'
             http://www.maths.nott.ac.uk/personal/smd/

 
 
 

Simple bash script help?

Post by James Johnso » Thu, 22 Oct 1998 04:00:00


Try ftpget, it is a nice command line version if ftp, it works well.  Do a search
on 'ftpget linux' to find the source.

BTW: You can also run ftp in the background, sleep, then see if ftp is still
running, and if so, kill it and try again.  you can probably even have ftp kill
your sleep command when it is done.

-Jim

Quote:

> With perl or expect you can. So if the ftp site is busy (for instance) you
> can sleep() and try again later automatically. The above approach is
> probably ok in certain simple circumstances; but IMHO The Right Way[tm] is
> with perl or expect.

 
 
 

Simple bash script help?

Post by bob » Thu, 22 Oct 1998 04:00:00


In response to my original post regarding bash script help.

Thanks everyone!

I now have a working system using several suggestions.

My script is:

#!bin/sh
while (echo"")
do
cd /path_to_my_files
my_program
ftp wingate 140 < ftp_commands
sleep 600
done

ftp_commands

cd /remote_path
binary
put my_file my_file_remote
close

NOTE: I have wingate running on NT port 140 as my ftp server

This works fine. I know it could be expanded to include error checking etc
but for my purposes it works (KISS!!).

Bob

 
 
 

Simple bash script help?

Post by Peter Marbai » Thu, 22 Oct 1998 04:00:00


Hallo Bob!
Am 20 Oct 98 schrieb Bob an All
zum Thema "Simple bash script help?":


 B> Hi

 B> I'm trying to write a simple bash script to ftp a file at regular
 B> intervals
 B> to another site.

 B> My question is, how do I pass parameters to ftp when it's at the
 ftp>>
 B> prompt?

 B> Thanks

 B> Bob
why dont you try

---------------------------------------------------------------
Seaching for sitecopy
---------------------------------------------------------------
Host http://sunsite.unc.edu
 - Page : http://sunsite.unc.edu/pub/Linux/apps/www/misc/sitecopy-0.2.tar.gz

---- gnerated with wwwoffle-sql scripts (c) Peter Marbaise ----
Release : 0.02 10/03/98

That is what i use to update an ftp or http site.

ciao Peter


FidoNet : 2:2452/110.20
WWW     : http://www.fido.de/~pema/

 
 
 

Simple bash script help?

Post by Jordan Conle » Sat, 24 Oct 1998 04:00:00


You should look into expect if you want error checking or just use
here-documents if you don't care

Jordan
9 days till I'm 18!!!


> Hi

> I'm trying to write a simple bash script to ftp a file at regular intervals
> to another site.

> My question is, how do I pass parameters to ftp when it's at the ftp>
> prompt?

> That is, I can do:-

> #!/bin/sh
> ftp remote_site

> which works

> but then how do I pass the cd, cp etc commands to ftp?

> I've read all I can find about shell programming but I must be missing
> something!!

> Thanks

> Bob

--
"In the beginning the Universe was created. This has made
many people very angry and has been widely regarded as a
bad move."
-Douglas Adams, Hitchhikers Guide to the Galaxy
 
 
 

Simple bash script help?

Post by Joi Elli » Wed, 28 Oct 1998 04:00:00


:>I'm trying to write a simple bash script to ftp a file at regular intervals
:>to another site.

: Bash, or indeed any shell that I know of, can't interact with subprocesses
: in this way.

:>My question is, how do I pass parameters to ftp when it's at the ftp>
:>prompt?

:>That is, I can do:-

:>#!/bin/sh
:>ftp remote_site

:>which works

:>but then how do I pass the cd, cp etc commands to ftp?

:>I've read all I can find about shell programming but I must be missing
:>something!!

: You're not missing anything - it can't be done :-)

Ahem.

#!/bin/bash
ftp tardis <<EOF
binary
get /tmp/trash
quit
EOF

--
Joi Ellis

 
 
 

1. simple bash script help

Redhat 7.3

I have no bash script skills at all - need all the help I
can get.

I want to convert a collection of mp3 files to wave or cdr.

Using a script ripped off from the cdwrite howto ( below )
the result is a wave file with two extensions ; eg "file.mp3.wav".
Obviously I want only ".wav".

                        _____________

#!/bin/bash

for I in *.mp3
do
        mpg321 --w - "$I"  > "$I".wav
done
                       -----------------------

Two script modifcations I received produce errors

script 1

#!/bin/bash
for I in *.mp3
do
        mpg321 --w - $I  >  ${I%.mp3}.wav
done

this one reports  "ambiguous redirect"

                  --------------
script 2

 #!/bin/bash
  for i in *.mp3; do
    mpg321 --w - "$i"  > `basename $i .mp3`.wav
  done

this one reports "basename: too many arguments"
probably due to the spaces in the filenames

I would appreciate any suggestion to modify either

--
faeychild

2. Calendar server error after restoring

3. BASH BASH BASH BASH BASH BASH BASH BASH BASH BASH

4. Unable to open /proc entry from root. Why ?

5. Simple Cshell script, doesn't work in Bash

6. File fragmentation on RAID-5 ?

7. Simple bash script to delete files

8. Apache 2.0.43 error on startup

9. Simple bash script ques.

10. Bash script simple problem

11. Trying to port a simple korn shell script to bash

12. simple UNTIL script hell in bash

13. simple Bash question: environment read from script?