【问题标题】:cp: command not foundcp:找不到命令
【发布时间】:2014-08-13 08:52:38
【问题描述】:

我试图将一个文件复制到另一个目录并在调用中断时收到错误消息。

脚本:

#!/bin/bash


PATH=~/MkFile/

exitfn () {
    trap SIGINT              # Resore signal handling for SIGINT
        echo ; echo 'Called ctrl + c '    # Growl at user,

        cp ./BKP/temp.txt $PATH/backup.txt
            exit                     #   then exit script.
}

trap "exitfn" INT            # Set up SIGINT trap to call function.ii



    read -p "What? "

    echo "You said: $REPLY"
# reset all traps## 


    trap - 0 SIGINT

输出:

./signal.sh
What? ^C
Called ctrl + c
./signal.sh: line 9: cp: command not found

你知道这个脚本有什么问题吗??

【问题讨论】:

  • 我相信你不需要在你的脚本中修改PATH。你为什么要碰PATH???

标签: linux bash signals cp bash-trap


【解决方案1】:

您修改了 PATH 变量,这就是原因。也许您只是想为其添加另一条路径:

PATH=$PATH:~/MkFile/

或者如果在Bash 中,只需使用附加运算符:

PATH+=:~/MkFile/

想一想,我认为您实际上并不想使用 PATH 变量。只需使用另一个参数名称即可:

DIR=~/MkFile/

有些人会建议只使用小写字母以避免与内置 shell 变量冲突:

path=~/MkFile/

来自手册:

PATH    A colon-separated list of directories in which the shell looks for
        commands.  A zero-length (null) directory name in the value of PATH
        indicates the current directory. A null directory name may appear
        as two adjacent colons, or as an initial or trailing colon.

【讨论】:

    【解决方案2】:

    在 Linux 中,$PATH 是一个环境变量,它保存用于搜索可执行文件的目录(例如,参见 http://www.linfo.org/path_env_var.html)。

    我真的不知道您的目的是否是更改 PATH 变量。如果是,则应遵循 konsolebox 答案,但如果不是,则应避免在脚本中使用环境变量作为变量。尝试改用:

    路径=~/MkFile/

    MYPATH=~/MkFile/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-18
      • 2020-02-09
      • 2018-09-16
      • 1970-01-01
      • 2016-05-30
      • 2014-11-18
      • 2021-02-10
      相关资源
      最近更新 更多