首页 > 解决方案 > 在google colab中执行shell文件时没有这样的文件或目录

问题描述

我在 google colab 中运行机器翻译代码,因为我将代码克隆到我的驱动器中,并将驱动器位置存储在一个变量中:

!PATH='/content/mydrive/My Drive/facebook/unsup'

克隆在所需位置成功完成。当我尝试运行 shell 文件时出现问题:

!."$PATH/NMT/get_data_enfr.sh"

它返回

/bin/bash: ./content/mydrive/My Drive/facebook/unsup/NMT/get_data_enfr.sh: No such file or directory

但如果我跑

!head "$PATH/NMT/get_data_enfr.sh"

它显示文件内容。

# Copyright (c) 2018-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#

set -e

#

标签: bashgoogle-colaboratory

解决方案


您可以尝试将脚本运行为:

sh "$PATH/NMT/get_data_enfr.sh"

在像上面那样运行它之前,请运行以下命令:

chmod +x "$PATH/NMT/get_data_enfr.sh"

似乎@dash-o 是正确的,当您在脚本.前面运行脚本时,它实际上是从当前目录中寻找脚本。

它适用于head命令,因为完整路径正在传递给它,head并且它正在该路径中找到文件,但在运行它时不是真的。

./path/to/script实际上试图从当前目录运行脚本并忽略绝对路径。但是当使用shand then时$PATH,它实际上是从绝对路径运行的。


推荐阅读