Help in shell script

Hi all,

I am not sure whether i have coded correctly for my request. It would be great if anyone suggest me whether i have coded the if and else part correctly.

Chennai,Banglore -> Execute 1st step need to be executed.
Trichy,Pondichery,salem ->Execute the 2nd step.
Tirupur ->Execute the 3rd step.

Location.txt
Chennai,salem, tirupur,banglore, trichy,pondichery.
#!/bin/sh
for location in $(`cat /home/arun/location.txt`)
do
if [[ $store="chennai" || $store="banglore"]]
then
echo "execute 1st step for   $i";
comand
elif [[ $store ="salem" ||$store ="trichy"  || $store="pondichery" ]]
command
echo "execute 2nd step for  $i";
elif [[ $store ="tiruput" ]]
echo "execute 3rd step for $i";
command
fi
fi
fi
done

---------- Post updated at 09:01 AM ---------- Previous update was at 08:27 AM ----------

Each and every time inputs files be differed and the job will be executed on daily basis

Input : 
chennai
salem 
banglore
trichy 
tripur

Chennai or Banglore -> Specific Oracle function wil be called.
Salem,Trichy,Pondi  -> specific oracle function will be called.
Tirupur                  -> Specific oracle function wiill be called.

I need to read the input files and need location need to be checked and particular oracle function need to be triggered.

you read a variable called location and then use tests on store . Is this the whole script?

You also use ` ... ` inside of $( ... ) . Both are for command substitution. Neither are necessary because ...:

You should not read files with for!

Iterate lists with for . Read files with while read .

while read location; do
  if [[ $location = "this" ]] || [[ $location = "that" ]]; then
    ..
  fi
done < "/home/arun/location.txt"

Lastly, the test command requires whitespace. These are different parameters. There must be a space on either side of = when doing tests, and there must not be any when doing assignments.

1 Like

Oracle function are called for different locations.
Logic conditios

WHEN (LOCATION = 'CHENNAI' OR  LOCATION = 'BANGLORE')
ID:=GET_LOC.HIGHPRIORITY(Location,Sales)
              
WHEN (LOCATION = 'SALEM' OR  LOCATION = 'TRICHY' or LOCATION=Pondichery)

ID:=GET_LOC.MEDIUMPRORITY(Location,Sales)
               (:lOCATION, :SALES);

WHEN (LOCATION = 'TRIUPUR)

ID:=GET_LOC.LOWPRORITY(Location,Sales)
               (:lOCATION, :SALES);

Hello Arun,

Following is an example for same, hope this may help you.
Input_file:

cat function_check
chennai
salem
banglore
trichy
tripur
Punjab
 

Code is as follows for an example.

cat function_check.ksh
chennai_blr () {
                                echo "you are in function of Chennai and Bangalore. here."
               }
Salem_Trichy_Pondi  () {
                                        echo "You are in function of Salem,Trichy,Pondi here."
                       }
Tirupur () {
                echo "you are in function of Tirupur."
           }
while read file
do
        if [[ "$file" == "chennai" || "$file" == "banglore" ]]
        then
                chennai_blr
        elif [[ "$file" == "salem" || "$file" == "trichy" || "$file" == "pondi" ]]
        then
                Salem_Trichy_Pondi
        elif [[ "$file" == "tripur" ]]
        then
                Tirupur
        else
                echo "You are out of chennai, blr, Salem_Trichy_Pondi and Tirupur, seems you are in $file"
        fi
done < "function_check"
 

Output will be as follows.

 ./function_check.ksh
you are in function of Chennai and Bangalore. here.
You are in function of Salem,Trichy,Pondi here.
you are in function of Chennai and Bangalore. here.
You are in function of Salem,Trichy,Pondi here.
you are in function of Tirupur.
You are out of chennai, blr, Salem_Trichy_Pondi and Tirupur, seems you are in Punjab
 

Thanks,
R. Singh

1 Like

Thanks a lot Ravi & Neutron Scott.

From the below script i have called the oracle function for the respective condition through shell script. It would be great if you check an suggest me it is right.

I am not sure whether i have efficiently coded. It would be great if anyone call help me on the below coded script.
Oracle function to be called scenario

Requirement for calling Oracle function for different cities

 
CASE 1: 
(LOCATION = 'chennai'| LOCATION = 'Banglore')
:VALUE:=LOCATION_COUNT.CHENNAI_BANG
               (&LOCATION,&SALES);
case 2: 
(LOCATION ='SALEM'|LOCATION = 'TRICHY' |LOCATION = 'PONDI')
:VALUE:=LOCATION_COUNT.SAL_TRICHY_PONDI
               (&LOCATION,&SALES);
 
case 3:
(LOCATION = 'TIRUPUR')
:VALUE:=LOCATION_COUNT.GET_TIRUP
               (&LOCATION,&SALES);
 
cat function_check.ksh
chennai_blr () {
        VALUE=`sqlplus -S /NOLOG << EOF
        CONNECT dbs/passwd@dbtod
        SET head off
        SET serveroutput on
        select LOCATION_COUNT.CHENNAI_BANG
               (&LOCATION,&SALES) from dual;
        exit;
EOF
               }
 
Salem_Trichy_Pondi  () {
VALUE=`sqlplus -S /NOLOG << EOF
        CONNECT dbs/passwd@dbtod
        SET head off
        select :LOCATION_COUNT.SAL_TRICHY_PONDI
               (&LOCATION,&SALES) from dual;
        exit;
EOF`
 
     }
Tirupur () {
               VALUE=`sqlplus -S /NOLOG << EOF
        CONNECT dbs/passwd@dbtod
        SET head off
        select LOCATION_COUNT.GET_TIRUP
               (&LOCATION,&SALES); from dual;
        exit;
EOF`
           }
 
sales=1000;
while read location
do
        if [[ "$location" == "chennai" || "$location" == "banglore" ]]
        then
                chennai_blr
        elif [[ "$location" == "salem" || "$location" == "trichy" || "$location" == "pondi" ]]
        then
                Salem_Trichy_Pondi
        elif [[ "$location" == "tripur" ]]
        then
                Tirupur
        else
                echo "You are out of chennai, blr, Salem_Trichy_Pondi and Tirupur, seems you are in $file"
        fi
done < "function_check"

---------- Post updated 07-26-15 at 02:19 AM ---------- Previous update was 07-25-15 at 09:03 AM ----------

I have tried myself for calling oracle function for the respective condition.

I am not sure whether it is efficient code. it would be great if anyone call help me out

Perhaps you could write it more like something like this (untested) :

get_sales_info () {
  loc_code=$1
  sqlplus -S /NOLOG << EOF
  CONNECT dbs/passwd@dbtod
  SET head off
  select LOCATION_COUNT.$loc_code
     (&LOCATION,&SALES) from dual;
  exit;
EOF
}

while read location
do
  case $location in
    chennai|banglore)
      get_sales_info CHENNAI_BANG ;;
    salem|trichy|pondi)
      get_sales_info SAL_TRICHY_PONDI ;;
    tripur)
      get_sales_info TIRUP ;;
    *)
      echo "You are out of chennai, blr, Salem_Trichy_Pondi and Tirupur, seems you are in $location" ;;
  esac
done < "function_check"
1 Like

Hi Scrutinzer,

Thanks a lot for your response.

I will test it from my end.