[ale] Question about scripting...

Richard Bronosky Richard at Bronosky.com
Mon Aug 10 10:11:49 EDT 2009


I mix bash with other langs all the time. There are 2.1 techniques to master
and you will scripting like pro in no time.

First, I'll go ahead and point out the obvious: What you have shown in your
example is a very unnecessary use of Perl. You should just be using the scp
command with public key authentication rather than passwords. But, lets move
past that because the desire to mix things like this is very valid. For
example, I mix bash+awk and bash+sed all the time. I highly recommend
everyone keep the bash heredoc tests handy
http://src.opensolaris.org/source/xref/sfw/usr/src/cmd/bash/bash-3.2/tests/heredoc.tests(as
open solaris adopts newer versions of bash the 3.2 will change a
simple
http://www.google.com/search?q=bash+heredoc+tests+opensolaris will find the
new one)

#Technique 1#
Use quoted/escaped scripts (aka: progs or commands) passed as an argument.
This is supported by most languages. You can find it in the SYNOPSIS of most
man pages. Here are a few examples:
    perl -e 'command'
    awk 'prog'
    sed -e 'script'
    mysql -e 'statement'
(Using -e for this is pretty common. Notice that awk is the odd man out
here.) The problem is that if you quote your script with double quotes, you
must escape all double quotes (and $var names that you don't want bash to
expand, but rather pass to perl) within it. If you use single quotes, no
bash expansion happens and you cannot escape out single quotes within. (That
warrants repeating.) You cannot escape single quotes with single quotes in
bash. You will go crazy trying: echo 'It\'s never going to work.' This
technique is find for oneliners and awk, but get tedious very quickly. This
is well suited for awk because it only uses double quotes as far as I've
ever seen. I shared an example of this with my team just this morning:
http://dpaste.com/77724/ The example on line 7 should work for anyone, the
one on line 3 uses servers that are not public. You may have to change gawk
to awk, depending on your platform.

#Technique 2#
Use bash heredoc syntax to feed your script into stdin. This will not work,
however if your script needs to process stdin, for that jump to technique
2.1 below. Most script interpreters that accept a script file as a command
line argument will interpret a hyphen "-" as an alias for stdin. Examples:
python - << EOF
print  "It's good!"
print "my home is $HOME"
EOF

perl - << 'EOF'
# notice the use of single quotes to protect this heredoc from bash
expansion
my $msg  = "It's good!";
print $msg;EOF

(mysql expect stdin to be a script by default)
mysql << EOF
mysql -u root -p << EOF
select now(), '$(date)', '$HOME';
# notice that even subshells or back ticks can be used
EOF

#Technique 2.1"
Use heredoc syntax to create your script as a temporary file and delete it
after execution. Why would you do this instead of just have 2 files? So that
your script has its requirements build in, and you can easily share it with
other members of your team. If having everything in one file wasn't
beneficial, we wouldn't have .tar, .jars, .egg, .gem, etc.

tmp=$(mktemp)
cat >$tmp<<'EOF'
s/bad/good/
EOF
rm $tmp

echo "It's bad" | perl -p -i -f  $tmp

I know it's overwhelming, but it works. I hope someone finds this useful. I
expect someone will since I saw a bunch of "you can't do that" replies come
in while I wrote this.

I have modified your script below to use technique 2.1 I haven't tested it
though. Enjoy.

On Mon, Aug 10, 2009 at 8:28 AM, Chuck Payne <terrorpup at gmail.com> wrote:

> Sorry if this question seem a bit silly, but I am still very much a
> newbie when it come some to scripting.
>
> I want to write a shell script that does some scp of files, but I like
> to use both bash and perl. Could I do some thing this,  how would I
> got about mixing shell, python, perl and other things with in a
> script.
>
> #!/bin/bash

workdir=/tmp
file="`hostname`.`date +%m%d%Y`.tgz"

# Functions

scpjvm () {

tmp=$(mktemp)
cat >$tmp<<'EOF'
use Net::SFTP;
use strict;

my $host = "mars.myhost.com";
my %args = (
   user => 'toor,
   password => '1234567',
   debug => 'true'
);

my $sftp = Net::SFTP->new($host, %args);
$sftp->get("/tmp/'jvm.`hostname`.`date +%m%d%Y`.tgz'",
"/home/ia/'jvm.`hostname`.`date +%m%d%Y`.tgz'");
EOF

/usr/bin/perl -f $tmp
rm $tmp

}

# The work

cd $workdir

tar czvfpP $file  /etc/httpd /opt/jboss/jboss/bin/
/opt/jboss/jboss/server /usr/local/bin --exclude *log* --exclude
*nohup*

scpjvm

rm  $file

>
> --
> ----------------------------------------
> Old utilities do not die they just slowly fade away!
> -----------------------------------------
> OpenSUSE -- http://en.opensuse.org/User:Terrorpup
> OpenSuSE Ambassador
> OpenSuSE Member
> Skype -- terrorpup
> twitter -- terrorpup
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
>



-- 
.!# RichardBronosky #!.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.ale.org/pipermail/ale/attachments/20090810/7e2a9a0c/attachment-0001.html 


More information about the Ale mailing list