How to create 2 different path folders?

Hi all,

I am new to this forum.

  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

Thanks

What's the contents of A_NAME ?

Thanks for early reply..

I am passing parameter as sample

That doesn't answer the question. Please rerun the script with the -x option set, and echo the A_NAME variable.

1 Like

Thank you.

That was working fine i mean ,i deleted the existing folder in below path

/opt/test/sample abc/def

after deleted and re ran the code.

now it is working fine.

/opt/test/sample in this i have now abc, def folder.

How to if condition in this code.

if deptno=10 then create /opt/test/sample
if deptno=20 then create /opt/test/sample/abc, def

please help me

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.

Thank you.

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

Please suggest

I can't see an attempt to tackle the deptno problem?

---------- Post updated at 17:49 ---------- Previous update was at 17:49 ----------

So I can't comment on it nor correct/improve it.

Hi Boost,

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.

Two create two directories:

mkdir -p /opt/test1/sample/abc /opt/test1/sample/def

Or:

mkdir -p /opt/test1/sample/{abc,def}
1 Like

Ok.

My requirement is

if it is deptno=10 then
/opt/test/sample/$CURRENT_YEAR/$CURRENT_MONTH

if it is deptno=20 then
/opt/test/sample1/$CURRENT_YEAR/$CURRENT_MONTH/$DEPT_NO/$DEPT_NAME

/opt/test is already existing folder path.

finally create a folder as like below.

/opt/test/sample/$CURRENT_YEAR/$CURRENT_MONTH for deptno=10

/opt/test/sample1/$CURRENT_YEAR/$CURRENT_MONTH/10 and Sales for deptno=20

Please help me i am completely new this

---------- Post updated at 09:14 AM ---------- Previous update was at 09:01 AM ----------

Thanks Aja.

mkdir -p /opt/test1/sample/{$abc,$def}

like above right?

Could you please see my Post# 10

Thanks again!!

Hello Boost,

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
 

Thanks,
R. Singh

1 Like

Thank you ravinder.

What is wrong in my code

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

could please help me