Search code examples
bashslurmsbatch

How to use User Inputs with Slurm


I'm looking for using inputs in the shell prompt by way of SLURM . For example, when I use a simple bash script like :

#!/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

And I use in the prompt :

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

And it creates the archive my_archive.tar . But now, I have to use that script with SLURM. When I use sbatch my_script.sh , that automatically submits the script in a job and I can't add my inputs : /the/path/of/the/files and my_archive.tar

Any idea?


Solution

  • You have two options:

    Modify the script so that it uses parameters rather than interactive questions.

    The script would then look like this:

    #!/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
    

    This script would then be run with bash my_script.sh /the/path/of/the/files my_archive.tar rather than bash my_script.sh . The first argument is made available in the $1 variable in the script, the second one is in $2, etc. See this for more info.

    The syntax $(1?Usage...) is a simple way to issue an error message if the script is not run with at least two arguments. See this for more information

    or,

    Use Expect to answer the questions automatically

    The expect command is (from the doc)

    a program that "talks" to other interactive programs according to a script.

    You could use an Expect script like this:

    #!/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
    

    It gives the following, after making the Expect script executable:

    $ ./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
    

    You can run the Expect script in a Slurm submission script.