0

I have a code of python in lets say test.py. I need to run it using Slurm in a remote location. Thats why I am trying to make a .sh file.

In Putty, I am doing these:

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

Then I make it executable with

chmod u+x main.sh

And I try to run it with

bash main.sh

But I am getting this error:

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

As I try to check

file main.sh

I am getting

main.sh: ASCII text

I am a novice in bash. So I cant understand what the issue is. Can someone help?

2
  • # is the shell comment marker, so the command echo #!/bin/bash > main.sh is treated as echo with the comment #!/bin/bash > main.sh... and of course the comment part is ignored. So you need to quote or escape the #. Depending on what shell you're using and what mode it's in, ! may also be a special character (even in double-quotes!). So it's safest to just single-quote the entire string. Commented Jun 27, 2021 at 23:10
  • @KhabbabZakaria: Depending on how you configure your bash, echo #!/bin/bash will write, when invoked on the command line, #!/bin/bash to stdout, while when invoked in a script (or configured differently), the same statement will just output a newline character. Google for interactive comments to get more insight in this thema. Commented Jun 28, 2021 at 6:29

1 Answer 1

2

The only difference is that I used single quotes

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

If train.py is in another directory, you will have to indicate the location

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.