【发布时间】:2021-05-23 22:20:23
【问题描述】:
我有一个要在 HPC 集群上运行的 bash 作业。在我的 bash 脚本中,我使用的是作业数组,因此我不会为要提交的每个作业编写单独的脚本。为了提高效率,我将所有要执行的命令(数组中的每个作业 1 个命令)存储在 .txt 文件中,如下所示:
python mybashtest.py --fname 'Sofia' --lname 'Ghnam'
python mybashtest.py --fname 'Loulou' --lname 'Ghnam'
python mybashtest.py --fname 'Leen' --lname 'hkg02'
python mybashtest.py --fname 'Leen Khaled' --lname 'Gh'
我正在使用 python 的argparse 来解析参数。这是我的python脚本:
import argparse
parser = argparse.ArgumentParser(description='My script')
parser.add_argument('--fname', type=str, default='')
parser.add_argument('--lname', type=str, default='')
parsed_args = parser.parse_args()
if __name__ == '__main__':
print(parsed_args.fname + " " + parsed_args.lname)
这是我用来运行作业数组的 shell 脚本:
#!/usr/bin/env bash
#SBATCH --job-name=all_jobs
#SBATCH --account=hkg02
#SBATCH --nodes=1
#SBATCH --array=1-3
module load python/3
# Print the task id.
echo "My SLURM_ARRAY_TASK_ID: " $SLURM_ARRAY_TASK_ID
# here the head -n $SLURM_ARRAY_TASK_ID reads the first n lines
# from the txt file of job command, the # tail -n 1 takes the last line of those.
# A simple trick to associate the Job array number
# with the appropriate line number in the txt file of command
srun $(head -n $SLURM_ARRAY_TASK_ID jobstest.txt | tail -n 1)
我有以下两个问题:
- 我的程序的输出如下(我们以第一个作业为例)
我不希望My SLURM_ARRAY_TASK_ID: 1 'Sofia' 'Ghnam'''成为打印输出的一部分。我不确定它们为什么会出现; 通常我在''中传递任何字符串,因为字符串可能包含空格。什么时候 发生这种情况,请参阅第二个项目符号: - 在最后的工作中,我传递的字符串在
--fname 'Leen Khaled'之间有间隔,我遇到了以下错误
我的 SLURM_ARRAY_TASK_ID:4 用法:mybashtest.py [-h] [--fname FNAME] [--lname LNAME] mybashtest.py:错误:无法识别的参数:Khaled' srun:错误:onode08:任务0:以退出代码2退出 3. 列表项
【问题讨论】:
-
我怀疑这与 slurm 无关。请确认使用您的解释器正常运行时仍然会出现此行为。
-
这与 bash 接受带空格的字符串的方式有关 @AryaMcCarthy
标签: python string bash argparse