shell result from cmdline

echo $?

if 0 then success ;else failure

(shell 执行每部返回值,rm -rf 错误,打包不能覆盖)

我们知道,linux命令行上一个命令的退出码放在了$?环境变变中,但是,如果这个命令是一串管道符连接和多个命令,怎么知道每个命令的退出码?你可以 使用 PIPESTATUS环境变量。比如这个测试:true | false | true; echo "${PIPESTATUS[@]}"

解决sudo: sorry, you must
have a tty to run sudo
 
前几天遇到一个问题,在一个终端中调用另一个shell,始终是无法执行的,后来捕捉到报错信息为sudo:
sorry, you must have a tty to run sudo,后来,在网上了解到可以如下解决:
1. 编辑 /etc/sudoers
 
  1)Defaults  
 requiretty,修改为 #Defaults    requiretty,表示不需要控制终端。
 
  2)Defaults  
 requiretty,修改为 Defaults:nobody !requiretty,表示仅 nobody 用户不需要控制终端。
 
       如果修改为
Defaults:%nobody !requiretty,表示仅 nobody 组不需要控制终端。
 
其实只要注释掉)Defaults  
 requiretty
那个就可以了。表示在执行的时候不打开终端。但是,有的shell必须要有终端才可以执行。这样显然是不行的。后来,又找到一片文章才搞定。下面为抄录的,仅为记录以后使用。
 
有些程序/脚本可能在没有控制终端的环境下上执行(如系统启动服务时, Daemon,
或者是setsid启动的程序等)
但可能这个程序需要控制终端, 这这么办呢?
如我们的例子: 我们在linux启动时启动一个服务,
可是中间有个程序(旧的systemtap)使用了sudo
但sudo需要控制终端(当然可以通过修改sudo的配置文件,
但这样很对用户不友好啊)
例子: (setsid 启动的程序会失去控制终端)
# setsid sudo ls
sudo: sorry, you must have
a tty to run sudo
(如果你的系统没有输出这句话,
那就是你的系统的sudo配置文件允许sudo可以在这个,
请确保已经设置了Defaults  
 requiretty)
没有控制中断的时候, 打开控制终端是这样的效果:
# setsid head -c 0 /dev/tty
head: cannot open
`/dev/tty' for reading: No such device or address
为了解决这个问题,
所以应该使用能创建(伪)控制终端的程序来启动你的程序如: script, expect
如:
# setsid script -c "sudo
ls" /dev/null
或:
# setsid expect -c 'spawn
sudo ls; expect'
#打开控制终端成功:
# setsid script -c 'head -c
0 /dev/tty' /dev/null 
# setsid expect -c 'spawn
head -c 0 /dev/tty; expect'
不过 setsid 跟 script
组合使用有着奇怪的bug, 我这段时间非常的depression,
这样对script, expect来说,
都是大材小用了

shell (check return of each line)(PIPESTATUS[@])and sudoer的更多相关文章

  1. shell (check return of each line)and sudoer

    shell result from cmdline echo $? if 0 then success ;else failure (shell 执行每部返回值,rm -rf 错误,打包不能覆盖) 解 ...

  2. Shell脚本:while read line无法读取最后一行的问题

    [1]Shell脚本:while read line无法读取最后一行的问题 刚刚利用shell脚本处理日志文件时,发现了一个问题:while read line无法读取到最后一行 通过编辑器可以看到待 ...

  3. SAP(ABAP):STOP,EXIT,CHECK,RETURN,REJECT,CONTINUE

    Stop 命令使用该命令的程序位置INITIALIZATION, AT SELECTION-SCREEN, START-OF-SELECTION和GET 事件中处理说明1. 当在INITIALIZAT ...

  4. learning shell check requires root privileges

    [Purpose]        Shell script check requires root privileges   [Eevironment]        Ubuntu 16.04 bas ...

  5. learning shell check host dependent pkg

    [Purpose]        Shell script check host dependent pkg   [Eevironment]        Ubuntu 16.04 bash env ...

  6. Shell check IP

    #! /bin/bash checkip() {        if echo $1 |egrep -q '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3 ...

  7. 如何在Windows下开发Python:在cmd下运行Python脚本+如何使用Python Shell(command line模式和GUI模式)+如何使用Python IDE

    http://www.crifan.com/how_to_do_python_development_under_windows_environment/ 本文目的 希望对于,如何在Windows下, ...

  8. Google Shell Style Guide

    转自:http://google.github.io/styleguide/shell.xml Shell Style Guide Revision 1.26 Paul Armstrong Too m ...

  9. KoaHub.JS用于Node.js的可移植Unix shell命令程序代码

    shelljs Portable Unix shell commands for Node.js ShellJS - Unix shell commands for Node.js     Shell ...

随机推荐

  1. Selenium - Switch & Select Api

    一.多表单切换  driver.switch_to.frame() iframe  :直接将一个html 页面嵌入另一个html 页面中 switch_to.frame() 默认可以直接取表单的id ...

  2. PHP截取中文字符串不出现?号的解决方法[原创]

    PHP截取中文字符串不出现?号的解决方法[原创] 大 | 中 | 小 [不指定 -- : | by 张宴 ] [文章作者:张宴 本文版本:v1. 最后修改: 转载请注明出处:http://blog.z ...

  3. iOS开发之使用AFN上传图片

    //1.创建管理者对象 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.responseSerializ ...

  4. linux命令之fuser

    Usage: fuser [ -a | -s | -c ] [ -n SPACE ] [ -SIGNAL ] [ -kimuv ] NAME... [ - ] [ -n SPACE ] [ -SIGN ...

  5. python操作word之pywin32的安装

    PyCharm 2016.2 官网中文汉化破解版 注册码 http://idea.lanyus.com/ 首先下载安装win32com,下载32位的,不然安装的时候可能检测不到python https ...

  6. xeno 实时性能测试 在100us的采样周期的测试数据

    1 xeno 用户层测试时间: root@sama5d3-linux:/usr/bin latency -t0 -T25 -p100== Sampling period: 100 us== Test ...

  7. div允许用户输入

    主要是用到contenteditable属性,就可以用div让用户输入了 <div id="guo" style="width:500px; height:200p ...

  8. Entity Framework(四):使用DbModelBuilder API创建表结构

    DbContext类有一个OnModelCreating方法,它用于流利地配置领域类到数据库模式的映射.下面我们以fluent API的方式来定义映射.首先,先将Product类注释掉,重新编写该类, ...

  9. php 判断查询结果是否为空

    select count(people) c from people where people='乐乐' 上面这条sql就是原理 php利用代码 <?php $p=$_POST['p']; $c ...

  10. Python安装相关的机器学习库以及图像处理库

    安装 sudo apt-get install python-scipy sudo apt-get install python-numpy sudo apt-get install python-m ...