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.<br><br>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 <a href="http://src.opensolaris.org/source/xref/sfw/usr/src/cmd/bash/bash-3.2/tests/heredoc.tests">http://src.opensolaris.org/source/xref/sfw/usr/src/cmd/bash/bash-3.2/tests/heredoc.tests</a> (as open solaris adopts newer versions of bash the 3.2 will change a simple <a href="http://www.google.com/search?q=bash+heredoc+tests+opensolaris">http://www.google.com/search?q=bash+heredoc+tests+opensolaris</a> will find the new one)<br>
<br>#Technique 1#<br>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:<br> perl -e 'command'<br>
awk 'prog'<br> sed -e 'script'<br> mysql -e 'statement'<br>(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: <a href="http://dpaste.com/77724/">http://dpaste.com/77724/</a> 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.<br>
<br>#Technique 2#<br>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:<br>
python - << EOF<br>print "It's good!"<br>print "my home is $HOME"<br>
EOF<br><br>perl - << 'EOF'<br>
# notice the use of single quotes to protect this heredoc from bash expansion<br>my $msg = "It's good!";<br>
print $msg;EOF<br><br>
(mysql expect stdin to be a script by default)<br>mysql << EOF<br>mysql -u root -p << EOF<br>select now(), '$(date)', '$HOME';<br># notice that even subshells or back ticks can be used<br>EOF<br>
<br>#Technique 2.1"<br>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.<br>
<br>tmp=$(mktemp)<br>cat >$tmp<<'EOF'<br>s/bad/good/<br>EOF<br>rm $tmp<br><br>echo "It's bad" | perl -p -i -f $tmp<br><br>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.<br>
<br>I have modified your script below to use technique 2.1 I haven't tested it though. Enjoy.<br><br><div class="gmail_quote">On Mon, Aug 10, 2009 at 8:28 AM, Chuck Payne <span dir="ltr"><<a href="mailto:terrorpup@gmail.com">terrorpup@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Sorry if this question seem a bit silly, but I am still very much a<br>
newbie when it come some to scripting.<br>
<br>
I want to write a shell script that does some scp of files, but I like<br>
to use both bash and perl. Could I do some thing this, how would I<br>
got about mixing shell, python, perl and other things with in a<br>
script.<br>
<br></blockquote>
#!/bin/bash<br>
<br>
workdir=/tmp<br>
file="`hostname`.`date +%m%d%Y`.tgz"<br>
<br>
# Functions<br>
<br>
scpjvm () {<br>
<br>tmp=$(mktemp)<br>
cat >$tmp<<'EOF'<br>
use Net::SFTP;<br>
use strict;<br>
<br>
my $host = "<a href="http://mars.myhost.com" target="_blank">mars.myhost.com</a>";<br>
my %args = (<br>
user => 'toor,<br>
password => '1234567',<br>
debug => 'true'<br>
);<br>
<br>
my $sftp = Net::SFTP->new($host, %args);<br>
$sftp->get("/tmp/'jvm.`hostname`.`date +%m%d%Y`.tgz'",<br>
"/home/ia/'jvm.`hostname`.`date +%m%d%Y`.tgz'");<br>
EOF<br><br>/usr/bin/perl -f $tmp<br>
rm $tmp<br><br>
}<br>
<br>
# The work<br>
<br>
cd $workdir<br>
<br>
tar czvfpP $file /etc/httpd /opt/jboss/jboss/bin/<br>
/opt/jboss/jboss/server /usr/local/bin --exclude *log* --exclude<br>
*nohup*<br>
<br>
scpjvm<br>
<br>
rm $file<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
--<br>
----------------------------------------<br>
Old utilities do not die they just slowly fade away!<br>
-----------------------------------------<br>
OpenSUSE -- <a href="http://en.opensuse.org/User:Terrorpup" target="_blank">http://en.opensuse.org/User:Terrorpup</a><br>
OpenSuSE Ambassador<br>
OpenSuSE Member<br>
Skype -- terrorpup<br>
twitter -- terrorpup<br>
_______________________________________________<br>
Ale mailing list<br>
<a href="mailto:Ale@ale.org">Ale@ale.org</a><br>
<a href="http://mail.ale.org/mailman/listinfo/ale" target="_blank">http://mail.ale.org/mailman/listinfo/ale</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>.!# RichardBronosky #!.<br>