【问题标题】:Executing who -m from ant从 ant 执行 who -m
【发布时间】:2023-03-06 16:48:01
【问题描述】:

我正在尝试从 Apache ant 执行 "who -m" 命令,但没有成功。

这是我的 ant 脚本:

<?xml version="1.0" encoding="UTF-8"?>
<project name="default" default="who.am.i">
    <target name="who.am.i">
        <exec executable="who" outputproperty="myOutput">
            <arg value="-m"/>
        </exec>
        <echo message="I am = ${myOutput}"/>
    </target>
</project>

结果为空。

[echo] I am = 

如果我在没有参数的情况下运行 exec,它会显示正确的结果:

        <exec executable="who" outputproperty="myOutput">
        </exec>

[echo] host.name = gary     tty8         2014-02-03 12:04 (:0)
[echo] gary     pts/0        2014-02-03 12:09 (:0)
[echo] gary     pts/1        2014-02-03 12:23 (:0)
[echo] gary     pts/2        2014-02-04 11:36 (:0)
[echo] gary     pts/4        2014-02-05 13:27 (:0)
[echo] gary     pts/7        2014-02-04 12:23 (:0)
[echo] gary     pts/8        2014-02-06 12:44 (:0)

如果我从终端运行 who -m 命令,它会显示我要查找的内容:

who -m
gary     pts/8        2014-02-06 12:44 (:0)

任何想法为什么 ant 不接受 -m 参数?

【问题讨论】:

    标签: ant exec


    【解决方案1】:

    尝试作为 shell 可执行文件执行以查看是否有帮助。它有助于使用您要运行的确切 unix 命令调用 shell。

    <exec executable="sh" outputproperty="myOutput">
       <arg value="who -m"/>
    </exec>
    

    【讨论】:

    • 试了没成功,决定走bashrc路线
    【解决方案2】:

    您没有提及您使用的是哪种 Unix,但在 Solaris 上,当我尝试您的任务时收到此错误消息:

    [echo] $ Must be attached to terminal for 'am I' option
    

    相比之下,在 OSX 上它似乎可以工作,但是说:

    [echo] I am = mjc      tty??    Feb  7 02:35 
    

    注意?? - 它也没有找到会话的终端。

    我怀疑在您的情况下,由于与 Solaris 测试相同的原因,它静默失败 - 即由 Ant(即由 java)分叉的 shell 与您的终端会话无关。

    (可能有一种解决方法,但我不知道,如果有的话,它不太可能是可移植的。)

    【讨论】:

    • 我在我的 Linux 机器上测试它。当我在 Solaris 上运行它时,我还得到:必须附加到终端以获取“我是”选项。
    【解决方案3】:

    最后我决定走另一条路。

    ~/.bashrc 我添加了以下行:

    who -m | awk '{print $5}' > ~/.whoami.out
    

    为了使其全球化,我只是将它添加到 /etc/bashrc

    这将在我每次登录远程系统时写入 ~/.whoami.out 文件。

    在我的 ant 脚本中,我阅读了这个文件的内容:

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="default" default="default">
    
        <target name="test.who.key">
            <loadfile property="who.key" srcFile="${user.home}/.whoami.out" failonerror="false"/>
            <condition property="who.cond">
                <isset property="who.key"/>
            </condition>
            <condition property="who.cond2">
                <not>
                    <isset property="who.key"/>
                </not>
            </condition>
        </target>
    
        <target name="init.who.key" depends="test.who.key" if="who.cond">
            <echo message="WHO EXIST"/>
            <property name="whoAmI" value="${who.key}"/>
        </target>
    
        <target name="init.not.who.key" depends="test.who.key" if="who.cond2">
            <echo message="WHO DOES NOT EXIST"/>
            <property name="whoAmI" value=""/>
        </target>    
    
        <target name="default" depends="init.who.key, init.not.who.key">
            <echo message="whoAmI = ${whoAmI}"/>
        </target>
    
    </project>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      • 2021-12-25
      • 1970-01-01
      • 2019-09-06
      相关资源
      最近更新 更多