循环使用Korn shell中Rscript中的不同参数
我在终端中运行了一个R脚本,首先生成一个名为myscript.ksh的.ksh文件,其中包含以下信息:循环使用Korn shell中Rscript中的不同参数,r,terminal,ksh,R,Terminal,Ksh,我在终端中运行了一个R脚本,首先生成一个名为myscript.ksh的.ksh文件,其中包含以下信息: #!/bin/ksh Rscript myscript.R 'Input1' #!/bin/ksh if [ $# -ne 1 ]; then echo "Usage: $0 input" exit 1 fi # Or start at the background with nohup .... &, other question Rscript myscript.
#!/bin/ksh
Rscript myscript.R 'Input1'
#!/bin/ksh
if [ $# -ne 1 ]; then
echo "Usage: $0 input"
exit 1
fi
# Or start at the background with nohup .... &, other question
Rscript myscript.R "$1"
然后用
./mycode.ksh
它将脚本发送到我们部门集群上的一个节点(我们发送到集群的进程必须是.ksh文件)
“Input1”是R脚本用于某些分析的输入参数
我现在遇到的问题是,我需要使用函数的不同输入参数多次运行此脚本。一种解决方案是生成几个.ksh文件,例如:
#!/bin/ksh
Rscript myscript.R 'Input2'
及
然后分别执行,但我希望找到更好的解决方案
请注意,我必须对100个不同的输入参数执行此操作,因此编写100个这样的文件是不现实的。是否有方法生成另一个文件,其中包含需要提供给函数的信息,例如“Input1”“Input2”“Input3”,然后分别为这些文件运行myscript.ksh
例如,我可以使用一个变量定义输入参数的名称,然后使用一个循环将其传递给myscript.ksh。可能吗
以这种方式运行这些程序的原因是,每次迭代都有希望发送到集群上的不同节点,从而以更快的速度分析数据 您需要做两件事:
#!/bin/ksh
#Create array of inputs - space separator
inputs=(Input1 Input2 Input3 Input4)
# Loop through all the array items {0 ... n-1}
for i in {0..3}
do
echo ${inputs[i]}
done
这将输出输入数组中的所有值
您只需将do循环的内容替换为:
Rscript myscript.R ${inputs[i]}
此外,您可能需要在Rscript命令行的末尾添加一个“&”,以将每个Rscript命令作为一个单独的线程派生出来——否则,shell将等待每个Rscript命令返回,然后再继续下一个命令
编辑: 根据您的评论,您需要实际生成.ksh脚本以提交到
qsub
。为此,只需展开do
循环即可
例如:
#!/bin/ksh
#创建输入数组-空格分隔符
输入=(输入1输入2输入3输入4)
#循环遍历所有数组项{0…n-1}
对于{0..3}中的i
做
cat>submission.ksh当脚本启动时不知道所有参数时,可以创建一个名为mycode.ksh的.ksh文件,其中包含以下信息:
#!/bin/ksh
Rscript myscript.R 'Input1'
#!/bin/ksh
if [ $# -ne 1 ]; then
echo "Usage: $0 input"
exit 1
fi
# Or start at the background with nohup .... &, other question
Rscript myscript.R "$1"
然后用
/mycode.ksh输入
当应用程序知道所有参数时,可以使用循环:
#!/bin/ksh
if [ $# -eq 0 ]; then
echo "Usage: $0 input(s)"
exit 1
fi
for input in $*; do
Rscript myscript.R "${input}"
done
然后用
./mycode.ksh input1 input2 "input with space in double quotes" input4
输入参数的命名是否一致?例如,输入#,其中#来自1。。。100?否。它们有特定的名称。问题是,我需要为每个输入参数执行不同的.ksh脚本(以便将其发送到集群上的不同节点)。您建议将.ksh发送到一个集群。假设我想对两个不同的运行执行此操作,一个运行输入'input1',另一个运行输入参数'blahblah'。我想我需要另一个文件来生成不同的.ksh脚本(每个ksh覆盖最后一个),并使用qsub myscript.kshEdited answer执行,以响应您的澄清。