bash脚本创建脚本变量 [英] bash script create script variable

查看:166
本文介绍了bash脚本创建脚本变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个创建脚本(并执行其他操作)的脚本。我的问题是创建的这些脚本包含环境变量,并且在实际运行我的脚本时,它们不会出现在脚本中。

I'm trying to create a script that creates scripts (and do other things). My problem is that these scripts created contain environment variables, and when actually running my script, they do not appear in the script.

#!/bin/bash
for i in {001..116}
do
   rm job
   cat > job <<!
#PBS -S /bin/bash
#PBS -N $i-Fe63S
#PBS -j oe
#PBS -q default
#PBS -l nodes=1:ppn=24
#PBS -l walltime=48:00:00
#PBS -V
cd $PBS_O_WORKDIR  
/opt/openmpi/bin/mpirun -np 24 /opt/vasp/5.4/vasp.5.4.1/bin/vasp_std > log
!
   mkdir $i
   cp job $i/
done

在生成的作业文件中,它尝试,但找不到$ PBS_O_WORKDIR,因此生成的脚本是

In the resulting "job" file created, it attempts, but fails to find the $PBS_O_WORKDIR, so the resulting script is

#PBS -S /bin/bash
#PBS -N 116-Fe63S
#PBS -j oe
#PBS -q default
#PBS -l nodes=1:ppn=24
#PBS -l walltime=48:00:00
#PBS -V

cd

/opt/openmpi/bin/mpirun -np 24 /opt/vasp/5.4/vasp.5.4.1/bin/vasp_std > log

我如何修改脚本,所以结果脚本中的行写为cd $ PBS_O_WORKDIR 而不是cd?

How do i modify the script, so the line in the result script is written "cd $PBS_O_WORKDIR" rather than "cd"?

推荐答案

浏览本文档中的变量/表达式,在您写文件时,需要扩展。

Go through the variables/expressions in your here document and escape the ones you don't want expanded at the point when you write the file.

在这种情况下,您需要扩展现在的 $ i $ PBS_O_WORKDIR 稍后展开,所以你逃避后者:

In this case you want $i expanded now, and $PBS_O_WORKDIR expanded later, so you escape the latter:

#!/bin/bash
for i in {001..116}
do
   rm job
   cat > job <<!
#PBS -S /bin/bash
#PBS -N $i-Fe63S
#PBS -j oe
#PBS -q default
#PBS -l nodes=1:ppn=24
#PBS -l walltime=48:00:00
#PBS -V
cd \$PBS_O_WORKDIR    # <- Escaped
/opt/openmpi/bin/mpirun -np 24 /opt/vasp/5.4/vasp.5.4.1/bin/vasp_std > log
!
   mkdir $i
   cp job $i/
done

这篇关于bash脚本创建脚本变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆