if condition in a while loop

Gurus,

I need to read a line from a file and strip the characters from it and compare the stripped value with the value I pass to the script while executing it. Below is the code for the same. But when i execute the code, it is throwing an error.

#!/bin/ksh

. /home/.i_env

FOLDER_NAME=$1
APP=$2


while read line

   grp=${line%_*}

if [ $APP -eq $grp ]; then

   group=${line%,*}
   perm=${line#*,}

# "doing something here using above variables"   
fi
done < i_groups.txt

Below is the data from i_groups.txt

ADM_GRP,RWX
CTF_DEVS,RWX
CTF_ADM,RWX
CTF_TESTERS,RX
MTF_DEVS,RWX
MTF_ADM,RWX
MTF_TESTERS,RX
LTF_DEVS,RWX
 LTF_ADM,RWX
 LTF_TESTERS,RX
 

When I execute the script, it is giving the below error:

test2.ksh[20]: syntax error at line 46 : `done' unexpected

Please shed some light on this.

-Sam

You forgot something:

#!/bin/ksh

. /home/.i_env

FOLDER_NAME=$1
APP=$2


while read line
do
   grp=${line%_*}

if [ $APP -eq $grp ]; then

   group=${line%,*}
   perm=${line#*,}

# "doing something here using above variables"   
fi
done < i_groups.txt
1 Like

oops. thats a good catch. now the script is giving a different error.

my requirement is to strip out the first 3 characters from the input file and compare it with the value i pass, if it is same, then do something, otherwise look for the next value.

script is working fine if the values in my input file has only one '_' in between (ex: CTF_DEV).

But if there are 2 underscores (ex: CTF_DEV_GRP) then it is not giving expected value.

#!/bin/ksh
 
. /home/.i_env
 
FOLDER_NAME=$1
APP=$2
 
while read line
 do
  grp=$(echo "$line"|sed 's/^\(...\).*/\1/' )
  
  if [ "$APP" == "$grp" ]; then
    group=${line%,*}
    perm=${line#*,}
 
# "doing something here using above variables"   
  fi
done < i_groups.txt
1 Like
if [ "$APP" = "$grp" ]

both of each are has same mean in this example and actually does not effect differ..:wink:

Yep, as well as if there are 2 comas ...

you need to double the % and # sign :

group=${line%%,*}
perm=${line##*,}

I meant it as opposed to the original:

[ $APP -eq $grp ]

which will not work since the values are not numerical.

for not numerical values you are right :b:

APP=$2

while read line
do
   case $line in
     "$APP"*)
        group=${line%,*}
        perm=${line#*,}
        : do something here
        ;;
   esac
done < i_groups.txt
1 Like

Thank you. Its working.

---------- Post updated 11-15-10 at 03:00 PM ---------- Previous update was 11-14-10 at 07:04 PM ----------

Hi ygemini/someone,

Can you explain me how this sed statement works?

grp=$(echo "$line"|sed 's/^\(...\).*/\1/' )

It's an inefficient way of getting the first three characters of "$line".

The pattern between the first pair of slashes is the string to search for; it is replaced by the string after the second slash.

The parentheses delineate the first three characters and \1 selects that string.

It will not do what is wanted if the number of characters before the comma is anything other than 3.

A more efficient (and portable) method uses parameter expansion instead of an external command:

grp=${line%"${line#???}"}
1 Like

I dont agree your comment about inefficient way..
Why do you think such as?
Sed is external command but it has same effect every shells.

svajhala already wants process for only first three chars..

Maybe more portable or maybe not..If we hope to do every jobs from shell , it is not exactly true according to me.

@ygemici, I just ran a speed test of the script against a file with 2560 lines

With grp=${line%"${line#???}"} it took 0.8 seconds

With grp=$(echo "$line"|sed 's/^\(...\).*/\1/' ) it took 51.1 seconds

firstly thanks for tests :b:
yes it is very normal but we dont race in this :slight_smile:
i have been thought only as functionality..