Setting a variable in a while loop (.ksh script)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Setting a variable in a while loop (.ksh script)
# 1  
Old 07-08-2011
Setting a variable in a while loop (.ksh script)

Hello Everyone,

I'm still trying to grasp many concepts in .ksh scripting, one of them being variables inside loops. My problem is the following:

* I'm trying to set a variable inside a while read loop to reuse it outside of said loop. My lines are the following :
Code:
file="/home/jpassema/purge_/appli"

IFS="&"
cat $file | while read UPROC REP_CIBLE LIST_REP_SOURCE
do
        if [ "$UPROC" = "$uproc_cible" ]; then
                (( rep_cible=$REP_CIBLE ))
                (( liste_rep_sources=$LIST_REP_SOURCE ))
        fi
done

echo "repertoire cible = $rep_cible"

basically i'm trying to read a file until i find the line designated by the value "uproc_cible" and setting the contents of the colums on that line to script-wide variables.

I believe that loops run as parrallel processes, which may be the source of my problem.

Thanks in advance for any pointers

Jimmy

Last edited by Franklin52; 07-08-2011 at 02:06 PM.. Reason: Please use code tags
# 2  
Old 07-08-2011
Code:
IFS="&" ; grep "^${uproc_cible}" yourfile | read  uproc rep_cible liste_rep_sources

this should work, but it's untested
# 3  
Old 07-08-2011
Hi Funksen,

Thanks for your prompt reply.
As i undestand it, this command will find the line identified by "uproc_cible" and set the variables : uproc, rep_cible, liste_rep_sources with the values in that line.

stop me if i'm wrong....Smilie

---------- Post updated at 10:36 AM ---------- Previous update was at 10:30 AM ----------

Here is the modification I made following your recommendation :

Code:
IFS="&" ; grep "${uproc_cible}" $file | read  uproc rep_cible liste_rep_sources

echo $uproc
echo $rep_cible

and this is the output:

Code:
+ file=/home/jpassema/purge_/appli
+ IFS=&
+ grep uproc1 /home/jpassema/purge_/appli
+ read uproc rep_cible liste_rep_sources
+ echo

+ echo

perhaps i goofed up the syntax??

Last edited by Franklin52; 07-08-2011 at 02:07 PM.. Reason: Please use code tags
# 4  
Old 07-08-2011
Try this..

Code:
 
file="/home/jpassema/purge_/appli"
rep_cible=""
IFS="&"
cat $file | while read UPROC REP_CIBLE LIST_REP_SOURCE
do
if [ "$UPROC" = "$uproc_cible" ]; then
(( rep_cible=$REP_CIBLE ))
(( liste_rep_sources=$LIST_REP_SOURCE ))
fi
done
echo "repertoire cible = $rep_cible"

# 5  
Old 07-08-2011
hm looks good, but since you want to find the content of uproc_cible in the first column, you'll need the
Code:
^

in the grep, this indicates the beginning of the line

but this has nothing to do with your problem, since the variables should contain anything

the read syntax will not work in sh or bash, just in ksh

maybe give a short example of the content of uproc_cible and the file


possible issue could be, that the search string cannot be found in the file, case problem perhaps? use grep -i to ignore case
# 6  
Old 07-08-2011
Hi Itkamaraj,

I have tried your solution, here is the output:
Code:
file=/home/jpassema/purge_/appli
+ rep_cible=
+ IFS=&
+ cat /home/jpassema/purge_/appli
+ read UPROC REP_CIBLE LIST_REP_SOURCE
+ [ uproc1 = uproc1 ]
+ let  rep_cible=uproc1_rep_source
+ let  liste_rep_sources=rep_source1|rep_source2|rep_source3
+ read UPROC REP_CIBLE LIST_REP_SOURCE
+ [ uproc2 = uproc1 ]
+ read UPROC REP_CIBLE LIST_REP_SOURCE
+ echo repertoire cible =
repertoire cible =


Last edited by Franklin52; 07-08-2011 at 02:08 PM.. Reason: Code tags
# 7  
Old 07-08-2011
Code:
 
file="/home/jpassema/purge_/appli"
rep_cible=""
IFS="&"
cat $file | while read UPROC REP_CIBLE LIST_REP_SOURCE
do
if [ "$UPROC" = "$uproc_cible" ]; then
echo $REP_CIBLE   # to check the value
rep_cible=$REP_CIBLE
liste_rep_sources=$LIST_REP_SOURCE
fi
done

echo "repertoire cible = $rep_cible"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with loop in ksh script

Hi, I am new to UNIX. I am working on a script where it takes the input and produces a desired output and it works fine for one instance. Input(One Instance): CREATE TABLE TAB1 ( COL1, COL2 ); CREATE UNIQUE INDEX XPKTAB1 ( COL1 )TAB1; Output: CREATE TABLE TAB1 ( COL1, COL2... (8 Replies)
Discussion started by: varun2327
8 Replies

2. Shell Programming and Scripting

Setting a variable using variables in a loop

Hi, I am having a bit of trouble with the below code: file=/path/to/file for i in 03 06 07 21; do if ; then eval count$i=`grep -c word $file-$i` fi done Totalcount=0 for i in 03 06 07 21; do if ; then echo $count$i variable not exist; else Tcount=`expr $Tcount + $count$i`; fi... (3 Replies)
Discussion started by: brunlea
3 Replies

3. Shell Programming and Scripting

Setting up env variable in ksh

I am facing a very strange issue. I have script in ksh with #!/bin/ksh as shebang. This script has function which sets the env variable before running other functions of the script. by set_up_env() { CONFIG_FILE="/opt/app/tools/deepmarking/latestVersion/script/UploadEnv" if then ... (7 Replies)
Discussion started by: Tuxidow
7 Replies

4. Shell Programming and Scripting

Setting a TZ variable in a script

Hello all, I know this must be simple .... but i can't grasp what could be the issue. I'm trying to setup the timezone variable (to the unix command date) according to what i find in a value that i got from parsing the config file. The end result would be setting the log file with this new... (4 Replies)
Discussion started by: maverick72
4 Replies

5. Shell Programming and Scripting

setting a shell script variable in awk

The following is part of a larger shell script grep -v "Col1" my_test.log | grep -v "-" | awk '$5 == "Y" {print $1}' instead of printing, can I set set $1 to a variable that the rest of the shell script can read? if $5 == Y, I want to call another shell script and pass $1 as a... (2 Replies)
Discussion started by: guessingo
2 Replies

6. Shell Programming and Scripting

Setting environment variable using shell script

Hi All, I'm trying to write an menu driven program to automate some functions which involve loging to multiple hosts. The hosts can differ for every use, so I thought I would use an config file to get the hostnames. Now I need to set those values in the config file to environment variable to... (6 Replies)
Discussion started by: arun_maffy
6 Replies

7. Shell Programming and Scripting

Setting Variable in TCL to be understood by KSH Shell

Hi , I am having one TCL TK script , I am setting some variables in the GUI TK interface set DI 1 set MODELS_PATH /a/d/path but I want to make ksh shell understand the variables when I am running TK script I tried to do like this set a but it is not working ... (1 Reply)
Discussion started by: kshitij
1 Replies

8. Shell Programming and Scripting

setting ksh environmental variable

Hi, I have problem setting up environmental variables. The idea is to start with main.ksh script that will run setting.ksh, and in side of it I'll set up variables. Please take a look at my code, and help me to find my mistake. Thanks, Mila Main.ksh look like this: #!/usr/bin/ksh #... (2 Replies)
Discussion started by: mefquik
2 Replies

9. Shell Programming and Scripting

using awk in a for loop (getting ksh variable)

Hi, I've tried searching the forums for a case similar to mine but was unsuccessful. This is my first time to use awk so any help would be really appreciated :) I have one file containing data for with the first value in each row being a State Name. I would need to create a separate file... (1 Reply)
Discussion started by: karver
1 Replies

10. UNIX for Dummies Questions & Answers

setting a global variable in script

Hi All, I know to set global variable i can use export .. But take the situation like below .. I want to set a variable in one script and access that in second script i have done like this .. It is not working one.sh #!/usr/bin/ksh echo $RISSHI export RISSHI=1 two.sh... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies
Login or Register to Ask a Question