Value not getting printed outside loop

Hi all I'm using below code

#!/bin/bash
export fileclob
cd /home/appsuser/dataload
file='EG.mdd'
chmod 777 $file
dos2unix -ascii -k -q -o  $file $file
sed -e '${/^$/d}' $file
cat $file | while read LINE
do
  echo "line is"
  if [ -n $LINE ]
  then
  echo "line is $LINE"
  echo " "
  Y=${#fileclob}
  if [ $Y = 0 ]
  then
    fileclob=$LINE
     echo  "fileclob_1 $filecloob"
  else
    fileclob="${fileclob},${LINE}"
    echo  "fileclob_2  $fileclob"
  fi
 fi
done

echo "${fileclob} "
sqlplus apps/apps <<EOF
set pagesize 0 feedback off verify off heading off echo off serveroutput on
insert into lshadmin.global_table (file_name,study_name,table_struct)
  values('EG','MK898','$fileclob);
commit;
exit;
EOF

when I try to print the value ${fileclob} outside the loop it is coming null but within the loop it is printing correctly.
There is no blank line at end of file.

Pls help.

Use:

while read LINE
do
  echo "line is"
  if [ -n $LINE ]
  then
  echo "line is $LINE"
  echo " "
  Y=${#fileclob}
  if [ $Y = 0 ]
  then
    fileclob=$LINE
     echo  "fileclob_1 $filecloob"
  else
    fileclob="${fileclob},${LINE}"
    echo  "fileclob_2  $fileclob"
  fi
 fi
done<$file

Instead of:

cat $file | while read LINE
do
  echo "line is"
  if [ -n $LINE ]
  then
  echo "line is $LINE"
  echo " "
  Y=${#fileclob}
  if [ $Y = 0 ]
  then
    fileclob=$LINE
     echo  "fileclob_1 $filecloob"
  else
    fileclob="${fileclob},${LINE}"
    echo  "fileclob_2  $fileclob"
  fi
 fi
done

do not pass '$file' through 'cat', change the while block to below and it will work

while read LINE
do
.
.
.
done < $file

Below is the test

$ echo ${var1}
$ cat sam | while read line
> do
> var1=$line
> done
$ echo ${var1}
$ echo ${var2}
$ while read line
> do
> var2=$line
> done < sam
$ echo ${var2}
X2 L3 G1 3 6 9
$

I changed it to:

#!/bin/bash
export fileclob
cd /home/appsuser/dataload
file='EG.mdd'
chmod 777 $file
dos2unix -ascii -k -q -o  $file $file
sed -e '${/^$/d}' $file
 while read LINE
do
  if [ -n $LINE ]
  then
  echo "line is $LINE"
  echo " "
  Y=${#fileclob}
  if [ $Y = 0 ]
  then
    fileclob=$LINE
     echo  "fileclob_1 $fileclob"
  else
    fileclob="${fileclob},${LINE}"
    echo  "fileclob_2  $fileclob"
  fi
 fi
done < $file
echo "file is ${fileclob} "
sqlplus apps/apps <<EOF
set pagesize 0 feedback off verify off heading off echo off serveroutput on
insert into lshadmin.global_table (file_name,study_name,table_struct)
  values('EG','MK898','$file');
commit;
exit;
EOF

echo "file is ${fileclob} " whole statement is not visible after executing the script

try

for LINE in $(cat $file)

instead of

cat $file | while read LINE

in your orignal script

its concatenating and printing the values within the loop but ouside the loop same variable becomes null :frowning:

for the below statement in your code, what output do you see

echo "file is ${fileclob} "

nothing... it just displays the line within loop

r u sure?can you show your updated script?Its working my end.

#!/bin/bash
export fileclob
cd /home/appsuser/dataload
file='EG.mdd'
chmod 777 $file
dos2unix -ascii -k -q -o  $file $file
sed -e '${/^$/d}' $file
for LINE in $(cat $file)
do
  echo "line is"
  if [ -n $LINE ]
  then
  echo "line is $LINE"
  echo " "
  Y=${#fileclob}
  if [ $Y = 0 ]
  then
    fileclob=$LINE
     echo  "fileclob_1 $filecloob"
  else
    fileclob="${fileclob},${LINE}"
    echo  "fileclob_2  $fileclob"
  fi
 fi
done
echo "${fileclob} "

output

line is
line is Hello
fileclob_1
line is
line is Bye
fileclob_2  Hello,Bye
line is
line is Hi
fileclob_2  Hello,Bye,Hi
Hello,Bye,Hi

show us ED.mdd as well

1 Like

If nothing is printed of the statement, echo statement is being ignored...need to look into the other part of the code
by the way, what do you want to achive through the while loop?

1 Like

It is workin now :slight_smile: