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 |
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. |
Add at the beginning of your script
Code:
RUNCNTL="" |
You need to export the variable inside of the loop not before it.
|
Why?
Code:
#!/bin/bash Code:
bash-4.1$ ./s.sh |
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 |
Try:
Code:
#!/bin/sh |
Quote:
Code:
c@CW8:/tmp$ foo= |
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= Code:
c@CW8:/tmp$ foo= |
still not working guys!
|
Then it would be helpful to see your latest script, the input file, the command you used to run the script and the output.
|
#!/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 |
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 |
Quote:
|
you guys rock!!! its working!!!..Thanks
|
All times are GMT -5. The time now is 03:21 PM. |