我正在使用服务器上的PBS队列。 我想自动提交几个工作 因此需要用路径替换变量。 因为无法获得参数 运行脚本与队列一起运行,我想 替换通常使用的shell变量 使用硬编码参数。
我需要的是:
#! /bin/sh
#PBS -N runme_$file
#PBS -o $file.out
#PBS -e $file..err
#PBS -q batch
#PBS -j oe
#PBS -m abe
#PBS -r n
#PBS -l nodes=1:ppn=1
#PBS -l walltime=24:00:00
/home/example/proggie script $file $parameter2
echo "DONE"
用'/ tmp / output'替换$ file,因此应将其转换为:
#! /bin/sh
#PBS -N runme_/tmp/output
#PBS -o /tmp/output.out
#PBS -e /tmp/output.err
#PBS -q batch
#PBS -j oe
#PBS -m abe
#PBS -r n
#PBS -l nodes=1:ppn=1
#PBS -l walltime=24:00:00
/home/example/proggie script /tmp/output $parameter2
/home/example2/proggie2 /tmp/output
echo "DONE"
PBS部分很重要,因为它阻碍了参数的使用。 所以我不能只使用$ 1和$ 2并给脚本提供参数 它需要通过参数。相反,我必须通过一个更改文件 替换命令,然后我可以将它提交给排队系统。 我仍然希望能够从命令行使用脚本, 用于快速测试。
我已经尝试过使用sed,但是特殊字符的可能性 发生在文件中搞砸了一切。必须有更好的方法。
答案 0 :(得分:0)
如果您将$ file指定为$ {file},该怎么办?然后你应该能够使用sed。当你说“特殊字符”时,我假设你指的是其他$符号。至少这是我在你发布的内容中唯一能看到的内容。
答案 1 :(得分:0)
使用sed
:
sed "s%\$file%$file%" script.in > script.out
使用awk
(gawk
或GNU Awk):
awk '{ gsub(/\$file/, file); print }' file="$file" script.in > script.out
在这两个示例中,我假设此编辑上下文在$file
中具有正确的文件名值。我还假设您在脚本中没有任何其他变量,例如$file1
或$filename
;如果你确实有这样的变量,正则表达式必须更复杂。我还假设你在sed
示例中没有在文件名中嵌入百分号。