在Perl中创建用户

时间:2015-03-17 01:12:53

标签: perl

我正在尝试使用这些选项创建脚本,创建用户,删除用户, 创建一个组,删除一个组。如何阻止它立即运行所有命令?

#!/usr/bin/perl

$opt = 0;
&disp_menu;

$opt = <>;
print "You selected Option ";
print $opt;

$user = `sudo adduser mjensen`;
$group = `sudo groupadd Group1`;
$dgroup = `sudo groupdel Group1  `;
$duser = `sudo userdel mjensen`;

sub disp_menu {
print "Main Menu\n";
print "Option 1 Create Group\n";
print "Option 2 Drop Group\n";
print "Option 3 Create User\n";
print "Option 4 Drop User\n";
print "Option 5 Exit Menu\n";
print "Enter Option => ";
return;}

if ($opt == 3) {print "$user";
}

elsif ($opt == 1) {print "$group\n";
}
elsif ($opt == 2) {print "$dgroup\n";
}
elsif ($opt == 4) {print "$duser\n";
}
elsif ($opt == 5) {print "You have exited the program";
      }

1 个答案:

答案 0 :(得分:0)

#!/usr/bin/perl

$opt = 0;
&disp_menu;

$opt = <>;
print "You selected Option ";
print $opt;

if ($opt == 3) {
    $user = `sudo adduser mjensen`;
    if ( $? != -1 ) {
        print "user mjensen has been created\n";
    }        
}
elsif ($opt == 1) {
    $group = `sudo groupadd Group1`;
    if ( $? != -1 ) {
        print "group Group1 has been created\n";
    }
}
elsif ($opt == 2) {
    $dgroup = `sudo groupdel Group1`;
    if ( $? != -1 ) {
        print "group Group1 has been dropped\n";
    }
}
elsif ($opt == 4) {
    $duser = `sudo userdel mjensen`;
    if ( $? != -1 ) {
        print "user mjensen has been dropped\n";
    }
}
elsif ($opt == 5) {
    print "You have exited the program";
}

sub disp_menu {
    print "Main Menu\n";
    print "Option 1 Create Group\n";
    print "Option 2 Drop Group\n";
    print "Option 3 Create User\n";
    print "Option 4 Drop User\n";
    print "Option 5 Exit Menu\n";
    print "Enter Option => ";
    return;
}