For Loop with Strings as parameters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For Loop with Strings as parameters
# 1  
Old 06-23-2006
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  
Old 06-23-2006
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 Smilie . 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  
Old 06-23-2006
Thanks for looking at this Doc_RuNNeR, but I don't understand your solution
# 4  
Old 06-23-2006
Try:
Code:
for PARAM in "${a}" "${b}"

# 5  
Old 06-23-2006
#! /bin/ksh

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

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

Try that script
# 6  
Old 06-26-2006
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  
Old 10-06-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use positional parameters in loop / while syntax in whiptail

I like to “optimize” / make more like a real program my bash script by replacing repetitious code which utilizes positional parameters. I am having two issues I cannot solve and would appreciate some assistance with resolving them. a) how to modify the whiptail checklist... (3 Replies)
Discussion started by: annacreek
3 Replies

2. UNIX for Dummies Questions & Answers

Concatenate strings in a a for loop

hi guys, I have this question. I am creating an script to that read a text file(.ini) with the list of the patterns to find for example: EPMS_VO EMPS_PARTS Then it check if file have been delivered in a folder and process it with each pattern, but I am having problems concatenting the... (7 Replies)
Discussion started by: Danman
7 Replies

3. Shell Programming and Scripting

Loop to read parameters and stop

Hey guys, How do I make a loop that reads all the parameters en then stop when there are no parameters anymore ? Something that gives an output like this: ./Script.sh parameter1 parameter2 parameter3 parameter = parameter1 parameter = parameter2 parameter = parameter3 Thanks a lot,... (5 Replies)
Discussion started by: Miki1579
5 Replies

4. UNIX for Dummies Questions & Answers

Trouble displaying parameters passed into a for loop

#!/bin/bash function check_num_args() { if ; then echo "Please provide a file name" else treat_as_file $* fi } function treat_as_file() { numFiles=$# for((i=1;i<=$numFiles;i++));do echo $i ... (3 Replies)
Discussion started by: kikilahooch
3 Replies

5. Shell Programming and Scripting

Strings to integers in an arithmetic loop

Hi all, Could someone please advise what is the correct syntax for my little script to process a table of values? The table is as follows: 0.002432 20.827656 0.006432 23.120364 0.010432 25.914184 0.014432 20.442655 0.018432 20.015243 0.022432 21.579517 0.026432 18.886874... (9 Replies)
Discussion started by: euval
9 Replies

6. Shell Programming and Scripting

Unix Shell Script to loop over Directory and send Filesname as parameters

Hi there I'm new to UNIX scripting; I’m stuck with the following I have an Oracle SQL script that takes three parameters 1- File Name 2- File Path 3- File creation date Under UNIX I have a folder where files will be placed frequently and I need to upload those files to Oracle, what I need... (3 Replies)
Discussion started by: windjashi
3 Replies

7. Shell Programming and Scripting

bash if loop for checking multiple parameters

Hello, I've got next problem: I want to examine at the beginning of a script in an if loop that: 1. Is there 4 parameters given 2. If first state is true then: is there switches -e and -d? 3. At the end, how can i indentify them as variebles regardlees to its order. I was thinking like... (2 Replies)
Discussion started by: szittyafergeteg
2 Replies

8. Shell Programming and Scripting

for loop logic with multiple parameters

hi, unix wizards, i have a question about the logic of my inner for loop below. first, what i am trying to do is to write a script called create_account that automatically creates mysql accounts. the user can provide a user_name or a group_id as an argument (and the script can take multiple... (1 Reply)
Discussion started by: ankimo
1 Replies

9. Shell Programming and Scripting

Parameters in loop

Hi, I am trying to write a script which will read inputs form user and process those files, I have issue reading the input parameters in a loop. Following is the script... I run the script as ./Script.sh 3 table1 table 2 table3 NumberOfTables=$1 let TableCount=1 while do ... (3 Replies)
Discussion started by: mgirinath
3 Replies

10. Shell Programming and Scripting

Urgent:Comparing two Strings using If Loop

Hi All, Please help me out in this... I am new to scripting How to compare two strings by using the same string in single loop, I am using ksh for ex:see the code snippet below I am writing in java, Can u guys tell me that in scripting if ("string1"=="string2" ||... (2 Replies)
Discussion started by: Anji
2 Replies
Login or Register to Ask a Question