如何在shell脚本中分配变量 [英] how to assign variable in shell script

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

问题描述

我下面的shell脚本代码运行正常。

I have below shell script code which is working fine.

#!/bin/sh

run() {
  cd /tmp/in/current 
  java \
    -Dlog4j.configurationFile=/tmp/in/logging/log4j2_Importer.xml \
    -Djava.security.egd=file:///dev/urandom \
    -classpath /tmp/in/runner/lib/*:/tmp/in/lib/* \
    baag.runner.Application \
    --config /tmp/in/config/import.dev.properties.TODO \
    --workflow import \
    --inputDir "$1"
}

dev_path="/data/etl-dev/in/eurex"
simu_path="/data/etl-simu/in/eurex"

mode=$1

case "$mode" in
    "$dev_path" | "$simu_path" ) 
        run "$mode" 
        ;;
    *)  echo "error: invalid mode" >&2
        exit 1
        ;;
esac

但是目前我只能在dev数据库上运行此代码,如您在运行功能脚本 import.dev.properties.TODO 设置为dev。我想使它灵活,以便如果simu_path变量的路径为 / tmp / in / simu,则属性应为 import.simu.properties.TODO 并为dev_path应该为 import.dev.properties.TODO ,以便它将在相应的数据库上运行。

But currently i can run this code only on dev database as you can see in run function script the import.dev.properties.TODO is set as dev. I want to make this flexible such that if the path is "/tmp/in/simu" for simu_path variable then the properties should be import.simu.properties.TODO and for dev_path it should be import.dev.properties.TODO so that it will run on the respective database.

我不确定是否可以在此处设置参数化变量。例如这样的 import。$ {varaible_here_dev_or_simu} .properties.TODO

I am not sure if its possible to set parametarized variable here. For example something like this import.${varaible_here_dev_or_simu}.properties.TODO

我想保留dev_path和simu_path可以更改
,因为我在参数中传递了它

I want to keep the dev_path and simu_path as it is as it can be changed as i am passing this in argument

推荐答案

据我所知,不幸的是,bash不支持诸如数组之类的构造,这可能是较早版本4的解决方案。

As far as I know, bash unfortunately does not support constructs like associative arrays, which could be a possible solution, prior version 4.

如果环境的路径看起来都一样,那么您可以这样写。

If the paths for the environments all look like the same, you could write it like this.

#!/bin/sh

base_path="/tmp/in"

dev_env="dev"
simu_env="simu"

run() {
cd /tmp/in/current; java -Dlog4j.configurationFile=/tmp/in/logging/log4j2_Importer.xml -Djava.security.egd=file:///dev/urandom -classpath /tmp/in/runner/lib/*:/tmp/in/lib/* baag.runner.Application --config /tmp/in/config/import.$1.properties.TODO --workflow import --inputDir "$base_path/$1"
}

mode=$1

case "$mode" in
    "$dev_env" | "$simu_env" ) 
        run "$mode" 
        ;;
    *)  echo "error: invalid mode" >&2
        exit 1
        ;;
esac

注意:在此实现中,您必须通过 dev simu 而不是整个路径。如果需要通过完整的路径,则必须更改 $ dev_env | $ simu_env) $ base_path / $ dev_env | $ base_path / $ simu_env)

Note: In this implementation you would have to pass dev or simu to the script instead of the whole path. If you need to pass the complete path you have to change the "$dev_env" | "$simu_env" ) to "$base_path/$dev_env" | "$base_path/$simu_env" )

更新

假设路径结构和环境是固定的,则可以使用简单的正则表达式提取环境并将其作为秒参数传递给函数,如下所示:

Assuming the path structure and the environments are fixed, you can extract the environment with a simple regex and pass it to the function as the seconds parameter, like so:

#!/bin/sh

dev_path="/data/etl-dev/in/eurex"
simu_path="/data/etl-simu/in/eurex"
prod_path="/data/etl-prod/in/eurex"

environments="(dev|simu|prod)"

run() {
    cd /tmp/in/current; java -Dlog4j.configurationFile=/tmp/in/logging/log4j2_Importer.xml -Djava.security.egd=file:///dev/urandom -classpath /tmp/in/runner/lib/*:/tmp/in/lib/* baag.runner.Application --config /tmp/in/config/import.$2.properties.TODO --workflow import --inputDir "$1"
}

mode=$1

case "$mode" in
    "$dev_path" | "$simu_path" )
        environment=$(echo $mode | sed -E "s/.*${environments}.*/\\1/")
        run "$mode" $environment
        ;;
    *)  echo "error: invalid mode" >&2
        exit 1
        ;;
esac

这篇关于如何在shell脚本中分配变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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