Leser: 1
![]() |
|< 1 2 3 4 >| | ![]() |
35 Einträge, 4 Seiten |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!usr/bin/perl
print "Waehlen sie aus was sie möchten? \n (1) TeamSpeak2 Server Starten oder Rebooten \n (2) TeamSpeak2 Server Stoppen \n (3) Server Reboot \n";
print "Eingabe: ";
print $b1=<>;
chop ($b1);
$Givenvergleich = $b1;
given $Givenvergleich
{
when '1' {print `cd ../home/TS/` + `./teamspeak2-server_startscript stop` + `./teamspeak2-server_startscript start`;}
when '2' {print `cd ../home/TS/` + `./teamspeak2-server_startscript stop`;}
when '3' {print `reboot`;}
}
print `cd ../home/TS/` + `./teamspeak2-server_startscript stop` + `./teamspeak2-server_startscript start`;
1
2
3
4
if ($eingabe eq '1') {chdir '/path/script'; print qx'./teamspeak2-server_startscript start';}
elsif ($eingabe eq '2') {chdir '/path/script'; print qx'./teamspeak2-server_startscript stop';}
elsif ($eingabe eq '3') {print qx'reboot'; exit;}
else die "Falche eingabe";
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
qx/STRING/
`STRING`
A string which is (possibly) interpolated and then executed as a system command with /bin/sh or its equivalent. Shell wildcards, pipes, and redirections will be honored. The collected standard output of the command is returned; standard error is unaffected. In scalar context, it comes back as a single (potentially multi-line) string, or undef if the command failed. In list context, returns a list of lines (however you've defined lines with $/ or $INPUT_RECORD_SEPARATOR), or an empty list if the command failed.
Because backticks do not affect standard error, use shell file descriptor syntax (assuming the shell supports this) if you care to address this. To capture a command's STDERR and STDOUT together:
$output = `cmd 2>&1`;
To capture a command's STDOUT but discard its STDERR:
$output = `cmd 2>/dev/null`;
To capture a command's STDERR but discard its STDOUT (ordering is important here):
$output = `cmd 2>&1 1>/dev/null`;
To exchange a command's STDOUT and STDERR in order to capture the STDERR but leave its STDOUT to come out the old STDERR:
$output = `cmd 3>&1 1>&2 2>&3 3>&-`;
To read both a command's STDOUT and its STDERR separately, it's easiest and safest to redirect them separately to files, and then read from those files when the program is done:
system("program args 1>/tmp/program.stdout 2>/tmp/program.stderr");
Using single-quote as a delimiter protects the command from Perl's double-quote interpolation, passing it on to the shell instead:
$perl_info = qx(ps $$); # that's Perl's $$
$shell_info = qx'ps $$'; # that's the new shell's $$
How that string gets evaluated is entirely subject to the command interpreter on your system. On most platforms, you will have to protect shell metacharacters if you want them treated literally. This is in practice difficult to do, as it's unclear how to escape which characters. See perlsec for a clean and safe example of a manual fork() and exec() to emulate backticks safely.
On some platforms (notably DOS-like ones), the shell may not be capable of dealing with multiline commands, so putting newlines in the string may not get you what you want. You may be able to evaluate multiple commands in a single line by separating them with the command separator character, if your shell supports that (e.g.; on many Unix shells; & on the Windows NT cmd shell).
Beginning with v5.6.0, Perl will attempt to flush all files opened for output before starting the child process, but this may not be supported on some platforms (see perlport). To be safe, you may need to set $| ($AUTOFLUSH in English) or call the autoflush() method of IO::Handle on any open handles.
Beware that some command shells may place restrictions on the length of the command line. You must ensure your strings don't exceed this limit after any necessary interpolations. See the platform-specific release notes for more details about your particular environment.
Using this operator can lead to programs that are difficult to port, because the shell commands called vary between systems, and may in fact not be present at all. As one example, the type command under the POSIX shell is very different from the type command under DOS. That doesn't mean you should go out of your way to avoid backticks when they're the right way to get something done. Perl was made to be a glue language, and one of the things it glues together is commands. Just understand what you're getting yourself into.
See "I/O Operators" for more discussion.
1
2
3
4
5
6
my $eingabe;
while ($eingabe ne 'exit') {
$eingabe = <STDIN>;
chmop($eingabe);
# if ($eingabe eq '1') { ...
}
![]() |
|< 1 2 3 4 >| | ![]() |
35 Einträge, 4 Seiten |