当我选择一个项目时它没有做任何事情?

时间:2014-08-01 23:03:46

标签: perl

#!/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 ("/usr/bin/groupadd -g $group_id $group_name");
print "Group Created Successfully!\n"
   } else {
print "Group Already Exists !\n";
   }

chomp $choice;
if ($choice == 2) {
print "**********************************\n";
print " Delete a Unix Group\n";
print "**********************************\n";
print " Enter a Group Name to Delete >\n";
chomp ($group_name = <STDIN>);

if ( ! `grep -i $group_name /etc/group` ) {
   system ("/usr/bin/groupdel -g $group_id $group_name");
print "Delete Group Successfully!\n"
   } else {
print "Group Doesn't Exist !\n";
   }

}   
}

当我选择选择号码&#34; 2&#34;这被称为&#34;删除一个Unix组&#34;它没有做任何事情。它只是让我回到命令行?我错过了什么?

2 个答案:

答案 0 :(得分:2)

当选择2时应该执行的块是内部当选择1时的块。尝试删除末尾的右侧括号之一,并在块之前添加一个用于选择2的位置。

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 ("/usr/bin/groupadd -g $group_id $group_name");
    print "Group Created Successfully!\n"
  } else {
    print "Group Already Exists !\n";
  }
}

#chomp $choice;    <-- This chomp is superfluous; you already removed the newline
elsif ($choice == 2) {
  print "**********************************\n";
  print " Delete a Unix Group\n";
  print "**********************************\n";
  print " Enter a Group Name to Delete >\n";
  chomp ($group_name = <STDIN>);

  if ( ! `grep -i $group_name /etc/group` ) {
    system ("/usr/bin/groupdel -g $group_id $group_name");
    print "Delete Group Successfully!\n"
  } else {
    print "Group Doesn't Exist !\n";
  }
}

使用缩进可以帮助将来捕获这类内容。

答案 1 :(得分:0)

你的主要问题是&#34; if($ choice == 1)&#34;块包括&#34; if($ choice == 2)&#34;块,表示&#34; if($ choice == 2)&#34;声明仅在$ choice为1时执行;当然,如果$ choice == 1,&#34; if($ choice == 2)&#34;块将不会被执行。如果选择为2,则在&#34; if($ choice == 1)&#34;之后的下一条指令;程序终止。

底线:重构if ...块以反映您的意图;在你这样做之后,你可能想要改变&#34; if($ choice == 2)&#34;使用elsif - 具有相同的功能,IMO更清晰。

另外,更改顶部的初始化以摆脱&#39; = STDIN&#39 ;;你不想在那时从stdin读取。