user2966307
user2966307

Reputation: 23

How can I assign a value from a file to a variable in UNIX sh shell?

I have been scouring this site trying to find an answer to this question and came across several really good answers, unfortunately, none of them have worked for me.

Here is the script that I am using:

VALUE=`cat szpfxct.tmp`
export VALUE
echo " "                >>$LGFILE
echo "term code " $VALUE >>$LGFILE

When opening the tmp file, I see a single string (201510) which should be assigned to VALUE. However, in the log file, it echo's "term code " showing that $VALUE is blank.

The permissions for the file are ok: -rw-rw-rw- 1 oracle dba 7 Oct 26 21:30 szpfxct.tmp. And the commands run fine when executed individually from the command line.

Here is the full script:

#!/usr/bin/sh

######################################
# Create Log File
######################################

LGFILE=$HOME/szpfxct_$ONE_UP.log
export LGFILE

echo "*********************************" >$LGFILE
echo "Start SZPFXCT Fix Enrollment Counts   " >>$LGFILE
echo "*********************************" >>$LGFILE
echo "    " >>$LGFILE
echo "starting szpfxct" `date` >> $LGFILE

echo $BANUID    >>$LGFILE
echo $PSWD
echo $ONE_UP >>$LGFILE

#####################################
# Retrieve Parameter
#####################################

sqlplus $UIPW @$BANNER_LINKS/szpfxct.sql $ONE_UP 1>> $LOG 2>&1
sqlplus baninst1/[password] <<ENDsql
set echo off
set feedback off
set verify off
set timing off
set showmode off
set time off
set pagesize 60
set serveroutput on size 1000000;

clear breaks
clear computes
ttitle off

DECLARE

    output_file       utl_file.file_type;
    term_code         stvterm.stvterm_code%TYPE;

BEGIN
   output_file := utl_file.fopen('/export/home/jobsub','szpfxct.tmp','w');
    SELECT substr(gjbprun_value,1,6)
      into term_code
      from gjbprun
     where gjbprun_job = 'SZPFXCT' and
           gjbprun_number = '01' and
           gjbprun_one_up_no = $ONE_UP;

   utl_file.put_line(output_file, term_code);

   utl_file.fclose_all;
END;
/
ENDsql
#TPS
chmod 777 szpfxct.tmp
VALUE=`cat szpfxct.tmp`
export VALUE
echo " "                >>$LGFILE
echo "term code " $VALUE >>$LGFILE

Upvotes: 2

Views: 2557

Answers (3)

havmaage
havmaage

Reputation: 135

to source a file with sh ( bourne shell )

. /path-to-file/filename 

It should work

Upvotes: 0

cpu
cpu

Reputation: 567

Try this. It will possibly give you an idea of what problem you have:

VALUE="`cat szpfxct.tmp 2>&1`"
echo "term code '$VALUE'"
term code  'cat: szpfxct.tmp: No such file or directory'

In this example, I forced the file not to exist. Also, if the variable is truly empty, you will realize it because there will be single quotes on each sides. If it is just spaces, there will be one or more spaces in between the quotes.

I suggest this because in some scripts, the stderr might have been sent somewhere where you do not see it anymore. If all there is in your script is these few lines you showed, then I can't explain why you would not have seen the stderr output. Also, what might help is to let us knwo which shell interpreter you are using. I tried sh and bash but I could not replicate your issue. That is why I am hoping this change will provide more information. If it doesn't, try this too:

VALUE="`pwd ; cat szpfxct.tmp 2>&1`"

Maybe you are not in the directory you expect to be in, and that directory also has such a file, but it's empty.

Upvotes: 2

havmaage
havmaage

Reputation: 135

Try source your file lets say that the value of szpfxct.tmp is

VALUE=100

in your shell scriot you could do something like this

#!/usr/bin/bash
#either 
source szpfxct.tmp
#or
. .szpfxct.tmp
echo $VALUE

Upvotes: 0

Related Questions