LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Break command in Linux (https://www.linuxquestions.org/questions/linux-newbie-8/break-command-in-linux-941716/)

nishanthp82 04-25-2012 02:21 PM

Break command in Linux
 
Hi

cat $DATAFILE | while read LINE
do
REC_ID=`echo $LINE | awk -F: '{print $1}'`
echo "rec_id value is:$REC_ID"
if [ $REC_ID = $1 ]
then
echo "iam in step1"
RUNCNTL=$REC_ID
echo "runcntl is $RUNCNTL"
break
fi
echo "runcntl is $RUNCNTL"

after the break command,the value for RUNCNTL gets lost!!
do you know why? or any other command can i use in place of break command,so value does not lost?

Thanks for reply

Kustom42 04-25-2012 02:25 PM

With for and while loops the variables inside of the them are local to the loop only and are not global. You can export the variable and make it global. Do some searching on exporting variables.
Also please use the '[CODE]' tags for your code.

whizje 04-25-2012 02:29 PM

Add at the beginning of your script
Code:

RUNCNTL=""
export RUNCNTL


Kustom42 04-25-2012 02:30 PM

You need to export the variable inside of the loop not before it.

whizje 04-25-2012 02:57 PM

Why?
Code:

#!/bin/bash
RUNCNTL=""
export RUNCNTL
for ((i=1; i<=100; i++))
do
 RUNCNTL=$i
 if (( i == 50 ))
 then
  break
 fi
done
echo $RUNCNTL

Code:

bash-4.1$ ./s.sh
50


nishanthp82 04-25-2012 04:07 PM

Still not working

#!/bin/sh
RUNCNTL=""
export RUNCNTL
cat $DATAFILE | while read LINE
do
REC_ID=`echo $LINE | awk -F: '{print $1}'`
echo "rec_id value is:$REC_ID"
RUNCNTL=$REC_ID
#echo $RUNCNTL

if (( $REC_ID == $1 ))
#if [ $REC_ID = $1 ]
then
break
fi
done
echo $RUNCNTL
output:
./nishanth1.sh aa_distributed
rec_id value is:aa_distributed

Kustom42 04-25-2012 04:26 PM

Try:

Code:

#!/bin/sh
RUNCNTL=""

cat $DATAFILE | while read LINE
do
REC_ID=`echo $LINE | awk -F: '{print $1}'`
echo "rec_id value is:$REC_ID"
RUNCNTL=$REC_ID
export RUNCNTL
#echo $RUNCNTL

if (( $REC_ID == $1 ))
#if [ $REC_ID = $1 ]
then
break
fi
done
echo $RUNCNTL


catkin 04-25-2012 08:49 PM

Quote:

Originally Posted by Kustom42 (Post 4662947)
With for and while loops the variables inside of the them are local to the loop only and are not global

That is not correct:
Code:

c@CW8:/tmp$ foo=
c@CW8:/tmp$ for ((i=0; i<5; i++)); do foo=bar; break; done
c@CW8:/tmp$ echo $foo
bar


catkin 04-25-2012 08:57 PM

In pipelines the individual commands are run in subshells so any changes made to variables while executing the pipeline are not available to the calling shell:
Code:

c@CW8:/tmp$ foo=
echo 'a
b' | while read line; do echo $line; foo=bar; echo $foo; break; done
a
bar
c@CW8:/tmp$ echo $foo
[no output]

A workaround is to use input redirection into the loop:
Code:

c@CW8:/tmp$ foo=
c@CW8:/tmp$ while read line; do echo $line; foo=bar; echo $foo; break; done < input.txt
a
bar
c@CW8:/tmp$ echo $foo
bar


nishanthp82 04-25-2012 10:20 PM

still not working guys!

catkin 04-25-2012 10:47 PM

Then it would be helpful to see your latest script, the input file, the command you used to run the script and the output.

nishanthp82 04-25-2012 11:11 PM

#!/bin/sh
RUNCNTL=""
export RUNCNTL
BASEDIR=/psoft/batch/fsdev
DATAFILE=$BASEDIR/scripts/alloc_runctl_groups.dat
cat $DATAFILE | while read LINE
REC_ID=`echo $LINE | awk -F: '{print $1}'`
do
RUNCNTL=$REC_ID
if (( $1 == $REC_ID ))
then
break
fi
done
echo $RUNCNTL
----------------------------------------
output:
$ ./nishanth1.sh ca_distributed_allocs(Output is blank)
----------------------------------------------

Input file:
cat alloc_runctl_groups.dat
ca_distributed_allocs:CAEMP/EMaaaEE/RENT/
ca_canada:CACAJJKKLLNADA/CANbbbxxxmmmADA
ca_ou_fte:CAOUFTE/FTEzzaa
ca_ou_alloc_stip:CAOUOOKKJJHSTIP/OU ALIKLAYLOCATIONS STIP-ICP#CDOPPYYGGHHUSTIP/OU AKKLLHBYLLOCATIONS

catkin 04-26-2012 01:53 AM

Of course it does not work like that. All you had to do was change to the solution given in post 9:
Code:

#!/bin/bash
RUNCNTL=""
export RUNCNTL
BASEDIR=/psoft/batch/fsdev
DATAFILE=$BASEDIR/scripts/alloc_runctl_groups.dat
#cat $DATAFILE | while read LINE
while read LINE

REC_ID=`echo $LINE | awk -F: '{print $1}'`
do
    RUNCNTL=$REC_ID
    if (( $1 == $REC_ID )); then
        break
    fi
#done < $DATAFILE
done < $DATAFILE
echo $RUNCNTL


pan64 04-26-2012 02:04 AM

Quote:

Originally Posted by Kustom42 (Post 4662952)
You need to export the variable inside of the loop not before it.

if you use that RUNCNTL only inside the script you do not need to export it.

nishanthp82 04-26-2012 09:04 AM

you guys rock!!! its working!!!..Thanks


All times are GMT -5. The time now is 03:21 PM.