Val Krem
2017-02-04 01:05:08 UTC
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
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