I have a question about using the emacs modes within a shell script. It's often so, that I wrote just some pieces of code in PHP for example like this:
#!/bin/ksh
export FILENAME=$1
php -r '
$xml = new SimpleXmlIterator(getenv("FILENAME"), null, true);
echo "//" . $xml->attributes()["name"] . "\n";
echo "struct " . $sxi->attributes()["type"] . " {\n";
php blabla
'
cat FILENAME | while read x
do
.... shell blabla
Because of the shebang the emacs mode is forced to shell mode, which is correct.
But is there a trick to use the php mode "within" the part where php code is written?
(without to kick out the php in its own script)
Related
I'm complete newby to perl and I hope you can help me with this line of code.
The issue is related to this one here, but it doesn't quite answer my question. I tried looking for it, but I just get more confused.
I have a txt input (batch) that I want to have a filename printed in the first line, but wrapped in a specific text. I am converting these files later into html and so I would like the .name to have "<div class="head">" printed before and "</div>" afterwards.
Here is the code I have and it works to print the name:
perl -i -pe 'BEGIN{undef $/;} s/^/$ARGV\n/' `find . -name '*.txt'`
I run this by first navigating to the directory where all the files are.
example of filename: 2016-05-20_18.32.08.txt
the files are plane text poetry and in the output i get:
./2016-05-20_18.32.08.txt
in the first line.
I tried something like this:
perl -i -pe 'BEGIN{undef $/;} s/^/$ARGV\n/' `find . -name ‘“<div class="head”>”’*.txt’”</div>”’
but of course it doesn't work. it just give me a >
I need to add the arguments in this part s/^/$ARGV\n/' but i already have troubles defining it.
Can you help pls?
In addition, the filename prints with ./ in the beginning, is there a simple way to exclude that?
perl -i -pe 'BEGIN{undef $/;} s/^/<div class=head> $ARGV <\/div>\n<div class=poem>\n/; s/$/\n<\/div>/' `find . -name '*.txt'`
This should work. But if you are new to perl, I suggest you try working with scripts rather than one-liners.
The -i flag will edit the file inplace. so if you want a html file, remove -i and redirect to another .html file.
I'm sure there is a more elegant way of doing it, but something like this will work
#!/usr/bin/perl
undef $/;
for (#ARGV){
open($fh,$_);
$content=<$fh>;
close($fh);
open($fh,">$_");
print $fh "<div class=\"head\">$_</head>\n$content";
close($fh)
}
so I'm receiving a Windows path as argument but since in the path there are special chars I'm having some trouble.
For example if the argument path ($1) is \test\bla the outout of the script it's "\SLE esla" (because of the \t and \b)
How can I print the correct path??
Thanks.
p.s.
maybe it's a stupid question but I'm new to ksh
as I said I'm new to ksh but here is the solution:
print -R $arg
Using print instead of echo make it possible to avoid the expansion of "\" with the "-R" option
I would like to use perl in a batch file then exit perl and continue with the batch code. A small example I would like to achieve :
perl do something
echo hello
pause
If Perl is installed, there should be no problem:
perl -e "print $_ for 1 .. 10"
perl script.pl
You might need to specify the full path to perl and the script.pl.
#echo off
perl script.pl
echo hello
pause
or
#echo off
perl -e" ...code..."
echo hello
pause
Maybe I'm just reading it wrong but I interpretted the question as asking how to embed perl scripts directly into batch files.
Activeperl adds a bunch of these as .cmd files and there is a tool on cpan for creating them from your perl script.
I also found two examples from a quick search that seem to extend the idea a little. They allow you to put the code into the same file and run dos commands before and after the perl call, which is what I thought you were asking.
Here's one of the examples:
#rem = 'Perl, ccperl will read this as an array of assignment & skip this block
#CD /d "%~dp0"
#perl -s "%~nx0" %*
#FOR /L %%c in (4,-1,1) do #(TITLE %~nx0 - %%cs to close & ping -n 2 -w 1000 127.0.0.1 NUL)
#TITLE Press any key to close the window&ECHO.&GOTO:EOF
#rem ';
#perl script starts below here
print 'Hi there! DOS rocks!\n'
You can also do vice-versa. i.e. Call Batch commands from perl
my $cmd = 'some command';
if (system $cmd) {
print "Error: $? for command $cmd"
}
Save this perl script. This should do your job as well.
I need to write a CGI program and it will display the output of a system command:
script.sh
echo "++++++"
VAR=$(expect -c " spawn ssh -o StrictHostKeyChecking=no $USER#$HOST $CMD match_max
100000 expect \"*?assword:*\" send -- \"$PASS\r\" send -- \"\r\" expect eof ")
echo $VAR
echo "++++++"
In CGI file:
my $command= "ksh ../cgi-bin/script.sh";
my #output= `$command`;
print #output;
Finally, when I run the CGI file in unix, the $VAR is a very long string including \n and some delimiters. However, when I run on web server, the output is
++++++
++++++
So $VAR is missing when passing in the web interface/browser.
I know maybe the problem is $VAR is very long string.
But anyway, is there anyway to solve this problem except writing the output to a file then retrieve it from browser?
Thanks if you are interested in my question.
script.sh uses several environment variables: $USER, $HOST, $CMD and $PASS. The CGI environment will have different environment variables set than a login shell. You may need to set these variables from your CGI script before calling script.sh.
Try finding where commands like expect and ssh that you are calling are on your system and adding their directory paths to the PATH used by your script.
I.e.
which expect
returns /usr/bin/expect then add the line:
PATH=$PATH:/usr/bin && export PATH
near the beginning of the ksh script. During debug you may also want to redirect stderr to a file by appending 2>/tmp/errors.txt to the end of your command since stderr is not shown in the browser.
my $command= "ksh ../cgi-bin/script.sh 2>/tmp/errors.txt";
I am trying to send a get or a post through a command-line argument. That is test the script in the command line before I test through a browser (the server has issues). I tried searching online, and I suppose I was probably using incorrect terminology because I got nothing. I know this is possible because I saw someone do it. I just don't remember how it was done.
Thanks! :)
To test a CGI program from the command line, you fake the environment that the server creates for the program. CGI.pm has a special offline mode, but often I find it easier not to use because of the extra setup I need to do for everything else my programs typically expect.
Depending on the implementation of your script, this involves setting many environment variables, which you can do from a wrapper script that pretends to be the server:
#!/bin/bash
export HTTP_COOKIE=...
export HTTP_HOST=test.example.com
export HTTP_REFERER=...
export HTTP_USER_AGENT=...
export PATH_INFO=
export QUERY_STRING=$(cat query_string);
export REQUEST_METHOD=GET
perl program.cgi
If you're doing this for a POST request, the environment is slightly different and you need to supply the POST data on standard input:
#!/bin/bash
export CONTENT_LENGTH=$(perl -e "print -s q/post_data/");
export HTTP_COOKIE=...
export HTTP_HOST=test.example.com
export HTTP_REFERER=...
export HTTP_USER_AGENT=...
export PATH_INFO=...
export QUERY_STRING=$(cat query_string);
export REQUEST_METHOD=POST
perl program.cgi < post_data
You can make this as fancy as you need and each time you want to test the program, you change up the data in the query_string or post_data files. If you don't want to do this in a shell script, it's just as easy to make a wrapper Perl script.
Are you using the standard CGI module?
For example, with the following program (notice -debug in the arguments to use CGI)
#! /usr/bin/perl
use warnings;
use strict;
use CGI qw/ :standard -debug /;
print "Content-type: text/plain\n\n",
map { $_ . " => " . param($_) . "\n" }
param;
you feed it parameters on the command line:
$ ./prog.cgi foo=bar baz=quux
Content-type: text/plain
foo => bar
baz => quux
You can also do so via the standard input:
$ ./prog.cgi
(offline mode: enter name=value pairs on standard input; press ^D or ^Z when done)
foo=bar
baz=quux
^D
Content-type: text/plain
foo => bar
baz => quux
Old discussion, but I was looking for the same answers - so for those who follow - this is what I found out
RTFM! from the CGI man page ( and there is more )
DEBUGGING
If you are running the script from the command line or in the perl
debugger, you can pass the script a list of keywords or parameter=value
pairs on the command line or from standard input (you don't have to
worry about tricking your script into reading from environment
variables). You can pass keywords like this:
your_script.pl keyword1 keyword2 keyword3
or this:
your_script.pl keyword1+keyword2+keyword3
or this:
your_script.pl name1=value1 name2=value2
or this:
your_script.pl name1=value1&name2=value2
To turn off this feature, use the -no_debug pragma.
If you don't want to alter the perl script, you can call it with at least two environment variables set, as others mentioned already. To simulate a GET request:
shell$ QUERY_STRING=limit=20 REQUEST_METHOD=GET ./events_html.pl
That's the console shortcut for www.myserver.org/events_html.pl?limit=20
Yes, it's possible to do this from the command line, bypassing your server. This page explains all: Perl CGI debugging (sitewizard.com) (Especially item 6 on that page). Here I quote the most important part:
To test the script offline using the
GET method, simply set the
QUERY_STRING environment variable
accordingly. If you are using Windows,
you might use the following command
line in a DOS window prior to running
the script in the same window:
set QUERY_STRING=recipient=John#Doe.com&Fullname=M+Name
To test the script offline using the
POST method, put the line below into a
text file named, say, testinput.txt.
recipient=John#Doe.com&Fullname=M+Name
Then redirect that file as an input to
the script. On Unix systems as well as
under Windows' MSDOS prompt, you can
do it this way:
perl -w scriptname.pl < testinput.txt
Your script will then receive that
input as though it was sent it by a
form on the website. Check the error
messages that perl spouts, if any, to
help you track the problem in the
script.
To give a cgi script post data:
$ echo -n 'a=b;c=d' | REQUEST_METHOD=POST CONTENT_LENGTH=999 perl index.cgi
To give a cgi script get data:
$ perl index.cgi 'a=b;c=d'
LWP comes with ready made scripts that can be used from the command-line. Check for GET and POST scripts in your system.
In Windows, you can use VBScript to write a command line util that calls into the MS XML library:
Dim XMLHttp : Set XMLHttp = CreateObject("Microsoft.XMLHTTP")
On Error Resume Next
strIPAddress = WScript.Arguments(0)
strMACAddress = WScript.Arguments(1)
strSubnetMask = WScript.Arguments(2)
On Error Goto 0
WScript.Echo "Attempting to wake host " & strIPAddress & " on NIC " & strMACAddress &
"using netmask " & strSubnetMask
strGetUrl = http://wolService/WolService/WolService.asmx/WakeBroadcast?hostIP=" &
strIPAddress & "&macAddress=" & strMACAddress & "&subnetMask=" & strSubnetMask
XMLHttp.Open "GET", strGetUrl, False
XMLHttp.Send ""
WScript.Echo XMLHttp.ResponseText
Edit: This script sends HTTP requests and can be used from the command line. I got confused by the question 'How can I send POST and GET data to a Perl CGI script via the command line' and thought this was about sending POST and GET data to a Perl CGI script via the command line from an unspecified client OS.