Ant Build-Script如何检查root权限

时间:2011-06-14 18:20:25

标签: ant build-script

如何使用Ant构建脚本检查root权限?我尝试使用像

这样的shellscript任务
<shellscript shell="bash">
    if [[ `whoami` != 'root' ]]; then
        echo "You need to be root to install ooplss";
        exit 1
    fi
</shellscript>

但这不会停止脚本的执行。

还有其他想法吗?

3 个答案:

答案 0 :(得分:4)

shellscript任务是exec任务的扩展。如果脚本失败,您应该能够指定failonerror以使构建过程失败:

  

failonerror:停止构建过程if   该命令以返回码退出   信令失败。默认为false。

<shellscript shell="bash" failonerror="true">
    if [[ `whoami` != 'root' ]]; then
        echo "You need to be root to install ooplss";
        exit 1
    fi
</shellscript>

然而,应该可以不使用shell脚本;以下是未经测试的:

<fail message="You need to be root to install ooplss">
 <condition>
   <not>
     <equals arg1="root" arg2="${user.name}"/>
   </not>
 </condition>
</fail>

答案 1 :(得分:0)

您可以查看java属性user.name

<target name="checkroot">
  <condition property="isroot">
    <equals arg1="${os.user}" arg2="root"/>
  </condition>
</target>
<target name="dostuff" depends="checkroot" unless="isroot">
...
</target>

从ant 1.7开始,你也可以使用<scriptcondition>在脚本中做一些聪明的事情而不是上面的<equals>

答案 2 :(得分:0)

使用Ant Plugin Flaka =

的直接方法
<project xmlns:fl="antlib:it.haefelinger.flaka">
  <fl:when test=" '${user.name}'.toupper eq 'ROOT' ">
    <!-- your tasks go here.. -->
  </fl:when>
</project>