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)
# 8  
Old 07-08-2011
Funksen,

My goal is not the content of Uproc but the content on the same line. Uproc is an identifier (unique of course).

This is the test content of the file to be read :
Code:
uproc1&uproc1_rep_source&rep_source1|rep_source2|rep_source3
uproc2&uproc2_rep_source&rep_source1|rep_source2|rep_source3

---------- Post updated at 11:36 AM ---------- Previous update was at 11:33 AM ----------

@Itkamaraj :

Here is the output (null value):
Code:
+ file=/home/jpassema/purge_/appli
+ rep_cible=
+ IFS=&
+ cat /home/jpassema/purge_/appli
+ read UPROC REP_CIBLE LIST_REP_SOURCE
+ [ uproc1 = uproc1 ]
+ echo repertoire cible =
repertoire cible =
+ 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 =

---------- Post updated at 11:38 AM ---------- Previous update was at 11:36 AM ----------

However when i try to insert an echo at the end of the "if" statement, 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 ]
+ echo repertoire cible =
repertoire cible =
+ let  rep_cible=uproc1_rep_source
+ let  liste_rep_sources=rep_source1|rep_source2|rep_source3
+ echo repertoire cible = 0
repertoire cible = 0
+ read UPROC REP_CIBLE LIST_REP_SOURCE
+ [ uproc2 = uproc1 ]
+ read UPROC REP_CIBLE LIST_REP_SOURCE
+ echo repertoire cible =
repertoire cible =


"repertoire cible" should contain a string, not an integer

Last edited by Franklin52; 07-08-2011 at 02:09 PM.. Reason: Please use code tags for data and code samples
# 9  
Old 07-08-2011
then the problem is reading from the file.

what is your file contents ? ( post some lines )

wherever you compare the values ( do some echo for testing and make sure it gets and got the correct data )

also echo this value $uproc_cible
# 10  
Old 07-08-2011
I have added echos to test the values of uproc_cible (the script input), and UPROC, the line read. Those echos echo the proper values (output below)
Code:
+ uproc_cible=uproc1
+ [ ! -f /home/jpassema/purge_/appli ]
+ [ ! -f /home/jpassema/purge_/uproc ]
+ file=/home/jpassema/purge_/appli
+ echo uproc1
uproc1
+ rep_cible=
+ IFS=&
+ cat /home/jpassema/purge_/appli
+ read UPROC REP_CIBLE LIST_REP_SOURCE
+ echo premiere ligne = uproc1
premiere ligne = uproc1
+ [ uproc1 = uproc1 ]
+ echo repertoire cible =
repertoire cible =
+ let  rep_cible=uproc1_rep_source
+ let  liste_rep_sources=rep_source1|rep_source2|rep_source3
+ echo repertoire cible = 0
repertoire cible = 0
+ read UPROC REP_CIBLE LIST_REP_SOURCE
+ echo premiere ligne = uproc2
premiere ligne = uproc2
+ [ uproc2 = uproc1 ]
+ read UPROC REP_CIBLE LIST_REP_SOURCE
+ echo repertoire cible =
repertoire cible =

---------- Post updated at 02:28 PM ---------- Previous update was at 02:18 PM ----------

and here is the format of my conf file :
Code:
uproc1&uproc1_rep_source&rep_source1|rep_source2|rep_source3

the 3 columns are

1: uproc1 2: uproc1_rep_source
3: rep_source1|rep_source2|rep_source3

Last edited by Franklin52; 07-08-2011 at 02:10 PM.. Reason: Code tags
# 11  
Old 07-08-2011
Using pipe read and set variable globally works only in ksh and zsh.
Code:
grep "^$uproc_cible&" $file | IFS="&" read  uproc rep_cible liste_rep_sources
echo "uproc:$uproc"
echo "rep_cible:$rep_cible"

Generic sh version, works in all about posix sh like ksh, dash, bash, ...
Code:
IFS="&" read  uproc rep_cible liste_rep_sources <<EOF
$(grep "^$uproc_cible&" $file )
EOF
echo "uproc:$uproc"
echo "rep_cible:$rep_cible"

This User Gave Thanks to kshji For This Post:
# 12  
Old 07-11-2011
Hi kshji,

Thanks for the suggestion. It still doesn't work, which tells me that the issue is probably the file in itself or something of that sort.Could it come from the format of the file or perhaps from my bash version : "GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)"
# 13  
Old 07-11-2011
Try this:
Code:
file="/home/jpassema/purge_/appli"

while IFS="&" 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 < $file

echo "repertoire cible = $rep_cible"

# 14  
Old 07-11-2011
Thanks a lot Franklin52,

It seems i was not able to properly read the file. Your code works just fine.
Thanks again!!
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