html中显示shell脚本的输出,网页从shell脚本中输入并显示结果

首先,不是在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
";

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值