【问题标题】:Problem with running a .sh script in remote location在远程位置运行 .sh 脚本时出现问题
【发布时间】:2021-06-27 22:47:36
【问题描述】:

我在 test.py 中有一个 python 代码。我需要在远程位置使用 Slurm 运行它。这就是我尝试制作 .sh 文件的原因。

在 Putty 中,我正在做这些:

touch main.sh
echo #!/bin/bash > main.sh
....
echo #SBATCH --gres=gpu:1 >> main.sh
....
echo python3 train.py >> main.sh

然后我让它可执行

chmod u+x main.sh

我尝试用它来运行它

bash main.sh

但我收到此错误:

sbatch: error: This does not look like a batch script.  The first
sbatch: error: line must start with #! followed by the path to an interpreter.
sbatch: error: For instance: #!/bin/sh

当我尝试检查时

file main.sh

我得到了

main.sh: ASCII text

我是 bash 的新手。所以我无法理解问题是什么。有人可以帮忙吗?

【问题讨论】:

  • # 是 shell 注释标记,因此命令 echo #!/bin/bash > main.sh 被视为带有注释 echo#!/bin/bash > main.sh... 当然注释部分被忽略。所以你需要引用或转义#。根据您使用的 shell 和它所处的模式,! 也可能是一个特殊字符(即使是双引号!)。所以最安全的做法是单引号整个字符串。
  • @KhabbabZakaria:根据您配置 bash 的方式,echo #!/bin/bash 在命令行上调用时会将 #!/bin/bash 写入标准输出,而当在脚本中调用(或配置不同),相同的语句将只输出一个换行符。 Google for interactive cmets 以更深入地了解这个主题。

标签: python linux bash shell


【解决方案1】:

唯一的区别是我用了单引号

touch main.sh
echo '#!/bin/bash' > main.sh
echo '#SBATCH --gres=gpu:1' >> main.sh
echo 'python3 --version' >> main.sh

echo 'python3 /home/Desktop/train.py' >> main.sh

如果 train.py 在另一个目录中,则必须指明位置

【讨论】:

    猜你喜欢
    • 2012-02-18
    • 2019-11-12
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多