bash - 如何在 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/files
和my_archive.tar
任何想法?
解决方案
你有两个选择:
修改脚本,使其使用参数而不是交互式问题。
该脚本将如下所示:
#!/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 脚本。
推荐阅读
- bash - 使用 sed 删除多个主题标签
- java - 使用 Java 8 流 API 的累积和
- javascript - (HTML + JS 问题)将输入类型文件中的图像加载到 img
- java - 使用嵌入式 Postgressql 数据库进行 Spring Boot 测试
- php - 在搜索 Algolia 索引时使关系数据在 eloquent 集合中可用
- db2 - 创建联合(数据虚拟化)服务器时的通信错误(目标为 DB2 on Cloud)
- sql - SQL:根据另一列中的重复值查询一列
- vue.js - vuetify 子组件属性
- video - ffmpeg 录制延时摄影机 v4l2
- c++ - C++ 查找从属名称