我有一个简短的bash脚本,可以在/ etc / os-release中找到发行版ID,并根据该结果打印一个字符串。如果可能的话,我想将其转换为perl。我对此表示感谢。
以下是我正在使用的脚本:
#!/bin/bash
grep "ID=fedora" /etc/os-release > /dev/null 2>&1
if [ $? = 0 ]; then
echo "You are running Fedora"
else
echo "You are running Ubuntu"
fi
谢谢,祝你有个美好的一天。:) 帕特里克。
答案 0 :(得分:1)
不确定为什么要将其转换为Perl。 Shell脚本是满足此类要求的最佳方式。
以下perl
脚本是一种转换方式
#!/usr/bin/perl
use warnings;
use strict;
my $fedora = 0;
open my $fread "<", "/etc/os-release" or die $!;
while (<$fread>) {
if (/ID=fedora/) {
$fedora = 1;
last;
}
}
if ($fedora) {
print "You are running Fedora\n";
} else {
print "You are running Ubuntu\n";
}
close $fread;
答案 1 :(得分:1)
#!/usr/bin/perl
use strict;
use warnings;
# Open /etc/os-release and read lines into array @list
open(F,'/etc/os-release');
my @list=<F>;
close F;
# Search for all lines containing "ID=fedora"
my @matchinglines= grep /ID=fedora/,@list;
# If the number of matching lines is >0 it's Fedora
if(scalar @matchinglines>0){
print "You are running Fedora\n";
} else {
print "You are running Ubuntu\n";
}
答案 2 :(得分:1)
perl -lne'
last if $f = /ID=fedora/;
END{ print "You are running ", $f ? "Fedora":"Ubuntu" }
' /etc/os-release
答案 3 :(得分:0)
一种方法,虽然不是最有效的,因为它读取内存中的整个文件:
perl -MList::Util=first -e '
printf qq|You are running %s\n|,
( first { m/ID=fedora/ } <> ) ?
q|Fedora|
:
q|Ubuntu|
' /etc/os-release
答案 4 :(得分:0)
perl -ne 'print "You are running $1\n" if /^ID=(.+)/' /etc/os-release
答案 5 :(得分:0)
作为一位经验丰富的古老神风敢死队:
#!/bin/bash
ID="something unknown"
test -r /etc/os-release && . /etc/os-release
echo You are running "$ID"