Can a bourne shell script close the cmdtool it is running inside? - sh

I am on a sun ultra10 running solaris. I'm trying to alter a bourne shell script so that it will close the window (a cmdtool). Ive tried exit, exit -0, exit -1, system(exit), system("exit").
the script always just exits with the cmdtool still open.

As far as I know, this is not possible. A workaround would be not to alter the script, but how it is called:
./script && exit
This will close the shell window after the script has finished execution with return value 0. It may make sense to leave the window open if the script returned with an error, so that any error messages are preserved. If you want the window to close either way, just use a ; instead of the &&.

Related

prints in an another window in cmd and close quickley

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').

Ipython script stop on shell error

Does ipython have a setting similar to '-e' in bash that stops the execution of the script if any ipython's shell command returns a non-zero value?
Not directly, no. However, after a !foo shell command, the exit code is stored as the value _exit_code. So your script could check that and throw an error.
For some reason, on my system it multiplies all the exit codes by 256. I'm not at all sure why it's doing that.

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.

Returning an exit code from a shell script that was called from inside a perl script

I have a perl script (verifyCopy.pl) that uses system() to call a shell script (intercp.sh).
From inside the shell script, I have set up several exit's with specific exit codes and I'd like to be able to do different things based on which exit code is returned.
I've tried using $?, I have tried assigning the value of system("./intercp.sh") to a variable then checking the value of that, but the error message is always 0.
Is this because even though something inside the shell script fails, the actual script succeeds in running?
I tried adding a trap in the shell script (ie trap testexit EXIT and testexit() { exit 222; } but that didn't work either.
$? should catch the exit code from your shell script.
$ cat /tmp/test.sh
#!/bin/sh
exit 2
$ perl -E 'system("/tmp/test.sh"); say $?'
512
Remember that $? is encoded in the traditional manner, so $? >> 8 gives the exit code, $? & 0x7F gives the signal, and $? & 0x80 is true if core was dumped. See perlvar for details.
Your problem may be one of several things: maybe your shell script isn't actually exiting with the exit code (maybe you want set -e); maybe you have a signal handle for SIGCHLD eating the exit code; etc. Try testing with the extremely simple shell script above to see if its a problem in your perl script or your shell script.

How do I exit the command shell after it invokes a Perl script?

If I run a Perl script from a command prompt (c:\windows\system32\cmd.exe), how can I exit the command prompt after the script finishes executing.
I tried system("exit 0") inside the Perl script but that doesn't exit the cmd prompt shell from where the Perl script is running.
I also tried exit; command in the Perl script, but that doesn't work either.
Try to run the Perl script with a command line like this:
perl script.pl & exit
The ampersand will start the second command after the first one has finished. You can also use && to execute the second command only if the first succeeded (error code is 0).
Have you tried cmd.exe /C perl yourscript.pl ?
According to cmd.exe /? /C carries out the command specified by string and then terminates.
If you're starting the command shell just to run the perl script, the answer by Arkaitz Jimenez should work (I voted for it.)
If not, you can create a batch file like runmyscript.bat, with content:
#echo off
perl myscript.pl
exit
The exit will end the shell session (and as a side effect, end the batch script itself.)
You can start the program in a new window using the START Dos command. If you call that with /B then no additional window is created. Then you can call EXIT to close the current window.
Would that do the trick?
You can send a signal to the parent shell from Perl:
kill(9,$PARENT_PID);`
Unfortunately, the getppid() function is not implemented in Perl on windows so you'll have to find out the parent shell PID via some other means. Also, signal #9 might not be the best choice.