prints in an another window in cmd and close quickley - perl

I am trying to run a Perl script from command prompt.
The script contains one line:
print "Hello World!\n"
I type in the cmd: Perl hello.pl
The line is printed in a new window and quickly is closed.
It's all happening in the cmd! Does anyone had this kind of problem?
I know Perl is working because I tried to run a script that creates an excel file and it worked.
The only problem is, that it doesn't print in the same window as it is supposed to do, but opens a new window, prints there and closes it. (I tried to do a while loop in the end and it didn't help).

I was able to solve this.
In windows there is an option called "Open command prompt as Administrator". A new window does not open up in that case.

The cmd window closes as soon as the command that it runs has exited. You can either
… start a cmd.exe of your own, and launch your script via
> perl C:\path\to\script.pl
instead of double-clicking the perl file (or whatever you are doing to start it). This should not start a new window.
… or you could have the script wait until you have read the message. Just wait for user input of some sort before exiting, e.g. like
<>; # read and discard a line to exit
at the bottom of your script.
You can also use the pause program for this, which you can execute like system('pause').

Related

powershell running the code instead of create a new line

I think that my question is something too easy that you guys will solve in 1 minute.
I'm trying to run a script that have multiple lines of code. But, when I write the first line and hits SHIFT+ENTER it runs the code. I need to write a new line, instead of running what I've wrote.
Anybody knows what should I do (instead killing myself because I'm too dumb) ?
In powershell console there are a few ways to make a new line
A. Shift + Enter : Use this at any point to make a new line
B. The opening of a string " or ' until the closing of the string " or ' : use this when you have a string that you wish to span many lines
C. A pipe | : Use this if you have output that you would like to pass to another command
D. The Back tick (escape char) ` : use this to separate lines for a new command or splitting a command into other lines
If you are new to powershell, I would suggest using Powershell ISE. If its installed you can go to the powershell console and type ISE or go to start and type Powershell ISE. This will be a good place to run scripts and debug as you can add breakpoints to your scripts.
The easiest and best way to do this would be to create the script inside of the PowerSheell ISE program. You can then reference this script and run it in the console by preceding it with a .\script.ps1.
If needed you can create script on the command line by creating and writing to the file from the console.
Open the PowerShell console
Run the following command to create a file New-Item script.ps1
Run the next command as many times as it takes to populate the file Add-Content script.ps1 "My code line here"
Run the code using the script run command .\script.ps1
Now let it be known that the ISE is a much better tool because it allows for debugging of files and testing them on demand. The only downside is it will cache whatever it uses or creates (such as variables or references). If you aren't getting the expected result trying closing and reopening to clear the cache run it from the console in tandem. One last thing to note is that if you use the ISE and it successfully runs there that doesn't mean it will run in the console. Be sure to test thoroughly.

How to run a applescript to enter a terminal command

I have been messing around with voice commands, but ran into a snag. I am trying to get a terminal command to run but it is not working. The command makes asterisks "snow" fall.
This is what I have so far.
tell application "Terminal"
activate
run script "ruby -e 'C=`stty size`.scan(/\d+/)[1].to_i;S=["2743".to_i(16)].pack("U*");a={};puts "\033[2J";loop{a[rand(C)]=0;a.each{|x,o|;a[x]+=1;print "\033[#{o};#{x}H \033[#{a[x]};#{x}H#{S} \033[0;0H"};$stdout.flush;sleep 0.1}'"
end tell
All I get are errors
Command line scripts executed with the do shell script command. The string escaping can get a bit gnarly, so be careful with that too. Here's a simple example:
do shell script "say \"Today is `php -r \"echo date('l');\"`\""
EDIT:
OK, I just realised your script actually depends on having a Terminal window to run in, so the usual approach of do shell script won't work here.
There are still a lot of unescaped quotation marks in your Applescript, but rather than fixing those, I think it would be easier to put the whole ruby script into a stand-alone file and pass that to Terminal instead.
stars.rb
#!/usr/bin/ruby
C=`stty size`.scan(/\d+/)[1].to_i;
S=["2743".to_i(16)].pack("U*");
a={};
puts "\033[2J";
loop {
a[rand(C)]=0;
a.each {
|x,o|;
a[x]+=1;
print "\033[#{o};#{x}H \033[#{a[x]};#{x}H#{S} \033[0;0H"
};
$stdout.flush;
sleep 0.1
}
AppleScript
tell application "Terminal"
activate
do script "~/stars.rb"
end tell
An easy way to escape a shell command for AppleScript is to save the command in a text file. Run the script below and copy the Result.
set myText to read (choose file) as «class utf8»

Cmd.exe no popup

I have to run cmd / c from a program, run the start command xx.exe, and I capture the result (there xx.exe?). until everything is right, however, remains open the console with the error popup. how can I close the console with the error?
Usually win32 applications will close the command prompt after execution. If this isn't the case with what you're trying to run, you could:
Run it from Windows "Run" option (Windows button+R) than your program name and path in prompt.
Run it from a batch file, like so:
runMe.bat:
START "" "C:\windows\notepad.exe"
EXIT`
Than just run runMe.bat from wherever. Notice the 'exit' command that closes the command prompt after execution.
Read more about batch files, the start command, and this issue here, and there.
Good luck!

Perl Strawberry: How to run a perl script by clicking

I'm working on Windows 7 and I have installed Strawberry. I would like to run a perl skript (test.pl):
open OUTPUT, ">test.txt";
print OUTPUT "This is a test\n";
by just clicking on the file or redirect with left mouse click to Perl-program (open with/perl.exe). When I do this a console opens for less than a second and disappears but the file test.txt is not created. If I go to the MS command and enter
> C:\myplace>perl test.pl
it works. I never had this experience before (WinXP, other Windows 7 PC with ActivePerl and Windows 8 with strawberry). I would be very happy if somebody could give me a hint how to solve this problem.
There are two problems here:
Creating the file where you want it. When double-clicking a perl script to launch it, it is not executed in the context of the folder you have opened in Explorer. If you want to specify an explicit context, do the following near the top of your script:
use FindBin; # This is a module that finds the location of your script
BEGIN { chdir $FindBin::Bin } # set context to that directory.
When you then create a new file without an aboslute path, the path is considered relative to that directory.
You do not have the problem when running the script from the command line, because you have specified the correct path. But if you run it from C:\ like
C:\> perl myplace/test.pl
then you have created the file in C\test.txt. The FindBin solution fixes this.
When running a script by double-clicking it, the command line exits before you can inspect the output. This “problem” is shared by all programming languages on Windows. You can force the window to stay open by waiting for some input. You can either do
system("PAUSE"); # not portable to non-Windows!
or
warn "Press enter to exit...\n";
<STDIN>; # Read a line from the command line and discard it.
# Feels akward when launching from the command line
to wait until an Enter ⏎ is pressed .
The other solution is to always use the command line for your scripts, which is what I'd actually suggest.
Check what is your script executing folder, as it might differ from C:\myplace
use Cwd;
print getcwd();
sleep 7;

Odd behavior with Perl system() command

Note that I'm aware that this is probably not the best or most optimal way to do this but I've run into this somewhere before and I'm curious as to the answer.
I have a perl script that is called from an init that runs and occasionally dies. To quickly debug this, I put together a quick wrapper perl script that basically consists of
#$path set from library call.
while(1){
system("$path/command.pl " . join(" ",#ARGV) . " >>/var/log/outlog 2>&1");
sleep 30; #Added this one later. See below...
}
Fire this up from the command line and it runs fine and as expected. command.pl is called and the script basically halts there until the child process dies then goes around again.
However, when called from a start script (actually via start-stop-daemon), the system command returns immediately, leaving command.pl running. Then it goes around for another go. And again and again. (This was not fun without the sleep command.). ps reveals the parent of (the many) command.pl to be 1 rather than the id of the wrapper script (which it is when I run from the command line).
Anyone know what's occurring?
Maybe the command.pl is not being run successfully. Maybe the file doesn't have execute permission (do you need to say perl command.pl?). Maybe you are running the command from a different directory than you thought, and the command.pl file isn't found.
There are at least three things you can check:
standard error output of your command. For now you are swallowing it by saying 2>&1. Remove that part and observe what errors the system command produces.
the return value of system. The command may run and system may still return an exit code, but if system returns 0, you know the command was successful.
Perl's error variable $!. If there was a problem, Perl will set $!, which may or may not be helpful.
To summarize, try:
my $ec = system("command.pl >> /var/log/outlog");
if ($ec != 0) {
warn "exit code was $ec, \$! is $!";
}
Update: if multiple instance of the command keep showing up in your ps output, then it sounds like the program is forking and running itself in the background. If that is indeed what the command is supposed to do, then what you do NOT want to do is run this command in an endless loop.
Perhaps when run from a deamon the "system" command is using a different shell than the one used when you are running as yourself. Maybe the shell used by the daemon does not recognize the >& construct.
Instead of system("..."), try exec("...") function if that works for you.