How to create folder by using if and elif condition?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to create folder by using if and elif condition?
# 1  
Old 02-11-2016
How to create folder by using if and elif condition?

Hi All,

I have already code to create folder for one country let say US , now we want to create folder/directory for JP country also using shell script , application server.

Code:
$COUNTRY='US'
if [[ $COUNTRY_CODE = "US" ]]
then
 if  mkdir -m 777 -p /opt/TEST/$COUNTRY/$INVOICE >/dev/null 2>&1 | tee -a  ${LOG_FILE_NAME}
 then
 echo "Directory Structure Created Successfully." | tee -a  ${LOG_FILE_NAME}
 else
 echo "Directory not created "| tee -a $LOG_FILE_NAME
 exit 2 
 fi
fi

$COUNTRY='JP'
if [[ $COUNTRY_CODE = "JP" ]]
then
 if  mkdir -m 777 -p /opt/TEST/$COUNTRY/$INVOICE >/dev/null 2>&1 | tee -a  ${LOG_FILE_NAME}
 then
 echo "Directory Structure Created Successfully." | tee -a  ${LOG_FILE_NAME}
 else
 echo "Directory not created "| tee -a $LOG_FILE_NAME
 exit 2 
 fi
fi

I can use same code as like US , but i need to use the code in one line of could you pls help me on this
# 2  
Old 02-11-2016
There are a few issues here:
  • The $ is not needed and is not allowed when you are assigning a value to a variable.
    Code:
    $COUNTRY="some country code"

    is a syntax error. Presumably, you want:
    Code:
    COUNTRY="some country code"

    instead.
  • Why do you set COUNTRY and check COUNTRY_CODE? You need to pick one variable name and use it consistently.
  • Why do you set a variable on one line and then think you need to test to see if that variable has been set to the value you just assigned to it on the next line?
  • Is there some reason why only two countries should be allowed by your script? Why is the test needed at all? Do you just need a test to verify that COUNTRY has been assigned a value that is not the empty string?
If you want to get a country code, verify that it is JP or US and create the directory only if it is one of those two country codes, you could try something more like:
Code:
printf 'Enter country code: '
read -r COUNTRY
if [ "$COUNTRY" = "JP" ] || [ "$COUNTRY" = "US" ]
then
  if mkdir -m 777 -p "/opt/TEST/$COUNTRY/$INVOICE" >/dev/null 2>&1 | tee -a  "${LOG_FILE_NAME}"
  then
    echo 'Directory Structure Created Successfully.' | tee -a  "${LOG_FILE_NAME}"
  else
    echo 'Directory not created' | tee -a "$LOG_FILE_NAME"
    exit 1
  fi
else
  echo "Unexpected country code '$COUNTRY' specified; Directory not created" | tee -a "$LOG_FILE_NAME"
  exit 2
fi

but, of course, none of this works unless something you haven't shown us assigns appropriate values to the variables INVOICE and LOG_FILE_NAME. And, since you haven't told us what shell you're using, I can't make any guarantee that any of this will work.

Last edited by Don Cragun; 02-11-2016 at 04:43 AM.. Reason: Numbered list not working with embedded CODE.
# 3  
Old 02-11-2016
I'm pretty sure that piping mkdir's output through tee will (almost) always succeed, so "directory not created" will never happen (unless tee fails for some reason):
Code:
if mkdir /XX | tee YY ; then echo good; else echo bad; fi
mkdir: cannot create directory ‘/XX’: Permission denied
good

This User Gave Thanks to RudiC For This Post:
# 4  
Old 02-11-2016
RudiC is correct... And, in addition to that, redirecting output to /dev/null (even though mkdir never writes anything to standard output) and then redirecting standard error to standard output throws away any diagnostic messages that mkdir might produce. But, you should be able to change:
Code:
  if mkdir -m 777 -p "/opt/TEST/$COUNTRY/$INVOICE" >/dev/null 2>&1 | tee -a  "${LOG_FILE_NAME}"

to:
Code:
  mkdir -m 777 -p "/opt/TEST/$COUNTRY/$INVOICE" 2>&1 | tee -a  "${LOG_FILE_NAME}"
  if [ -d "/opt/TEST/$COUNTRY/$INVOICE" ]

to get what you want. Note, however, that if the directory already existed, the mkdir will fail, but you still have the directory you want. If you don't want to get EEXIST errors when the directory already exists, you might want to try something more like:
Code:
  DIR="/opt/TEST/$COUNTRY/$INVOICE"
  [ ! -d "$DIR" ] && mkdir -m 777 -p "$DIR" 2>&1 | tee -a  "${LOG_FILE_NAME}"
  if [ -d "$DIR" ]

to give you a success message if the directory has been created at some point in the past or during the execution of your script and the desired error messages if you didn't successfully create the desired directory.
# 5  
Old 02-11-2016
The -p option to mkdir will make it "not fail" on existing directories
Quote:
-p, --parents
no error if existing, make parent directories as needed
, so sth serious must happen to select the failure branch.
# 6  
Old 02-11-2016
Quote:
Originally Posted by RudiC
The -p option to mkdir will make it "not fail" on existing directories, so sth serious must happen to select the failure branch.
Serious, yes. Unusual, no. Unlikely, maybe not. If the directory in which a new directory is to be created is not writeable by the user running the script, mkdir -p will fail with an EPERM error. If a non-directory (or symbolic link pointing to a non-directory) file exists with the name of a directory to be created, mkdir -p will fail with an EEXIST error. And, of course, EACCES, ELOOP, EMLINK, ENAMETOOLONG, ENOSPC, ENOTDIR, and EROFS errors are also possible.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with condition "if then elif else fi"

Hi everybody, I must get trought a shell script 3 arguments. 1st argument = $1 (can take values REP1..4) 2nd argument = $2 (can take values A..Z) 3rd arguement = $3 (also can take values A...Z) I've written this code : #!/bin/bash if then liste=/data/folder1 echo... (6 Replies)
Discussion started by: shellX
6 Replies

2. Shell Programming and Scripting

Create a shared folder using acl

Hello. I need some help to create a shared folder. A group 'publicuser' has been created. A user 'publicuser' has been created ( no login, no home) and belongs to group 'publicuser'. A public folder '/doc' has been created and owner is publicuser:publicuser. All users belonging to group... (12 Replies)
Discussion started by: jcdole
12 Replies

3. Shell Programming and Scripting

Create Dynamic if condition

Create Dynamic If condition Hi, I have a file color.txt which has data as shown below Red Blue Green Yellow White Pink Black Based on a variable I execute a tail command as shown below tail -${LEFT_OVR} color.txt LEFT_OVR can be any number less than count of number of lines in a... (7 Replies)
Discussion started by: wahi80
7 Replies

4. UNIX for Dummies Questions & Answers

Create a file on UNIX with multiple columns on certain condition

I need to write the list of files to a new file in one column , the second column would contain the first line of that file (header record extracted through head -1 ) and the third column would contain the last record of that file (trailer record tail -1 ) . Example :- folder where the files... (8 Replies)
Discussion started by: IshuGupta
8 Replies

5. UNIX for Dummies Questions & Answers

Create a condition for a non-included string

hey, just want to ask how to check this scenario a="apple banana cherry" if egg=0 fi how do you do the condition? thanks! (2 Replies)
Discussion started by: h0ujun
2 Replies

6. Shell Programming and Scripting

create separate file after checking condition..

Problem : I want to create a separate file for country list if condition is true. Please help. ***************************************************** Input file: SV-INCR-139302-365540488-201104090934.sqllog SV-INCR-1082-552793184-201104040805.sqllog SV-INCR-1077-855045741-201104040805.sqllog... (4 Replies)
Discussion started by: humaemo
4 Replies

7. Shell Programming and Scripting

how to create folder and sub-folder in UNIX ?

Hi all, I have the following code to check the whether the folder is exist in my system. if ; then echo 'folder exist'; else echo 'folder not exist'; mkdir /home/batch/testing ; fi When I remove the "testing" folder from "/home/batch" directory, the code is working fine. But when I... (2 Replies)
Discussion started by: suigion
2 Replies

8. Shell Programming and Scripting

how to create folder wen the condition satisfied

hi i hav files ha1j ha2m ha3n ha4q ha5s ...like tat im having some 20 files ..and i want to create a folder as the same amount of files which im having wen the condition if loop is satisfied .. thank you (5 Replies)
Discussion started by: maximas
5 Replies

9. AIX

AIX - create folder in hdisk1 instead

Dear All, I have two h.disks. Please advice for how to create a new folder/directory in hdisk1 instead of the hdisk0? I need to use the folder to store for xmlfiles for my application accessing to read it. Thank a lots. Best Regards, Tom (3 Replies)
Discussion started by: lwy2020
3 Replies

10. UNIX for Dummies Questions & Answers

if...elif...fi condition in Unix

Hey guyes! i have a little problem in if condition, can anybody please solve my problem? Here what i am doing. if then echo "int1 is equal to int2" elif then echo "int1 is greater than int2" else echo "int1 is smaller than int2" fiNo, matter int1 is smaller than... (9 Replies)
Discussion started by: abidmalik
9 Replies
Login or Register to Ask a Question