git - Read value from file - shell script -
i have jenkinsfile
read values within shell script.
the section of file
-- jenkinsfile stage ('setup vars') { switch(branch_name) { case 'develop': env.echo_chamber_branch = 'develop' env.hendricks_js_integration_branch = 'master' env.features_branch = 'master' break case 'master': env.echo_chamber_branch = 'develop' env.hendricks_js_integration_branch = 'master' env.features_branch = 'master' break default: env.echo_chamber_branch = 'feature-branch' env.hendricks_js_integration_branch = 'feature-branch' env.features_branch = 'feature-branch' break } }
i need read values assigned env.echo_chamber_branch
, env.hendricks_js_integration
, env.features_branch
within default section of case statement, in example feature-branch
returned each variable
using shell script how can these values
any appreciated (any questions please ask)
thanks
improved case_to_var
script (usage "case_to_var filename"):
#!/bin/sed -nf # @ lines ending in ':' /:$/{ # remove leading `case`, spaces, , ':', , save name. s/.* \|://g h # until `break` line, recall name, append variable name, # , make shell-parsable. :foo g n y/./_/ s/'//g s/ = /=/ s/\n.*env//gp /break$/!b foo }
output of case_to_var jenkinsfile
:
develop_echo_chamber_branch=develop develop_hendricks_js_integration_branch=master develop_features_branch=master master_echo_chamber_branch=develop master_hendricks_js_integration_branch=master master_features_branch=master default_echo_chamber_branch=feature-branch default_hendricks_js_integration_branch=feature-branch default_features_branch=feature-branch
making output shell parsable allows executed , used; cautious use of eval
:
eval $(case_to_var jenkinsfile) echo $default_features_branch $develop_features_branch
output:
feature-branch master
one-liner version:
sed -n '/:$/{s/.* \|://g;h;:foo g;n;y/./_/;s/'"'"'//g;s/ = /=/;s/\n.*env//gp;/break$/!b foo}' jenkinsfile
Comments
Post a Comment