Perl - what is the return value of a perl system function
I am playing around with the perl function system
, but I am confused as to what is the return value of this function. Consider the following examples:
[wakatana@arch ~]$ grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
[wakatana@arch ~]$ echo $?
0
[wakatana@arch ~]$ grep roots /etc/passwd
[wakatana@arch ~]$ echo $?
1
[wakatana@arch ~]$ perl -e 'my $code = system("grep root /etc/passwd 2>&1 1>/dev/null"); print $code . "\n"'
0
[wakatana@arch ~]$ perl -e 'my $code = system("grep roots /etc/passwd 2>&1 1>/dev/null"); print $code . "\n"'
256
Why does the second perl return 256 instead of 1?
Here's an excerpt from perldoc :
"A return value of -1 indicates a failure to start the program or an error in the wait (2) system call on request)"
It seems that something went wrong during the execution of the external command, so I also tried the following commands to figure out what was going on, but I am still at a loss.
# Seems that regular bash command, in this case exit, is equivalent to blablabla
[wakatana@arch ~]$ perl -e 'my $code = system("exit 2"); print $code . "\n"'
-1
[wakatana@arch ~]$ perl -e 'my $code = system(blablabla); print $code . "\n"'
-1
[wakatana@arch ~]$ perl -e 'my $code = system("grep roots /etc/passwd 2>&1 1>/dev/null") or die $!'
[wakatana@arch ~]$
Can someone explain this behavior.
@EDIT in response to: Paul Roub and ikegami
Mystery solved:
2[wakatana@arch ~]$ perl -e 'my $code = system("bash -c \"exit 2\""); print $code >> 8; print "\n"'
2
[wakatana@arch ~]$ perl -e 'my $code = system("exit 2"); print $! . "\n"'
No such file or directory
[wakatana@arch ~]$ perl -e 'my $code = system(blablabla); print $! . "\n"'
No such file or directory
[wakatana@arch ~]$ perl -e 'my $code = system("grep roots /etc/passwd 2>&1 1>/dev/null"); print $code >> 8; print "\n"'
1
+1
source to share
No one has answered this question yet
See similar questions:
or similar: