Sponsored Content
Top Forums UNIX for Advanced & Expert Users How to create folder by using if and elif condition? Post 302966342 by Don Cragun on Thursday 11th of February 2016 02:52:43 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 05:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy