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 :
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
IFS="&" ; grep "^${uproc_cible}" yourfile | read uproc rep_cible liste_rep_sources
this should work, but it's untested
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....
---------- Post updated at 10:36 AM ---------- Previous update was at 10:30 AM ----------
Here is the modification I made following your recommendation :
IFS="&" ; grep "${uproc_cible}" $file | read uproc rep_cible liste_rep_sources
echo $uproc
echo $rep_cible
and this is the output:
+ 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??
Try this..
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"
hm looks good, but since you want to find the content of uproc_cible in the first column, you'll need the
^
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
Hi Itkamaraj,
I have tried your solution, here is the output:
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 =
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"
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 :
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):
+ 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:
+ 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
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
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)
+ 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 :
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
kshji
July 8, 2011, 2:34pm
11
Using pipe read and set variable globally works only in ksh and zsh.
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, ...
IFS="&" read uproc rep_cible liste_rep_sources <<EOF
$(grep "^$uproc_cible&" $file )
EOF
echo "uproc:$uproc"
echo "rep_cible:$rep_cible"
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)"
Try this:
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"
Thanks a lot Franklin52,
It seems i was not able to properly read the file. Your code works just fine.
Thanks again!!