【问题标题】:Error With Bash Script [duplicate]Bash脚本错误[重复]
【发布时间】:2024-12-29 12:50:02
【问题描述】:

我想知道以下脚本中的错误是什么

我得到错误: command not foundh: line 1:: command not foundh: line 2:它的连续

我已尝试添加;,但现在正在工作,请告诉我该怎么做??

#!/bin/bash;
clear;
FILEREPO=http://192.168.1.2/cpplugin;

echo "-----------------------------------------------";
echo " Welcome to C-Panel Login Alert Installer";
echo "-----------------------------------------------";
    cd /var/cpanel/;
    mkdir perl5
    cd perl5/
    mkdir lib
    cd lib/
    wget $FILEREPO/LoginAlerthook.zip
    unzip LoginAlerthook.zip
    rm -r LoginAlerthook.zip
    cd /
    /usr/local/cpanel/bin/manage_hooks add module LoginAlert
    chmod 777 LoginAlert.pm
    echo " "
    echo " Login Alert Script Hooked With C Panel Finished"
    echo " "

【问题讨论】:

  • i) 删除; ii) 检查 FTP 是否在 192.168.1.2 上运行
  • 你写了“但是现在工作......”,你的意思是但不工作......?请编辑您的问题以包含多个错误消息的精确复制/粘贴。我不敢相信操作系统正在打印出 command not foundh 。祝你好运。
  • 你是如何执行脚本的?
  • 喜欢这个 sh.filename.sh

标签: linux bash


【解决方案1】:

paxdiablo 和 William Pursell 很好地解释了问题所在。

现在,如果您要分发它,请花时间改进您的脚本。

未测试示例:

#/bin/bash

ZIPFILE=http://192.168.1.2/cpplugin/LoginAlerthook.zip
CPANEL_LIBDIR=/var/cpanel/perl5/lib
MANAGE_HOOKS_CMD=/usr/local/cpanel/bin/manage_hooks

TMPFILE=`tempfile`

function exit_with_error(){
   echo "$*" >&2   # Write error messages to stderr!!
   exit 1
}

function at_exit(){
  [ -f "${TMPFILE}" ] && rm -v ${TMPFILE}
}

# Run at_exit function when script finishes
trap at_exit 0

echo "WELCOME TO ZOMBO.COM"

# Create lib directory if not exists, exit if not possible
if ! [ -d "${CPANEL_LIBDIR}" ]; then
     mkdir -p ${CPANEL_LIBDIR} || exit_with_error "Couldn't create required directory [${CPANEL_LIBDIR}]"
fi

wget ${ZIPFILE} -O ${TMPFILE} || exit_with_error "Couldn't download file"
unzip -d ${CPANEL_LIBDIR} ${TMPFILE} || exit_with_error "Couldn't unzip file"
chmod +x ${CPANEL_LIBDIR}/LoginAlert.pm || exit_with_error "Couldn't chmod file" 
$MANAGE_HOOKS_CMD add module LoginAlert

echo "End."

这只是一个肮脏的例子。阅读手册页进行改进。

man bash
man tempfile
man wget
man unzip
man chmod

【讨论】:

    【解决方案2】:

    paxdiablo 几乎可以肯定是正确的:您需要修复行尾。但是您在第一行中也有一个错误的分号。而不是:

    #!/bin/bash;
    

    你想要的:

    #!/bin/bash
    

    没有尾随分号。

    【讨论】:

      【解决方案3】:

      您得到有趣的输出这一事实肯定是您的脚本在行尾有回车 (CR) 字符,这通常是使用假定行尾应该是 CR/LF 的 Windows 编辑器的症状而不仅仅是标准的 UNIX LF(换行)。这导致错误输出如下:

      this_command_ends_hh<CR>: command not found
      

      并且因为 CR 将光标放回行首,它会覆盖其中的一部分:

      this_command_ends_hh<CR>
      : command not found
      

      制作:

      : command not foundh
      

      使用od -xcb scriptname 检查您的脚本以检查CR(显示为\r)字符,您还可以通过od -xcb 管道输出脚本以查看真实 输出。例如,我使用hello 创建了一个文件,然后在唯一的一行上加上回车:

      0000000    6568    6c6c    0d6f    000a
                h   e   l   l   o  \r  \n
              150 145 154 154 157 015 012
      0000007
      

      您可以在其中看到 CR (\r)。

      如果问题,只需删除 CR 字符,例如通过tr -d '\r' 管道。

      执行cat hello.txt | tr -d '\r' | od -xcb说明可以摆脱它:

      0000000    6568    6c6c    0a6f
                h   e   l   l   o  \n
              150 145 154 154 157 012
      0000006
      

      在您的情况下,假设您的脚本名为 freak.bash,您将使用:

      tr -d '\r' <freak.bash >newfreak.bash
      

      newfreak.bash 将是没有冒犯性字符的那个。

      【讨论】:

      • 我真的不明白你在说什么请多解释一下
      • @Nokia808Freak,扩展了一下,希望对您有所帮助。
      • 抱歉,@Nokia,忘记了&lt; 字符,请重试:tr -d '\r' &lt;loginalertinstall.sh &gt;newloginalertinstall.sh
      【解决方案4】:

      命令是命令,您可以使用该工具了解在执行此脚本时发生了什么以进行调试,

      bash -x scriptname.sh
      

      【讨论】:

        【解决方案5】:

        我现在没有 Centos 5,但也许……只是也许……bash 不在 /bin 中?在 FreeBSD 中,它位于 /usr/local/bin。在 Cygwin 中,它位于 /usr/bin。这个命令的输出是什么:

        which bash
        

        【讨论】:

        • 它的输出为 /bin/bash
        • 您使用什么编辑器编写脚本?我能想到的唯一可能性是 Linux 不知何故无法识别 #!/bin/bash 因为隐藏字符,通常是换行符。
        • 嗨,我在 Windows 中使用过 Dreamweaver
        • 尝试在 Linux 中使用 ee、emacs 或 vi 输入前 6 行,看看是否有效……如果有效,那么肯定是换行问题。