本文共 1689 字,大约阅读时间需要 5 分钟。
首先,不是在BASH脚本中使用$USERNAME。 $USERNAME是一个包含当前用户名的BASH变量。实际上,在BASH中使用UPPERCASE变量通常是一个糟糕的主意。大多数BASH环境变量都是大写字母,可能会导致混淆。让你的变量小写是个好习惯。
此外,因为我想你想要使用HTML表单来做到这一点,所以你不能让BASH从STDIN中读取数据。修改游脚本以将用户名作为参数:
BASH:
#!/bin/bash
user=$1;
DISPLAYNAME=`ldapsearch -p xxx -LLL -x -w test -h abc.com -D abc -b dc=abc,dc=com sAMAccountName=$user | grep displayName`
if [ -z "$DISPLAYNAME" ]; then
echo "No entry found for $user"
else
echo "Entry found for $user"
fi
的Perl:
#!/usr/bin/perl
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
use warnings;
## Create a new CGI object
my $cgi = new CGI;
## Collect the value of 'user_name' submitted by the webpage
my $name=$cgi->param('user_name');
## Run a system command, your display_name.sh,
## and save the result in $result
my $result=`./display_name.sh $name`;
## Print the HTML header
print header;
## Print the result
print "$result
";HTML:
这应该做你所需要的。它假设这两个脚本都位于网页的./cgi-bin/目录中,并被称为display_name.sh和display_name.pl。它还假定你已经正确设置了他们的权限(他们需要由apache2的用户www-data执行)。最后,它假定您已经设置了apache2来允许执行./cgi-bin中的脚本。
是否有您想使用BASH的特定原因?您可以直接从Perl脚本执行所有操作:
#!/usr/bin/perl
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
use warnings;
## Create a new CGI object
my $cgi = new CGI;
## Collect the value of 'name' submitted by the webpage
my $name=$cgi->param('user_name');
## Run the ldapsearch system command
## and save the result in $result
my $result=`ldapsearch -p xxx -LLL -x -w test -h abc.com -D abc -b dc=abc,dc=com sAMAccountName=$name | grep displayName`;
## Print the HTML header
print header;
## Print the result
$result ?
print "Entry found for $name
" :print "No entry found for $name
";转载地址:https://blog.csdn.net/weixin_39987847/article/details/118137329 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!