Discussion:
[Help-bash] export
Val Krem
2017-02-04 01:05:08 UTC
Permalink
Hi all,

I am trying to export a variable from a shell script. The script is working well if I run my script without an argument ( $#=0). I am inter4ested in to export the variable if the $# -ge 1 but did not work for me. Below is my my "broken" script.


######################### mtest.sh ###########################################

#!/bin/bash

ftmp=$(date +%m%Y);export ftmp # ftmp= 022017
mp=/val/data/

flag=$1; export flag
nwork=$2; export nwork
narg=$#; export narg


if [ ${narg} -eq 0 ]; then
if [ ! -d "${mp}"/"${ftmp}" ]; then
mkdir "${mp}"/"${ftmp}"
fi
mp2="${mp}"/"${ftmp}"; export mp2

elif [ "${narg}" -ge 1 ] && [ "${flag}" != "-t" ]; then
echo "illegal option!"
exit 1
else

if [ "${narg}" -eq 2 ] && [ "${flag}" == "-t" ]; then
if [ ! -d ${mp}/${ftmp}/${nwork} ]; then
mkdir ${mp}/${ftmp}/${nwork}
fi
mp2=${mp}/${ftmp}/${nwork} ; export mp2
fi
fi
echo "end of testing"
##############################################################


1. if I execute ./mtest.sh
I am getting : mp2= /val/data/022017

2. if I execute with ./mtest -t tdat
still I am getting mp2= /val/data/022017

but I want to get mp2= /val/data/tdat/022017.

I would appreciate if you help me out here

thank you in advance
Pierre Gaston
2017-02-04 01:31:53 UTC
Permalink
Post by Val Krem
Hi all,
I am trying to export a variable from a shell script. The script is
working well if I run my script without an argument ( $#=0). I am
inter4ested in to export the variable if the $# -ge 1 but did not work
for me. Below is my my "broken" script.
######################### mtest.sh ##############################
#############
#!/bin/bash
ftmp=$(date +%m%Y);export ftmp # ftmp= 022017
mp=/val/data/
flag=$1; export flag
nwork=$2; export nwork
narg=$#; export narg
if [ ${narg} -eq 0 ]; then
if [ ! -d "${mp}"/"${ftmp}" ]; then
mkdir "${mp}"/"${ftmp}"
fi
mp2="${mp}"/"${ftmp}"; export mp2
elif [ "${narg}" -ge 1 ] && [ "${flag}" != "-t" ]; then
echo "illegal option!"
exit 1
else
if [ "${narg}" -eq 2 ] && [ "${flag}" == "-t" ]; then
if [ ! -d ${mp}/${ftmp}/${nwork} ]; then
mkdir ${mp}/${ftmp}/${nwork}
fi
mp2=${mp}/${ftmp}/${nwork} ; export mp2
fi
fi
echo "end of testing"
##############################################################
1. if I execute ./mtest.sh
I am getting : mp2= /val/data/022017
2. if I execute with ./mtest -t tdat
still I am getting mp2= /val/data/022017
but I want to get mp2= /val/data/tdat/022017.
I would appreciate if you help me out here
thank you in advance
You simply can't do that running a regular script, see
http://mywiki.wooledge.org/BashFAQ/060

What you see is probably the result of having set mp2 at some point, if you
unset it and run your script again you will see it doesn't change the value
of mp2

Loading...