Using name of BASH script as input argument

本秂侑毒 提交于 2021-01-29 09:47:11

问题


I have a large number of .pbs files that I want to submit to a remote cluster. I want to be able to name the .pbs file something like "param1_123_param2_45.pbs", and then feed them into the ARGS for a Julia code. Below is an example .pbs of what I'm trying to do:

1 #!/bin/tcsh 
2 #PBS -l mem=10gb,nodes=1:ppn=2,walltime=1:00:00
3 #PBS -j oe
4 #PBS -o ./log/julia.${PBS_JOBID}.out
5 #PBS -t 1-3
6 
7 module load julia/1.5.1 python/3.8.1
8 
9 cd path/to/file  
10 
11 julia Example.jl 123 45

Except 123 & 45 are replaced by some general terms given in the name of the .pbs file. Is there an easy way to do this?


回答1:


You can use AWK. For example, content of file param1_123_param2_45.pbs:

PARAM1=$( echo ${0%%.pbs} | awk -F "_" '{print $2}' )
PARAM2=$( echo ${0%%.pbs} | awk -F "_" '{print $4}' )
echo "Filename: $0"
echo "Param 1: $PARAM1"
echo "Param 2: $PARAM2"

Running: bash param1_123_param2_45.pbs

Output:

Filename: param1_123_param2_45.pbs
Param 1: 123
Param 2: 45


来源:https://stackoverflow.com/questions/65742185/using-name-of-bash-script-as-input-argument

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!