【问题标题】:Cut and tr: command not found剪切和 tr:找不到命令
【发布时间】:2018-08-20 01:44:54
【问题描述】:

这段代码有什么问题:

#!/bin/sh
PATH=/sbin:/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions

 HOSTNAME="TAI-$(ifconfig | grep "HWaddr" | cut -d" " -f11 | tr -s ":" "-")"

然后:

root@teste:/ ./hostname.sh <enter>

脚本返回:

./hostname.sh: 7: ./hostname.sh: cut: not found
./hostname.sh: 7: ./hostname.sh: tr: not found

但在控制台(命令行)中运行完美。

root@teste:/ HOSTNAME="TAI-$(ifconfig | grep "HWaddr" | cut -d" " -f11 | tr -s ":" "-")" <enter>

root@teste:/ echo $HOSTNAME <enter>
root@teste:/ TAI-b8-27-eb-81-4f-c5  - It's OK.

有人可以帮帮我吗?

非常感谢。

【问题讨论】:

    标签: linux bash shell


    【解决方案1】:

    看线:

    PATH=/sbin:/bin
    

    cuttr 位于 /usr/bin 中,而您的 PATH 中没有。将:/usr/bin 添加到PATH 变量的末尾。

    PATH=/sbin:/bin:/usr/bin
    

    【讨论】:

    • 确保 PATH=/sbin 中没有空格
    • @AkshayVijayJain 或者,如果目录名称中包含空格,请确保始终引用来自变量的所有内容,例如“$@”、“$PATH”等。无论如何,引用所有内容,甚至使用包含空格的文件名/目录进行测试是一个好习惯。我没有引用我的答案,因为不包含空格并且不依赖于可能包含空格的变量/输出结果。
    猜你喜欢
    • 2020-09-14
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2017-07-04
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    相关资源
    最近更新 更多