A_NAME=$5
DEPT="abc"
DEPT_NAME="cdf"
echo " Name:" $A_NAME | tee -a ${LOG_FILE_NAME}
echo "dept :" $DEPT | tee -a ${LOG_FILE_NAME}
echo "dname :" $DEPT_NAME | tee -a ${LOG_FILE_NAME}
echo "parse_parms done" | tee -a ${LOG_FILE_NAME}
if [ -d "/opt/test/$A_NAME/$DEPT" ]; then
echo "$ folder already exists, not created."
else
mkdir -p "/opt/test/$A_NAME/$DEPT" > /dev/null 2>&1
status=$?
if [ "$status" -eq 0 ]; then
echo "$ folder created"| tee -a ${LOG_FILE_NAME}
else
echo "Error creating $folder" | tee -a $LOG_FILE_NAME
exit 2
fi
fi
if [ -d "/opt/test/$A_NAME/$DEPT_NAME" ]; then
echo "$ folder already exists, not created."
else
mkdir -p "/opt/test/$A_NAME/$DEPT_NAME" > /dev/null 2>&1
status=$?
if [ "$status" -eq 0 ]; then
echo "$ folder created"| tee -a ${LOG_FILE_NAME}
else
echo "Error creating $folder" | tee -a $LOG_FILE_NAME
exit 2
fi
fi
I am trying to create $DEPT and $DEPT_NAME in /opt/test/$A_NAME path,
but it is creating like /opt/test/sample/abc and in the abc it is created def ,
/opt/test/sample/abc/def
But i want to create in /opt/test/sample abc , def (2 folders ) in this path
You already have several if ... fi constructs in your script. Why don't you play around a little trying to find a solution on your own, and post it in either case, be it as a success (so other can learn from it) or with a problem (that can be solved in the discussions in here)?
---------- Post updated at 17:38 ---------- Previous update was at 17:37 ----------
By the way, for above problem a case ... esac statement might offer an elegant solution.
if [ -d "/opt/test/$A_NAME/$DEPT" ]; then
echo "$ folder already exists, not created."
else
mkdir -p "/opt/test/$A_NAME/$DEPT" > /dev/null 2>&1
status=$?
if [ "$status" -eq 0 ]; then
echo "$ folder created"| tee -a ${LOG_FILE_NAME}
else
echo "Error creating $folder" | tee -a $LOG_FILE_NAME
exit 2
fi
fi
Ok. I am request to could pls help me how to write a code in shorter.
/opt/test/$A_NAME/$DEPT
/opt/test/sample -- in the sample folder need to create abc,def
and create a folder based on condition
if deptno=10 then
/opt/test/sample
if deptno=20 then
/opt/test1/sample/abc,def
Forgive me if I do not have time to go through your whole thread conversation, however, I would like to point out that one of the benefits of using the flag -p with mkdir is that it will not report errors, therefore you do not need the redirection > /dev/null 2>&1
Also you do not need to check if the directory exist since mkdir -p will do nothing if the directory exist.
It is a request please use code tags as per forum rules for Inputs/codes/commands which you are using in your posts as per forum rules, following may help you in same.(Not tested though)
cat script.ksh
### NOTE: Please insert variables like CURRENT_YEAR etc values into script too.
mkdir /opt/test/sample/$CURRENT_YEAR
mkdir /opt/test/sample/$CURRENT_YEAR/$CURRENT_MONTH
if [[ $deptno == 10 ]]
then
mkdir /opt/test/sample/$CURRENT_YEAR/$CURRENT_MONTH/$DEPT_NO
else
echo "Either value of deptno is NOT 10, please check.
fi
if [[ $deptno == 20 ]]
then
mkdir /opt/test/sample/$CURRENT_YEAR/$CURRENT_MONTH/$DEPT_NAME
else
echo "Either value of deptno is NOT 20, please check.
fi
mkdir /opt/test/sample/$CURRENT_YEAR/$CURRENT_MONTH/"10 and sales"
If not needed you could remove echo statements in above else statements. If you have some queries or getting issues please post complete requirement, with your O.S details too(along with code tags if necessary).
EDIT: Just now saw you have changed your POST#10 things a lot, following may help you in same, please try and let me know if this helps you then.
cat script.ksh
### NOTE: Please insert variables like CURRENT_YEAR etc values into script too.
mkdir /opt/test/sample/$CURRENT_YEAR
if [[ $deptno == 10 ]]
then
mkdir /opt/test/sample/$CURRENT_YEAR/$CURRENT_MONTH/"10 and Sales for "$deptno
else
echo "Either value of deptno is NOT 10, please check.
fi
if [[ $deptno == 20 ]]
then
if [[ ! -d /opt/test/sample/$CURRENT_YEAR/$CURRENT_MONTH ]]
then
mkdir /opt/test/sample/$CURRENT_YEAR/$CURRENT_MONTH
fi
mkdir /opt/test/sample/$CURRENT_YEAR/$CURRENT_MONTH/"10 and Sales for "$deptno
else
echo "Either value of deptno is NOT 20, please check.
fi
dept_no=`sqlplus -silent apps/test<<EOF
set pagesize 0
set feedback off
set verify off
set heading off
select deptno from emp
where deptno=10;
exit;
EOF`
echo $dept_no
dept_no1=`sqlplus -silent apps/test<<EOF
set pagesize 0
set feedback off
set verify off
set heading off
select deptno from emp
where deptno=20;
exit;
EOF`
echo $dept_no1
DEPT_NO="DEPT"
D_NAME="DNAME"
if [[ $dept_no == 10 ]]
then
mkdir -m 777 -p /opt/test/sample/$CURRENT_YEAR/$CURRENT_MONTH >/dev/null 2>&1| tee -a ${LOG_FILE_NAME}
echo " Structure Created Successfully." | tee -a ${LOG_FILE_NAME}
else
echo "10 dept Directory not created "| tee -a $LOG_FILE_NAME
exit 2
fi
if [[ $dept_no1 == 20 ]]
then
mkdir -m 777 -p /opt/test/sample/$CURRENT_YEAR/$CURRENT_MONTH/$DEPT_NO >/dev/null 2>&1 | tee -a ${LOG_FILE_NAME}
echo " Structure Created Successfully." | tee -a ${LOG_FILE_NAME}
else
echo "20 dept Directory not created "| tee -a $LOG_FILE_NAME
exit 2
fi