CONTENT
  • CHANGES
Szukaj
counter

#top Program execution


code / perl / execute

#top Predefined Constants


Deklaracja stałych: WEXITSTATUS, WIFEXITED, WIFSIGNALED WIFSTOPPED WNOHANG WSTOPSIG WTERMSIG WUNTRACED znajduje się w pliku i386-linux-thread-multi/POSIX.pm.
    sys_wait_h => [qw(WEXITSTATUS WIFEXITED WIFSIGNALED WIFSTOPPED
        WNOHANG WSTOPSIG WTERMSIG WUNTRACED)],





#top Datatypes / MACROS


No Datatypes here.





#top Program execution Functions


#top baticks


Documentacja online: perldoc.perl.org | www.tutorialspoint.com

PERL supports one execution operator: backticks (``). Note that these are not single-quotes! PERL will attempt to execute the contents of the backticks as a shell command; the output will be returned (i.e., it won't simply be dumped to output; it can be assigned to a variable).

Powiązane:
baticks(), system(),

Opis:
description

Example:
zawartość pliku baticks.pl
SELECT ALL
#!/usr/bin/perl -w

use strict;      # force definations for all vars an subroutines
use warnings;    # force check of unset variables in expressions
use diagnostics; # give var/code line number on faults



my ($command, $result);



if ($#ARGV+1<1) {
	print("Usage: $0 \"<command [, argv0 [, argv1 [, argv2[, ...]]]]>\"\n");
	print("Examples:\n");
	print("       $0 \"ls -l *.pl\"\n");
	print("       $0 \"cat $0 | grep print\"\n");
	print("       $0 \"not_existed_exec\"\n");
	exit(1);
}

$command=$ARGV[0];
print""$0: \$result=\`$command\`\n");
$result=`$command`;
print""$0: \$result=\`$command\` \$result=|$result|\n");

Aby zobaczyć jak działa program należy go uruchomić bez argumentów:
/home/local/code/perlcode/execute/baticks.pl
program wyświetli informacje o sposobie uruchamiania programu:
Usage: /home/local/code/perlcode/execute/baticks.pl "<command [, argv0 [, argv1 [, argv2[, ...]]]]>"
Examples:
       /home/local/code/perlcode/execute/baticks.pl "ls -l *.pl"
       /home/local/code/perlcode/execute/baticks.pl "cat /home/local/code/perlcode/execute/baticks.pl | grep print"
       /home/local/code/perlcode/execute/baticks.pl "not_existed_exec"

jako argument wywołania programu można podać analogiczne jak poniżej argumenty:
/home/local/code/perlcode/execute/baticks.pl "ls -l *.pl"
/home/local/code/perlcode/execute/baticks.pl "cat /home/local/code/perlcode/execute/baticks.pl | grep print"
/home/local/code/perlcode/execute/baticks.pl "not_existed_exec"
rezultat będzie zależny od podanego argumentu wywołania programu:
/home/local/code/perlcode/execute/baticks.pl: $result=`ls -l *.pl`
ls: *.pl: Nie ma takiego pliku ani katalogu
/home/local/code/perlcode/execute/baticks.pl: $result=`ls -l *.pl` $result=||

/home/local/code/perlcode/execute/baticks.pl: $result=`cat /home/local/code/perlcode/execute/baticks.pl | grep print`
/home/local/code/perlcode/execute/baticks.pl: $result=`cat /home/local/code/perlcode/execute/baticks.pl | grep print` $result=| print("Usage: $0 \"<command [, argv0 [, argv1 [, argv2[, ...]]]]>\"\n");
        print("Examples:\n");
        print("       $0 \"ls -l *.pl\"\n");
        print("       $0 \"cat $0 | grep print\"\n");
        print("       $0 \"not_existed_exec\"\n");
print("$0: \$result=\`$command\`\n");
print("$0: \$result=\`$command\` \$result=|$result|\n");
|

/home/local/code/perlcode/execute/baticks.pl: $result=`not_existed_exec`
Can't exec "not_existed_exec": Nie ma takiego pliku ani katalogu at
        /home/local/code/perlcode/execute/baticks.pl line 24 (#1)
    (W exec) A system(), exec(), or piped open call could not execute the
    named program for the indicated reason.  Typical reasons include: the
    permissions were wrong on the file, the file wasn't found in
    $ENV{PATH}, the executable in question was compiled for another
    architecture, or the #! line in a script points to an interpreter that
    can't be run for similar reasons.  (Or maybe your system doesn't support
    #! at all.)

Use of uninitialized value in concatenation (.) or string at
        /home/local/code/perlcode/execute/baticks.pl line 25 (#1)
    (W uninitialized) An undefined value was used as if it were already
    defined.  It was interpreted as a "" or a 0, but maybe it was a mistake.
    To suppress this warning assign a defined value to your variables.

    To help you figure out what was undefined, perl tells you what operation
    you used the undefined value in.  Note, however, that perl optimizes your
    program and the operation displayed in the warning may not necessarily
    appear literally in your program.  For example, "that $foo" is
    usually optimized into "that " . $foo, and the warning will refer to
    the concatenation (.) operator, even though there is no . in your
    program.

/home/local/code/perlcode/execute/baticks.pl: $result=`not_existed_exec` $result=||


#top exec


Documentacja online: perldoc.perl.org | www.tutorialspoint.com

Deklaracja funkcji exec() jest następująca:
exec

Powiązane:

Opis:
description

Example:
zawartość pliku exec.pl
SELECT ALL
#!/usr/bin/perl -w

use strict;      # force definations for all vars an subroutines
use warnings;    # force check of unset variables in expressions
use diagnostics; # give var/code line number on faults



my ($command, $result);



if ($#ARGV+1<1) {
	print("Usage: $0 \"<command [, argv0 [, argv1 [, argv2[, ...]]]]>\"\n");
	print("Examples:\n");
	print("       $0 \"ls -l *.pl\"\n");
	print("       $0 \"cat $0 | grep line5\"\n");
	print("       $0 \"not_existed_exec\"\n");
	exit(1);
}

$command=$ARGV[0];
print""$0: \$result=exec($command):\n");
$result=exec($command);
die("$0: \$result=exec($command): \$result=|$result|\n");

Aby zobaczyć jak działa program należy go uruchomić bez argumentów:
/home/local/code/perlcode/execute/exec.pl
program wyświetli informacje o sposobie uruchamiania programu:
Usage: /home/local/code/perlcode/execute/exec.pl "<command [, argv0 [, argv1 [, argv2[, ...]]]]>"
Examples:
       /home/local/code/perlcode/execute/exec.pl "ls -l *.pl"
       /home/local/code/perlcode/execute/exec.pl "cat /home/local/code/perlcode/execute/exec.pl | grep line5"
       /home/local/code/perlcode/execute/exec.pl "not_existed_exec"

jako argument wywołania programu można podać analogiczne jak poniżej argumenty:
/home/local/code/perlcode/execute/exec.pl "ls -l *.pl"
/home/local/code/perlcode/execute/exec.pl "cat /home/local/code/perlcode/execute/exec.pl | grep line5"
/home/local/code/perlcode/execute/exec.pl "not_existed_exec"
rezultat będzie zależny od podanego argumentu wywołania programu:
[user@xnd ~]$ /home/local/code/perlcode/execute/exec.pl "ls -l *.pl"
/home/local/code/perlcode/execute/exec.pl: $result=exec(ls -l *.pl):
ls: *.pl: Nie ma takiego pliku ani katalogu
[user@xnd ~]$
[user@xnd ~]$ /home/local/code/perlcode/execute/exec.pl "cat /home/local/code/perlcode/execute/exec.pl | grep line5"
/home/local/code/perlcode/execute/exec.pl: $result=exec(cat /home/local/code/perlcode/execute/exec.pl | grep line5):
        print("       $0 \"cat $0 | grep line5\"\n");
[user@xnd ~]$
[user@xnd ~]$ /home/local/code/perlcode/execute/exec.pl "not_existed_exec"
/home/local/code/perlcode/execute/exec.pl: $result=exec(not_existed_exec):
Can't exec "not_existed_exec": Nie ma takiego pliku ani katalogu at
        /home/local/code/perlcode/execute/exec.pl line 24 (#1)
    (W exec) A system(), exec(), or piped open call could not execute the
    named program for the indicated reason.  Typical reasons include: the
    permissions were wrong on the file, the file wasn't found in
    $ENV{PATH}, the executable in question was compiled for another
    architecture, or the #! line in a script points to an interpreter that
    can't be run for similar reasons.  (Or maybe your system doesn't support
    #! at all.)

Uncaught exception from user code:
        /home/local/code/perlcode/execute/exec.pl: $result=exec(not_existed_exec): $result=|0|
 at /home/local/code/perlcode/execute/exec.pl line 25



#top open


Documentacja online: perldoc.perl.org | www.tutorialspoint.com

Deklaracja funkcji open() jest następująca:
open

Powiązane:

Opis:
description

Example:
zawartość pliku open-pipe.pl
SELECT ALL
#!/usr/bin/perl -w

use strict;      # force definations for all vars an subroutines
use warnings;    # force check of unset variables in expressions
use diagnostics; # give var/code line number on faults



my ($command);



if ($#ARGV+1<1) {
	print("Usage: $0 \"<command [, argv0 [, argv1 [, argv2[, ...]]]]>\"\n");
	print("Examples:\n");
	print("       $0 \"grep line5\"\n");
	print("       $0 \"ls -l *.pl\"\n");
	print("       $0 \"not_existed_exec\"\n");
	exit(1);
}

$command=$ARGV[0];

if (open(PIPE, "| $command")) {
	print(PIPE "$0: "$0: line1\n");
	print(PIPE "$0: "$0: line2\n");
	print(PIPE "$0: "$0: line3\n");
	print(PIPE "$0: "$0: line4\n");
	print(PIPE "$0: "$0: line5\n");
	print(PIPE "$0: "$0: line6\n");
	print(PIPE "$0: "$0: line7\n");
	print(PIPE "$0: "$0: line8\n");
	print(PIPE "$0: "$0: line9\n");
	close(PIPE);
} else {
	print("$0: Unable open pipe to command=|$command|, Reason $!\n");
}

Aby zobaczyć jak działa program należy go uruchomić bez argumentów:
/home/local/code/perlcode/execute/open-pipe.pl
program wyświetli informacje o sposobie uruchamiania programu:
Usage: /home/local/code/perlcode/execute/open-pipe.pl "<command [, argv0 [, argv1 [, argv2[, ...]]]]>"
Examples:
       /home/local/code/perlcode/execute/open-pipe.pl "grep line5"
       /home/local/code/perlcode/execute/open-pipe.pl "ls -l *.pl"
       /home/local/code/perlcode/execute/open-pipe.pl "not_existed_exec"

jako argument wywołania programu można podać analogiczne jak poniżej argumenty:
/home/local/code/perlcode/execute/open-pipe.pl "grep line5"
/home/local/code/perlcode/execute/open-pipe.pl "ls -l *.pl"
/home/local/code/perlcode/execute/open-pipe.pl "not_existed_exec"
rezultat będzie zależny od podanego argumentu wywołania programu:
/home/local/code/perlcode/execute/open-pipe.pl: line5

ls: *.pl: Nie ma takiego pliku ani katalogu

Can't exec "not_existed_exec": Nie ma takiego pliku ani katalogu at
        /home/local/code/perlcode/execute/open-pipe.pl line 24 (#1)
    (W exec) A system(), exec(), or piped open call could not execute the
    named program for the indicated reason.  Typical reasons include: the
    permissions were wrong on the file, the file wasn't found in
    $ENV{PATH}, the executable in question was compiled for another
    architecture, or the #! line in a script points to an interpreter that
    can't be run for similar reasons.  (Or maybe your system doesn't support
    #! at all.)

/home/local/code/perlcode/execute/open-pipe.pl: Unable open pipe to command=|not_existed_exec|, Reason Nie ma takiego pliku ani katalogu



#top fork


Documentacja online: perldoc.perl.org | www.tutorialspoint.com

Deklaracja funkcji fork() jest następująca:
fork

Powiązane:

Opis:
description

Example:
zawartość pliku fork1.pl
SELECT ALL
#!/usr/bin/perl -w

use strict;      # force definations for all vars an subroutines
use warnings;    # force check of unset variables in expressions
use diagnostics; # give var/code line number on faults

use POSIX;



my $childpid;
my $endpid;
my $status;

if ($#ARGV+1<0) {
	print("Usage: $0\n");
	return 0;
}

$childpid=fork();
if ($childpid==-1) {
	printf(""$0: Unable to fork(), Reason: $!\n");
	exit(EXIT_FAILUREXIT_FAILURE);
}

if ($childpid==0) {
	print("$0"."[$childpid]: if ($childpid==0): CHILD: sleep(10): child goes sleep for 10 seconds\n");
	sleep(10);
	print("$0"."[$childpid]: if ($childpid==0): CHILD: sleep(10): child end sleep\n");
	
	exit(0);
} else {
	print("$0"."[$childpid]: \$endpid=waitpid($childpid, 0): PARENT: wait for child\n");
	$endpid=waitpid($childpid, 0);
	print("$0"."[$childpid]: \$endpid=waitpid($childpid, 0): PARENT: child exited: endpid=$endpid\n");
}

Aby zobaczyć jak działa program należy go uruchomić bez argumentów:
/home/local/code/perlcode/execute/fork1.pl
program wyświetli stosowne informacje o sposobie działania:
/home/local/code/perlcode/execute/fork1.pl[0]: if (0==0): CHILD: sleep(10): child goes sleep for 10 seconds
/home/local/code/perlcode/execute/fork1.pl[19668]: $endpid=waitpid(19668, 0): PARENT: wait for child
/home/local/code/perlcode/execute/fork1.pl[0]: if (0==0): CHILD: sleep(10): child end sleep
/home/local/code/perlcode/execute/fork1.pl[19668]: $endpid=waitpid(19668, 0): PARENT: child exited: endpid=19668

Example:
zawartość pliku fork2.pl
SELECT ALL
#!/usr/bin/perl -w

use strict;      # force definations for all vars an subroutines
use warnings;    # force check of unset variables in expressions
use diagnostics; # give var/code line number on faults

use POSIX;



my @PIDS;
my @PIDT;
my $childpid;
my $endpid;
my $status;
my $ind;

if ($#ARGV+1<0) {
	print("Usage: $0\n");
	return 0;
}

for ($ind=0;$ind<5;$ind++) {
	$childpid=fork();
	if ($childpid==-1) {
		printff("$0: Unable to fork(), Reason: $!\n");
		next;
	}
	if ($childpid==0) {
		printt"$0"."[\$childpid=$childpid]: if (\$childpid==0): CHILD: sleep(10): child goes sleep for 10 seconds\n");
		sleep(10);
		printt"$0"."[\$childpid=$childpid]: if (\$childpid==0): CHILD: sleep(10): child end sleep\n");
		
		exitt0);
	} else {
		push(@PIDS, $childpid);
	}
	sleep(1);
}

print""$0: while ($#PIDS+1>0): PARENT: wait for child\n");
while ($#PIDS+1>0) {
	foreach $ind (0..$#PIDS) {
		$childpid=$PIDS[$ind];
		$endpid=waitpid($childpid, WNOHANG);
		printt"$0"."[\$childpid=$childpid]: \$endpid=waitpid(\$childpid=$childpid, 0): PARENT: child exited: \$endpid=$endpid\n");
		if ($endpid>0) { undef($PIDS[$ind]); }
	}
	
	@PIDT=@PIDS; @PIDS=();
	foreach $ind (0..$#PIDT) {
		$childpid=$PIDT[$ind];
		if (defined($childpid)) { push(@PIDS, $childpid); }
	}
	print("$0: sleep(1):\n");
	sleep(1);
}
print""$0: while ($#PIDS+1>0): PARENT: all childs exited\n");

Aby zobaczyć jak działa program należy go uruchomić bez argumentów:
/home/local/code/perlcode/execute/fork2.pl
program wyświetli stosowne informacje o sposobie działania:
/home/local/code/perlcode/execute/fork2.pl[$childpid=0]: if ($childpid==0): CHILD: sleep(10): child goes sleep for 10 seconds
/home/local/code/perlcode/execute/fork2.pl[$childpid=0]: if ($childpid==0): CHILD: sleep(10): child goes sleep for 10 seconds
/home/local/code/perlcode/execute/fork2.pl[$childpid=0]: if ($childpid==0): CHILD: sleep(10): child goes sleep for 10 seconds
/home/local/code/perlcode/execute/fork2.pl[$childpid=0]: if ($childpid==0): CHILD: sleep(10): child goes sleep for 10 seconds
/home/local/code/perlcode/execute/fork2.pl[$childpid=0]: if ($childpid==0): CHILD: sleep(10): child goes sleep for 10 seconds
/home/local/code/perlcode/execute/fork2.pl: while (4+1>0): PARENT: wait for child
/home/local/code/perlcode/execute/fork2.pl[$childpid=19688]: $endpid=waitpid($childpid=19688, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19689]: $endpid=waitpid($childpid=19689, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19690]: $endpid=waitpid($childpid=19690, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19691]: $endpid=waitpid($childpid=19691, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19692]: $endpid=waitpid($childpid=19692, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl: sleep(1):
/home/local/code/perlcode/execute/fork2.pl[$childpid=19688]: $endpid=waitpid($childpid=19688, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19689]: $endpid=waitpid($childpid=19689, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19690]: $endpid=waitpid($childpid=19690, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19691]: $endpid=waitpid($childpid=19691, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19692]: $endpid=waitpid($childpid=19692, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl: sleep(1):
/home/local/code/perlcode/execute/fork2.pl[$childpid=19688]: $endpid=waitpid($childpid=19688, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19689]: $endpid=waitpid($childpid=19689, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19690]: $endpid=waitpid($childpid=19690, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19691]: $endpid=waitpid($childpid=19691, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19692]: $endpid=waitpid($childpid=19692, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl: sleep(1):
/home/local/code/perlcode/execute/fork2.pl[$childpid=19688]: $endpid=waitpid($childpid=19688, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19689]: $endpid=waitpid($childpid=19689, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19690]: $endpid=waitpid($childpid=19690, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19691]: $endpid=waitpid($childpid=19691, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19692]: $endpid=waitpid($childpid=19692, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl: sleep(1):
/home/local/code/perlcode/execute/fork2.pl[$childpid=19688]: $endpid=waitpid($childpid=19688, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19689]: $endpid=waitpid($childpid=19689, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19690]: $endpid=waitpid($childpid=19690, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19691]: $endpid=waitpid($childpid=19691, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19692]: $endpid=waitpid($childpid=19692, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl: sleep(1):
/home/local/code/perlcode/execute/fork2.pl[$childpid=0]: if ($childpid==0): CHILD: sleep(10): child end sleep
/home/local/code/perlcode/execute/fork2.pl[$childpid=19688]: $endpid=waitpid($childpid=19688, 0): PARENT: child exited: $endpid=19688
/home/local/code/perlcode/execute/fork2.pl[$childpid=19689]: $endpid=waitpid($childpid=19689, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19690]: $endpid=waitpid($childpid=19690, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19691]: $endpid=waitpid($childpid=19691, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19692]: $endpid=waitpid($childpid=19692, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl: sleep(1):
/home/local/code/perlcode/execute/fork2.pl[$childpid=0]: if ($childpid==0): CHILD: sleep(10): child end sleep
/home/local/code/perlcode/execute/fork2.pl[$childpid=19689]: $endpid=waitpid($childpid=19689, 0): PARENT: child exited: $endpid=19689
/home/local/code/perlcode/execute/fork2.pl[$childpid=19690]: $endpid=waitpid($childpid=19690, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19691]: $endpid=waitpid($childpid=19691, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19692]: $endpid=waitpid($childpid=19692, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl: sleep(1):
/home/local/code/perlcode/execute/fork2.pl[$childpid=0]: if ($childpid==0): CHILD: sleep(10): child end sleep
/home/local/code/perlcode/execute/fork2.pl[$childpid=19690]: $endpid=waitpid($childpid=19690, 0): PARENT: child exited: $endpid=19690
/home/local/code/perlcode/execute/fork2.pl[$childpid=19691]: $endpid=waitpid($childpid=19691, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl[$childpid=19692]: $endpid=waitpid($childpid=19692, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl: sleep(1):
/home/local/code/perlcode/execute/fork2.pl[$childpid=0]: if ($childpid==0): CHILD: sleep(10): child end sleep
/home/local/code/perlcode/execute/fork2.pl[$childpid=19691]: $endpid=waitpid($childpid=19691, 0): PARENT: child exited: $endpid=19691
/home/local/code/perlcode/execute/fork2.pl[$childpid=19692]: $endpid=waitpid($childpid=19692, 0): PARENT: child exited: $endpid=0
/home/local/code/perlcode/execute/fork2.pl: sleep(1):
/home/local/code/perlcode/execute/fork2.pl[$childpid=0]: if ($childpid==0): CHILD: sleep(10): child end sleep
/home/local/code/perlcode/execute/fork2.pl[$childpid=19692]: $endpid=waitpid($childpid=19692, 0): PARENT: child exited: $endpid=19692
/home/local/code/perlcode/execute/fork2.pl: sleep(1):
/home/local/code/perlcode/execute/fork2.pl: while (-1+1>0): PARENT: all childs exited



#top system


Documentacja online: perldoc.perl.org | www.tutorialspoint.com

Deklaracja funkcji system() jest następująca:
system

Powiązane:
baticks(), system(),

Opis:
description

Example:
zawartość pliku system.pl
SELECT ALL
#!/usr/bin/perl -w

use strict;      # force definations for all vars an subroutines
use warnings;    # force check of unset variables in expressions
use diagnostics; # give var/code line number on faults



my ($command, $result);



if ($#ARGV+1<1) {
	print("Usage: $0 \"<command [, argv0 [, argv1 [, argv2[, ...]]]]>\"\n");
	print("Examples:\n");
	print("       $0 \"ls -l *.pl\"\n");
	print("       $0 \"cat $0 | grep line5\"\n");
	print("       $0 \"not_existed_exec\"\n");
	exit(1);
}

$command=$ARGV[0];
print""$0: \$result=system($command):\n");
$result=system($command);
print""$0: \$result=system($command): \$result=|$result|\n");

Aby zobaczyć jak działa program należy go uruchomić bez argumentów:
/home/local/code/perlcode/execute/system.pl
program wyświetli informacje o sposobie uruchamiania programu:
Usage: /home/local/code/perlcode/execute/system.pl "<command [, argv0 [, argv1 [, argv2[, ...]]]]>"
Examples:
       /home/local/code/perlcode/execute/system.pl "ls -l *.pl"
       /home/local/code/perlcode/execute/system.pl "cat /home/local/code/perlcode/execute/system.pl | grep line5"
       /home/local/code/perlcode/execute/system.pl "not_existed_exec"

jako argument wywołania programu można podać analogiczne jak poniżej argumenty:
/home/local/code/perlcode/execute/system.pl "ls -l *.pl"
/home/local/code/perlcode/execute/system.pl "cat /home/local/code/perlcode/execute/system.pl | grep line5"
/home/local/code/perlcode/execute/system.pl "not_existed_exec"
rezultat będzie zależny od podanego argumentu wywołania programu:
/home/local/code/perlcode/execute/system.pl: $result=system(ls -l *.pl):
ls: *.pl: Nie ma takiego pliku ani katalogu
/home/local/code/perlcode/execute/system.pl: $result=system(ls -l *.pl): $result=|512|

/home/local/code/perlcode/execute/system.pl: $result=system(cat /home/local/code/perlcode/execute/system.pl | grep line5):
        print("       $0 \"cat $0 | grep line5\"\n");
/home/local/code/perlcode/execute/system.pl: $result=system(cat /home/local/code/perlcode/execute/system.pl | grep line5): $result=|0|

/home/local/code/perlcode/execute/system.pl: $result=system(not_existed_exec):
Can't exec "not_existed_exec": Nie ma takiego pliku ani katalogu at
        /home/local/code/perlcode/execute/system.pl line 24 (#1)
    (W exec) A system(), exec(), or piped open call could not execute the
    named program for the indicated reason.  Typical reasons include: the
    permissions were wrong on the file, the file wasn't found in
    $ENV{PATH}, the executable in question was compiled for another
    architecture, or the #! line in a script points to an interpreter that
    can't be run for similar reasons.  (Or maybe your system doesn't support
    #! at all.)

/home/local/code/perlcode/execute/system.pl: $result=system(not_existed_exec): $result=|-1|





Zmodyfikowany ostatnio: 2014/09/02 10:03:51 (9 lat temu), textsize: 31,6 kB, htmlsize: 39,1 kB

Zapraszam do komentowania, zgłaszania sugestii, propozycji, własnych przykładów, ...
Dodaj komentarzKomentarze użytkowników