#!/usr/bin/perl
use strict;
use warnings;
my $group_name = <STDIN>;
my $group_id = <STDIN>;
my $choice = <STDIN>;
print "* My menu *\n";
print "* *\n";
print "* 1. Create a Unix Group *\n";
print "* 2. Delete a Unix Group *\n";
print "* 3. Create a Unix User *\n";
print "* 4. Delete a Unix USer *\n";
print "* 5. Quit *\n";
print "* *\n";
print "******************************************************\n";
print "* Enter Your Choice >\n";
$choice = <STDIN> ;
chomp $choice;
if ($choice == 1) {
print "**********************************\n";
print " Create A Unix Group\n";
print "**********************************\n";
print " Enter The Group Name to Create >\n";
chomp ($group_name = <STDIN>);
print " Enter the Group ID to Create >\n";
chomp ($group_id = <STDIN> );
if ( ! 'grep -i $group_name /etc/group' ) {
system ("groupadd -g $group_id $group_name");
print "Group Created Successfully!\n"
} else {
print "Group Already Exists !\n";
}
}
当我运行脚本并输入组ID时,它返回组已经存在。我输入的内容并不重要,说实话它不断回归集团已经存在,我知道这个集团并不存在。任何Suggestiongs?
答案 0 :(得分:4)
问题在于:
if ( ! 'grep -i $group_name /etc/group' )
在这里您使用引用'...'
,因此perl
会将其视为文字字符串。布尔上下文中非空的字符串将被标记为true,因此if
条件总是失败,执行else
分支。
要从perl
运行系统命令,您必须使用backstick `...`或使用system()
内置函数:
if ( ! `grep -i $group_name /etc/group` )