gpt4 book ai didi

linux - 从 perl 运行时将 bash 脚本的输出记录到日志文件和标准输出中

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:20:34 26 4
gpt4 key购买 nike

我有 perl 脚本

perl 脚本(install.pl):

use strict; use warnings;
use Term::ANSIColor;
my $logfilename = "install.log";
open LOG,">$logfilename" or die "failed to open log file reason:$!";
sub logMsg
{
my ($msg) = @_;
my ($error) = $_[1];
$msg .= "\n";
print LOG $msg;
if( $error ) { print color 'red' ;}
{print $msg}
if( $error ) { print color 'reset' ;}
}
sub lampInstall
{
logMsg("Installing apache2, php, and mysql db");
# Install apache2 and mysql:
my $cmdOutput = `./ubuntu-lamp-install.sh`;
print LOG "$cmdOutput\n";
}
lampInstall();
close LOG;

bash 脚本 (ubuntu-lamp-install.sh)

#!/bin/bash
sudo apt-get update
sudo apt-get install ntp
sudo /etc/init.d/ntp start
export DEBIAN_FRONTEND=noninteractive
echo "mysql-server mysql-server/root_password password test" | sudo debconf-set-selections
echo "mysql-server mysql-server/root_password_again password test" | sudo debconf-set-selections
sudo apt-get install -y apache2 php5 libapache2-mod-php5
sudo service apache2 restart
sudo a2enmod ssl

问题是,当 install.pl 被调用时,它会等待很长时间并且不会给出正在安装的任何输出信息。我需要它来更改 perl 脚本以显示来自 bash 脚本的输出信息(如果是即时的则更好)以及将输出记录到日志文件中。脚本是别人写的,我不懂perl脚本。

最佳答案

你可以做类似的事情

open my $cmdOutut, "-|", "./ubuntu-lamp-install.sh";
while (<$cmdOutput>) {
print LOG "$_\n";
print "$_\n";
}

在您的 lampInstall() 中。

这是新的 lampInstall():

sub lampInstall
{
logMsg("Installing apache2, php, and mysql db");
# Install apache2 and mysql:
open my $cmdOutut, "-|", "./ubuntu-lamp-install.sh";
while (<$cmdOutput>) {
print LOG "$_\n";
print "$_\n";
}
}

关于linux - 从 perl 运行时将 bash 脚本的输出记录到日志文件和标准输出中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22449632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com