if condition in a while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if condition in a while loop
# 8  
Old 11-13-2010
Quote:
Originally Posted by ygemici
both of each are has same mean in this example and actually does not effect differ..Smilie
I meant it as opposed to the original:
Code:
[ $APP -eq $grp ]

which will not work since the values are not numerical.
# 9  
Old 11-13-2010
Quote:
Originally Posted by Scrutinizer
I meant it as opposed to the original:
Code:
[ $APP -eq $grp ]

which will not work since the values are not numerical.
for not numerical values you are right Smilie
# 10  
Old 11-14-2010

Code:
APP=$2

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


Last edited by cfajohnson; 11-14-2010 at 08:27 PM..
This User Gave Thanks to cfajohnson For This Post:
# 11  
Old 11-15-2010
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?

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

# 12  
Old 11-15-2010
Quote:
Originally Posted by svajhala
ygemini/someone,

Can you explain me how this sed statement works?

Code:
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:
Code:
grp=${line%"${line#???}"}

This User Gave Thanks to cfajohnson For This Post:
# 13  
Old 11-16-2010
Quote:
Originally Posted by cfajohnson
It's an inefficient way of getting the first three characters of "$line".

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:
Code:
grp=${line%"${line#???}"}

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.
# 14  
Old 11-16-2010
@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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop/while condition - doubt

hi., As we know that using for-loop or while condition, we can only process one by one sequentially, but , lets say this example : 1. under the folder "logs" there are 1000 files 2. each file has one record or line 3. have to perform atleast 7 queries per 3 seconds ,for instance ... (3 Replies)
Discussion started by: alnhk
3 Replies

2. Shell Programming and Scripting

Check two condition in while loop

Hi, I Have to check two condition in while loop every 2 minutes. while loop is accompanied with number of times it will check.Please help in putting the two condition in while loop as appropriate. z= input value, A=1 while do 1.check the file output,if the file output is N then keep on... (2 Replies)
Discussion started by: netdbaind
2 Replies

3. Shell Programming and Scripting

While Loop with if else condition

Hi, I was trying to write a shell script which reads csv file and sends mail in html format along with tables. Hope i have completed 1st part , but while sending mail i was trying to highlight some rows in the table based on the egrep outcome. If the string exists in line/INPUT, i am trying to... (4 Replies)
Discussion started by: varmas424
4 Replies

4. Shell Programming and Scripting

Case loop condition

hello, I would like to do exit at the end ie list all errors before exiting How to put the token exit in a variable with a loop ? Thanks function g1 () { case "$1" in (-0-0 | -0-1 | -0-2 | -0-3 | -1-0 | -1-1 | -1-2 | -1-3) # nothing, OK ! ;; (*) echo 'Fatal, $1 = '"'$1'"', Date... (9 Replies)
Discussion started by: amazigh42
9 Replies

5. Shell Programming and Scripting

Leaving for loop on condition

hi all, i have a problem...no dream :-) i want to scan a file i use the loop famous while read line do do < myfile but this scan must finish when find the another string . How can i do it? best regards for all. Francesco Please use descriptive subjects. "script." doesn't tell... (5 Replies)
Discussion started by: FrancescoIt
5 Replies

6. Shell Programming and Scripting

Use of -z in while loop condition

Hi, Could you please tell what is the meaning of -z in while loop condition. For example, while ; do echo "*** Enter the age " readage (3 Replies)
Discussion started by: vidyaj
3 Replies

7. Shell Programming and Scripting

issues with a condition in a while loop

Hi, I am facing issues with test condition. I had a compound condition to write for both if and while, In one of the texts i referred, with a korn shell we can write compound statements like: ], however this doesn't worked for me. For example: if ] doesn't works, but if || worked. ... (1 Reply)
Discussion started by: amritps
1 Replies

8. Shell Programming and Scripting

how can i put the condition in for loop for the below.

i have the equation like below 07:35:07 ( AB : 2319f.ab * 22) + ( AB : 2320f.ab * 22.03 ) + ( AB :2321f.ab * 22.07 ) ...... N i want put ":" as a delimiter and break the equation like below 2319f.ab * 22 2320f.ab *22.03 2321f.ab * 22.07 . . N i know the number of... (1 Reply)
Discussion started by: mail2sant
1 Replies

9. UNIX for Dummies Questions & Answers

Testing For Loop condition

How do I test the return condition in the script if no files are found: for file in `Find ${LANDING_FILE_DIR}${BTIME_FILENAME_PATTERN}` do ... .. done I want to capture the return code so I can echo the error or condition. Using if ] always returns zero no matter where it's placed. ... (4 Replies)
Discussion started by: mavsman
4 Replies

10. UNIX for Dummies Questions & Answers

What condition to be put in the while loop?

i have got a file where the env command is appended 5 times. i have to now look for the username and display it in the form of 1) PWD=/home/lee.ballancore 2) USER=lee.ballancore 3) MAIL=/var/spool/mail/lee.ballancore 4) LOGNAME=lee.ballancore 5) HOME=/home/lee.ballancore 6)... (1 Reply)
Discussion started by: nehaquick
1 Replies
Login or Register to Ask a Question