首页 > 解决方案 > 如何让 bash 脚本接受来自另一个脚本的变量,或者如果它没有接收到以默认值运行的变量?

问题描述

我正在尝试编辑我的脚本,以便不仅在直接运行时工作,为变量设置任何值,它还可以通过另一个脚本运行,该脚本向用户询问这些变量是什么。

我不能将这一切合二为一的原因是,有时我需要在我的大学集群上运行,而在这种情况下我无法读取变量。

这是主脚本中的相关代码:

#!/bin/bash

if [$# -eq 0]; then
    echo "No input provided from control file, running with nwmag_looper values"

    csv_name="testbatch_1510_Chkd"

    Process="bq_tqhi_1510"
    Higgs="tot"
    basefilepath="/scratch/cb27g11/mg_run_basic/nw_mg_run.txt"
    testfilepath="/scratch/cb27g11/mg_run_iridis/nwmag_runcard.txt"

elif [$# -eq 5]; then
    echo "Input provided from control file, running with these values"

    csv_name=$1

    Process=$2
    Higgs=$3
    basefilepath=$4
    testfilepath=$5

    else
      echo "Please provide exactly 5 inputs, csv_name, process name, higgs combination, filepath to basic MG runcard and filepath to functional MG runcard; or run directly from nwmag_looper.sh"
      break
    fi

到目前为止,我一直在尝试直接运行它,bash nwmag_looper.sh但没有成功。在我进行更改以尝试使其双向运行之前,它运行良好,因此我认为在此代码段之外没有任何问题。

我收到这个错误

        (base) [cb27g11@green0591 Looping_Bash]$ bash nwmag_looper.sh 
    nwmag_looper.sh: line 3: [0: command not found
    nwmag_looper.sh: line 13: [0: command not found
    Please provide exactly 5 inputs, csv_name, process name, higgs combination, filepath to basic MG runcard and filepath to functional MG runcard; or run directly from nwmag_looper.sh
    nwmag_looper.sh: line 25: break: only meaningful in a `for', `while', or `until' loop
    cut: /scratch/cb27g11/Looping_Bash/.csv: No such file or directory
    data_coallator starting...
    store_path is currently: /scratch/cb27g11/Data_Ripper_files/
    Traceback (most recent call last):
      File "/scratch/cb27g11/Data_Ripper_iridis/Data_coallator.py", line 17, in <module>
        process = str(sys.argv[2])
    IndexError: list index out of range

语法有问题吗?

标签: bash

解决方案


推荐阅读