首先,不是在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<BR>";
HTML:
<html>
<body>
<form ACTION="./cgi-bin/display_name.pl" METHOD="post">
<INPUT TYPE="submit" VALUE="Submit"></a>
<INPUT TYPE="text" NAME="user_name"></a>
</form>
</body>
</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<BR>" :
print "No entry found for $name<BR>";
其中代码正在运行(在运行Web服务器的计算机上)? – akira
是的,代码在网络服务器上运行。 – 2012-09-26 11:04:17