首页 > 解决方案 > 如何在 Slurm 中使用用户输入

问题描述

我正在寻找通过 SLURM 在 shell 提示符下使用输入。例如,当我使用一个简单的 bash 脚本时:

#!/bin/bash

echo What\'s the path of the files? 

read mypath

echo What\'s the name you want to give to your archive with the files of $mypath? 

read archive_name

echo Ok, let\'s create the archive $archive_name

for i in $mypath/*;
do if [[ -f $i ]]; then
   tar -cvf $archive_name $mypath/*;
   fi;
done

我在提示中使用:

bash my_script.sh 

What's the path of the files? 
/the/path/of/the/files
What's the name you want to give to your archive with the files of $mypath?
my_archive.tar

它会创建存档my_archive.tar。但是现在,我必须将该脚本与 SLURM 一起使用。当我使用sbatch my_script.sh时,它会自动在作业中提交脚本并且我无法添加我的输入:/the/path/of/the/filesmy_archive.tar

任何想法?

标签: bashslurmsbatch

解决方案


你有两个选择:

修改脚本,使其使用参数而不是交互式问题。

该脚本将如下所示:

#!/bin/bash

mypath=${1?Usage: $0 <mypath> <archive_name>}
archive_name=${2?Usage: $0 <mypath> <archive_name>}    

echo Ok, let\'s create the archive $archive_name

for i in $mypath/*;
do if [[ -f $i ]]; then
   tar -cvf $archive_name $mypath/*;
   fi;
done

然后,该脚本将使用bash my_script.sh /the/path/of/the/files my_archive.tar而不是bash my_script.sh . 第一个参数$1在脚本中的变量中可用,第二个参数在 中$2,等等。有关更多信息,请参见this

如果脚本未使用至少两个参数运行,则该语法$(1?Usage...)是发出错误消息的简单方法。有关更多信息,请参阅此

或者,

使用 Expect 自动回答问题

期望命令是(来自文档)

根据脚本与其他交互式程序“对话”的程序。

您可以像这样使用 Expect 脚本:

#!/usr/bin/expect
spawn my_script.sh
expect "What's the path of the files?"
send "/the/path/of/the/files\r"
expect -re "What's the name you want to give to your archive with the files of .*"
send "my_archive.tar\r"
expect

在使 Expect 脚本可执行后,它提供以下内容:

$ ./test.expect 
spawn my_script.sh
What's the path of the files?
/the/path/of/the/files
What's the name you want to give to your archive with the files of /the/path/of/the/files?
my_archive.tar
Ok, let's create the archive my_archive.tar

您可以在 Slurm 提交脚本中运行 Expect 脚本。


推荐阅读