-1

so I have this bash file called test.sh

#!/bin/bash -l out_file=`basename "$0"` #SBATCH --gpus=1 #SBATCH -p long #SBATCH -o err_${out_file::-3}.out 

and I want to output the error to err_test.out, which is what I'm trying to do in the above file, but it's giving an error. Doesn't like the = how can I set the output file to the file name I specified.

1
  • "it's giving an error" - do we have to guess or will you add it to your question? Commented Jul 15, 2022 at 23:21

1 Answer 1

1

Here's the general approach:

#!/bin/bash out_file=$(basename "$0") echo sbatch --gpus=1 -p long -o "err_${out_file%.*}.out" 

If that outputs the sbatch command you're trying for, then remove the echo and give it a try.

A couple of comments:

  • 99% of the time you don't want a shell that's running your script to behave as a login shell, so I removed the -l option from the top line.
  • The $( ... ) method of capturing the output of a command is usually better than the old-style method that uses backticks/backquotes. There are many fewer problems with special characters and nesting one capture within another.
  • You had the right idea about how to strip trailing characters from the $out_file variable, I was lazy and didn't test your expression, instead I replaced it with one I use a lot, and is somewhat simpler.
9
  • much appreciated! Commented Jul 16, 2022 at 4:33
  • hmmm apologies I gave the accepted answer before testing it. Put this in the bash script !/bin/bash -l out_file=$(basename "$0") #SBATCH --gpus=1 #SBATCH -p long #SBATCH -o "err_${out_file%.*}.out" and it didn't output to that file for some reason. Echo printed out the correct string, but it's not outputting to that file. Any ideas? Commented Jul 16, 2022 at 5:49
  • You need to tell basename the suffix. But, having a .sh suffix to a file name is an anti-pattern. Commented Jul 16, 2022 at 16:34
  • @Gooby I don't understand how you intend the #SBATCH lines in your script to work. They don't match up to how a shell script functions except as a way to comment out (disable) lines, and that appears to be why it doesn't work when you try to use them. Can you explain what you're trying to do with them? Are you hoping to use multiple lines of the script to create the full sbatch command? Commented Jul 16, 2022 at 16:36
  • sorry I'm not super familiar with slurm and Linux this is just how I've seen others in my lab sbatch a slurm job. I don't even know what #SBATCH does exactly. Commented Jul 17, 2022 at 17:17

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.