[英]How to use shell variables in perl command call in a bash shell script?
[英]How to use shell command result in perl script?
任何人都可以告訴我,如何在Perl腳本中使用shell命令結果?
#!/usr/bin/perl
$whoami=`whoami`;
system ('cd /var/home/'.$whoami.'/htdocs');
print $whoami;
腳本輸出
[user1@srv _1]$ ./sys.pl
sh: line 1: /htdocs: No such file or directory
user1
我想將dir更改為/ var / home / user1 / htdocs
$whoami
包含結束字符\\n
,這會導致命令字符串如下所示:
cd /var/home/user1
/htdocs
您應該使用chomp
從$whoami
刪除尾隨換行符:
my $whoami = `whoami`;
chomp $whoami;
這可以在沒有炮轟的情況下完成:
#!perl
my $dir = '/home/'.getlogin().'/htdocs';
chdir $dir;
#!/usr/bin/perl
$whoiami=`whoami`;
print "$whoiami";
chomp $whoiami;
system ("cd /home/$whoiami/reports");
print $whoiami;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.