how to write for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to write for loop
# 1  
Old 10-13-2011
how to write for loop

Hi All,
I have one variable "TABLE_LIST" having the values and each valeus i can run in for loop like below:

TABLE_LIST= abc,abcd,acbfd

i should write the for loop and use each value at a time .

Please help me

Thanking in advance ..............

Regards,
Dathu
# 2  
Old 10-13-2011
You can make it split on "," by changing the special IFS variable:

Code:
VAR="a,b,c,d,e"
OLDIFS="$IFS"
IFS=","
for V in $VAR
do
        echo $V
done

IFS="$OLDIFS"

# 3  
Old 10-13-2011
i didn't understand ur code. Why yor are using OLDIFS.

Can i write code the below:

for EACH_TEBLE in $TABLE_LIST
do
if [[ EACH_TEBLE == "abc"]] then
......
elif [[ EACH_TEBLE == "abcd"]] then
......
else .....
done

The above code is correct?
# 4  
Old 10-13-2011
OLDIFS is used to keep the original IFS, and roll it back after run the script.
# 5  
Old 10-13-2011
Quote:
Originally Posted by pdathu
i didn't understand ur code. Why yor are using OLDIFS.
So you can set the special variable IFS to what it used to be before you changed it. It defaults to any whitespace. Changing it to "," lets you split on "," instead, like the ,'s in your string.

Quote:
Can i write code the below:

for EACH_TEBLE in $TABLE_LIST
do
if [[ EACH_TEBLE == "abc"]] then
......
elif [[ EACH_TEBLE == "abcd"]] then
......
else .....
done

The above code is correct?
It won't work without setting IFS="," first. That's what causes it to split $TABLE_LIST instead of considering it one big string.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write a Boolean variable which succeed and failed inside the if loop in shell script ?

I have if loop with multiple variable value check in if loop. How can i print only if loop satisfied variable and its value in shell script ? I dont want to check each variable in if loop. That makes my script larger. if ] then echo "Only satisfied variable with value" ... (3 Replies)
Discussion started by: prince1987
3 Replies

2. Shell Programming and Scripting

Is it possible to write write multiple cronjobs in shellscript??

Hi All, I need the answer of below question? 1) How to write multiple cronjobs in shellscript? Is there any way or we cant write in shellscript... Regards, Priyanka (2 Replies)
Discussion started by: pspriyanka
2 Replies

3. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

4. UNIX for Dummies Questions & Answers

Unable to write to a file within a loop

Hi All, Following is the program that i have written in cygwin. The redirection of the unfound $param1 to error.txt file doesnt work.Can any one help? #!/usr/bin/sh fname=$1 sed 's/ //g' "$fname" > fname1 while read i do echo $i > file1 #param1 is script name ... (1 Reply)
Discussion started by: janardhanamk
1 Replies

5. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

6. Shell Programming and Scripting

Push records to array during implicit loop and write to file

NEWBIE ALERT! Hi, I'm 1 month into learning Perl and done reading "Minimal Perl" by Tim Maher (which I enjoyed enoumously). I'm not a programmer by profession but want to use Perl to automate various tasks at my job. I have a problem (obviously) and are looking for your much appreciated help.... (0 Replies)
Discussion started by: jospan
0 Replies

7. Shell Programming and Scripting

How to write a If loop..

Hi all.. I've written a script which reads all system backup information. #!/bin/ksh export ORACLE_SID=$1 export primaryhost=$2 export sid=`echo $ORACLE_SID| tr ` RESULTFILE=/oracle/PC9/backupstatus_prod.log ssh -l ora${sid} ${primaryhost} "tail -1... (2 Replies)
Discussion started by: suri.tyson
2 Replies

8. Shell Programming and Scripting

trying to write a script to loop through a port info file

Below is part of a script i have written to loop through part of a port info file. How do i continue the script to get info for OS Device Name, manufacturer and then put information into an array? HBA Port WWN: 10000000c9420b4b OS Device Name: /dev/cfg/c10 Manufacturer: Emulex... (5 Replies)
Discussion started by: rcon1
5 Replies

9. UNIX for Dummies Questions & Answers

How to write if loop in unix

Hi Thanks to one who helped me to create a new thread I am so new to unix and trying to learn the stuff Thanks for all the help I am getting here I wanted to execute if loop like if( x=y || (x=z && x=q)) { some statements } Can I get help how to do write the if loop in unix shell... (5 Replies)
Discussion started by: pinky
5 Replies

10. Shell Programming and Scripting

Loop through file and write out lines to file(s)

Hi, I am new to ksh scripting so your help will be much appreciated. I have a file called file.txt which looks like this Header 20050702 20050703 ABC DEF Header 20050703 20050704 123 456 Header 20050704 20050705 XXX YYY What I am trying to do is write out each of the record... (7 Replies)
Discussion started by: Jtrinh
7 Replies
Login or Register to Ask a Question