和其他语言的eval功能差不多,都是将一个保存执行语句的变量作为参数,eval会让变量所保存的语句执行。

  下面是一个执行表单提交的命令:注意,这里只是示例,应用中不要这么使用,很危险

#!/bin/bash
#index.cgi echo "Content-Type:text/html;charset=utf-8"
echo cat << AAA
<html>
<head>
<title>run shell</title>
</head>
<body>
<form method="post">
请输入要执行的shell命令:<input type="text" name="code">
<input type="submit" value="run">
</form>
<pre>
AAA
if [ $REQUEST_METHOD = "POST" ];then
#使用cat或者tee接受post的方法
#query_string=`cat`
query_string=`tee`
code=`echo $query_string | sed 's/=/ /g' | awk '{print $2}' | sed 's/+/ /g'`
echo "您输入的命令是 "$code
eval $code
fi
cat << AAA
</pre>
</body>
</html>
AAA

  尝试提交命令 ls -la,运行结果如下:

shell脚本--eval执行shell命令的更多相关文章

  1. shell脚本中执行source命令不生效的解决办法

    一个shell脚本文件中有一个source命令,使用bash a.sh命令执行后source命令进行验证没有生效. 这是因为在shell脚本中执行source会看到效果,但是shell脚本执行完后再次 ...

  2. shell脚本中执行sql命令

    1.mysql 数据库表信息 2.shell脚本(a.sh)信息 #!/bin/sh mysql -u root << myInsert insert into test.t values ...

  3. 在shell脚本里执行sudo 命令

      可以 : echo "yourpasswd" |sudo -S yourcommand 但是不安全,因为密码都显示在shell脚本里面了-_- 引自http://hi.baid ...

  4. shell 脚本下执行Mongodb命令

    最近项目中搭建了两台mongodb的服务器,由于服务器只有两台的情况下,目前只是搭建了主从模式架构(官方目前并不推荐主从模式),缺点就是故障转移不变等等原因,而是推荐副本集模式(这里就不多说了)... ...

  5. shell脚本-批量执行机器命令

    场景:通过跳板机,批量获取线上机器日志 使用方式:run2 host 'ls -al /home/admin/' #! /bin/sh USER_NAME=$USER if [ $# -ne 2 ]; ...

  6. shell脚本中执行mysql命令

    1.mysql -hhostname -uuser -ppsword -e "mysql_cmd" 2. mysql -hhostname -uuser -ppsword < ...

  7. 在shell脚本中执行shell脚本

    任务下发 主节点脚本 任务管控脚本 [root@admin1 ~]# cat mytest.sh echo lovecode [root@admin1 ~]# cat myfor.sh #!/bin/ ...

  8. shell脚本中执行shell脚本(2)

    (a.sh)读取用户输入参数,并在脚本(b.sh)中使用 1.a.sh #!/bin/sh read -p "please input name value: " name ./b ...

  9. shell脚本中执行shell脚本

    1.a.sh #!/bin/sh name="hello" ./b.sh $name  2.b.sh(这里把b.sh与a.sh放在同一目录下,便于演示) #!/bin/sh ech ...

随机推荐

  1. February 13th, 2018 Week 7th Tuesday

    You are your greatest asset. 你就是你自己最大的资本. For most of us, there are few things that we can count on ...

  2. 《Linux服务器的监控》

    本文地址:http://www.cnblogs.com/aiweixiao/p/7131532.html 原文地址(公众号):http://t.cn/RKwmqUs 点击关注 微信公众号 1. 监控概 ...

  3. apache和nginx结合使用

    1  有时候我们希望将nginx和apache结合起来使用,nginx接受用户的请求,作为请求转发服务器,apache作为后端服务器. 2 配置如下 nginx 中将80端口的请求转发到8000端口上 ...

  4. python入门学习:2.列表简介

    python入门学习:2.列表简介 关键点:列表 2.1 列表是什么2.2 修改.添加和删除元素2.3 组织列表 2.1 列表是什么   列表,是由一系列按特定顺序排列的元素组成.你可以创建包含字母表 ...

  5. PHP json_encode 中文乱码

    每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code   在编码过程中.经常会用到json_encode来处理中文.但是.出现一个问题.中文 ...

  6. BAT美团滴滴java面试大纲(带答案版)之四:多线程Lock

    每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 这是多线程的第二篇. 多线程就像武学中对的吸星大法,理解透了用好了可以得道成仙,俯瞰芸 ...

  7. Spring Security(二十六):8. Spring Security Community

    8.1 Issue Tracking Spring Security uses JIRA to manage bug reports and enhancement requests. If you ...

  8. kali 源

    #阿里云 deb http://mirrors.aliyun.com/kali kali-rolling main non-free contrib deb-src http://mirrors.al ...

  9. JavaScript输入表单数据正则验证规则

    emailNameReg: /^(([a-zA-Z0-9]+\w*((\.\w+)|(-\w+))*[\.-]?[a-zA-Z0-9]+)|([a-zA-Z0-9]))$/, //匹配邮箱名称 ema ...

  10. Jenkins Pipeline高级用法-ShareLibrary

    1.Github配置 1.1 上传jenkinsfile到github https://github.com/zeyangli/ShareLibrary-jenkins.git 2.Jenkins配置 ...