Shell脚本CGI调用两个不同的脚本(使用Form Post方法)

时间:2014-05-28 10:00:40

标签: bash shell cgi

我正在创建一个Shell Bash CGI脚本,实际上反过来使用HTML表单调用两个脚本(一个Perl和其他Python)。

我能够成功地单独运行脚本,即一次运行一个脚本,但是当我没有从父Sheel Bash脚本调用两个子脚本的逻辑时。

如果选择了Form 1,我想运行第一个子脚本,如果选择了Form 2,则运行第二个子脚本?

还有一个问题,使用Get meathod显示密码,隐藏密码字符串的最佳选择是什么?

以下是代码:

(HTML Form starts here)
#!/usr/local/bin/bash
echo "Content-type: text/html"
echo ""
echo "<html><head><title>Fview"
echo "</title></head><body>"
echo "<h1>Please enter device name in the Box</h1>"
echo '<form method=GET action="support">'\
        '<table nowrap> <tr><td>Device</TD><TD><input type="text" name="val_x" size=12></td></tr>'\
        '<tr><td>Username</TD><TD><input type="text" name="val_y" size=12></td></tr>'\
        '<tr><td>Password: </TD><TD><input type="password" name="val_z" size=12></td></tr></tr></table>' 
echo '<br><input type="submit" value="Get Qkview"> <input type="reset" value="Reset"></form> '
echo '<form method=GET action="support">'\
        '<table nowrap> <tr><td>Device</TD><TD><input type="text" name="val_a" size=12></td></tr>'\
        '<tr><td>Username</TD><TD><input type="text" name="val_b" size=12></td></tr>'\
        '<tr><td>Password: </TD><TD><input type="password" name="val_c" size=12></td></tr></tr></table>' 
echo '<br><input type="submit" value="Get VIP"> <input type="reset" value="Reset"></form> '


#HTML ends here , you can see two forms 

PATH="/bin:/usr/bin:/usr/ucb:/usr/opt/bin:/usr/local/bin:"
export $PATH
if [ "$REQUEST_METHOD" != "GET" ]; then
        echo "<hr>Script Error:"\
             "<br>Usage error, cannot complete request, REQUEST_METHOD!=GET."\
             "<br>Check your FORM declaration and be sure to use METHOD=\"GET\".<hr>"
        exit 1
  fi
  # If no search arguments, exit gracefully now.
  if [ -z "$QUERY_STRING" ]; then
        exit 0
  else
     # No looping this time, just extract the data you are looking for with sed:
    XX=`echo "$QUERY_STRING" | sed -n 's/^.*val_x=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
    YY=`echo "$QUERY_STRING" | sed -n 's/^.*val_y=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
    ZZ=`echo "$QUERY_STRING" | sed -n 's/^.*val_z=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
    AA=`echo "$QUERY_STRING" | sed -n 's/^.*val_a=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
    BB=`echo "$QUERY_STRING" | sed -n 's/^.*val_b=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
    CC=`echo "$QUERY_STRING" | sed -n 's/^.*val_c=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`


#Here are the two child scripts . But am able to call the health_ui.pl but not view.py
         commandline="/home/u1/scripts/health_ui.pl $AA $BB $CC"
         commandline="/home/u2/scripts/view.py $XX $YY $ZZ"
         $commandline
         echo "done:"


  fi

echo '</body>'
echo '</html>'

exit 0

1 个答案:

答案 0 :(得分:0)

根据上述评论中的小讨论,这可能是个问题:

commandline="perl /home/u1/scripts/health_ui.pl $AA $BB $CC"
$commandline
echo "first script done."
commandline="python -c /home/u2/scripts/view.py $XX $YY $ZZ"
$commandline
echo "second script done."

不是两个 $命令行值必须&#34;执行&#34;如果没有将正确的shebang指定为这些脚本的第一行,那么(corect?)解释器必须在脚本路径之前。