The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Parameters in loop mgirinath Shell Programming and Scripting 3 03-18-2008 10:20 AM
Urgent:Comparing two Strings using If Loop Anji Shell Programming and Scripting 2 01-09-2008 08:54 AM
Read from a file and use the strings in a loop xboxer21 Shell Programming and Scripting 3 04-18-2006 03:30 PM
How to concatenate two strings or several strings into one string in B-shell? fontana Shell Programming and Scripting 2 08-26-2005 11:58 AM
how to get the similar function in while loop or for loop trynew Shell Programming and Scripting 3 06-17-2002 11:09 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-23-2006
kadishmj kadishmj is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 11
For Loop with Strings as parameters

I'm trying to create a script with a for loop that take strings in as the parameters. Some of the strings have spaces in them, and I can't get it to work properly. For example:

Code:
#!/bin/ksh

INFILE=snapshot.log
OUTFILE=summary.out

a="Lock waits"
b="Deadlocks detected"

for PARAM in ${a} ${b}
do
   /usr/bin/grep ${PARAM} ${INFILE} >> ${OUTFILE}
done
I want the loop to execute 2 times, but it is executing 4 times with the following values for PARAM:

1st pass - Lock
2nd pass - waits
3rd pass - Deadlocks
4th pass - detected

I've tried placing the strings in single and double quotes, but that doesn't seem to help. Any ideas?
  #2 (permalink)  
Old 06-23-2006
Doc_RuNNeR Doc_RuNNeR is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 17
I donīt know a lot of english and i will to try to explain it. If you have more questions you can ask again . Ok the system has a variable, that for loop uses to know how many parameters has the file for example a text. The variable has space, tabs and return (of course all convinations of that).

THE ANSWER

$cat > example
spaces=IFS #IFS is the system variable
IFS=" #We inicializating IFS=return to tae all the line
"
........................
........................
IFS=$spaces #We used the var spaces to give the value to IFS againwhen we finish
unset spaces

Bye
  #3 (permalink)  
Old 06-23-2006
kadishmj kadishmj is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 11
Thanks for looking at this Doc_RuNNeR, but I don't understand your solution
  #4 (permalink)  
Old 06-23-2006
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
  
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
Try:
Code:
for PARAM in "${a}" "${b}"
  #5 (permalink)  
Old 06-23-2006
Doc_RuNNeR Doc_RuNNeR is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 17
#! /bin/ksh

spaces=IFS
IFS="
"
for i in "hello world"
do
echo $i
done
IFS=$spaces

-----------------

Try that script
  #6 (permalink)  
Old 06-26-2006
kadishmj kadishmj is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 11
Thanks for everyone's suggestions. I finally got it working:

Code:
OUTFILE=summary.out
a='Deadlocks detected'
b='Lock escalations'

for PARAM in "${a}" "${b}"
do

 echo ${PARAM}

 COUNT=1

   while ((COUNT < 41))
   do
      INFILE=DB2Snapshot_${1}_${COUNT}.log
      /usr/bin/grep "${PARAM}" ${INFILE} >> ${OUTFILE}

   ((COUNT=COUNT+1))
   done
done
  #7 (permalink)  
Old 10-06-2008
er_aparna er_aparna is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 52
getting spaces while reading file from for loop

Hi ,

I am getting similar problem,
but in my case i am fetching variable value from a file , and then echo the value, but its considering spaces in the file. for exmp:

file - temp1 has contents in quotes
"group name used to communicate with x service"
"group name used to communicate with y service"

i used for loop :
for var in `cat "temp1"`
do
echo "DESC=$var;"
done


the desired output is
DESC="group name used to communicate with x service";
DESC="group name used to communicate with y service";

however, the output coming is
DESC="group";
DESC="name";

its reading spaces also...

Pls suggest...............

Thanks in advance
Aparna
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 09:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0