Operating System - HP-UX
1837941 Members
2192 Online
110124 Solutions
New Discussion

Re: Shell Script & variable / file name substitutions..How?

 
Declan Mc Kay
Occasional Advisor

Shell Script & variable / file name substitutions..How?

Hi,

I am trying to get a script to auto email a copy of a log file. However part of the logfile name is unknown untill I do a positional grep from another file...Thats all fine but problem is when I want to cat the new file name as follows:
cat $ErrMsg | cut '-c 40-45' > $TransNum
$TransLog='/u/ai/logs/pr.tr$TransNum.log'
cat $TransLog

However script when run returns the error>
./almedi_ai.scr[32]: =/u/ai/logs/pr.tr$TransNum.log: not found.

when I echo $TransNum (which is part of the actual file name I want printed) within the script to screen it returns the correct value..?
Appreciate any help on this.

8 REPLIES 8
Mark Grant
Honored Contributor

Re: Shell Script & variable / file name substitutions..How?

Either remove the quites completely or replace th ' with " on this line.

$TransLog='/u/ai/logs/pr.tr$TransNum.log'

Variables do not get interpreted within ' marks.
Never preceed any demonstration with anything more predictive than "watch this"
Jean-Louis Phelix
Honored Contributor

Re: Shell Script & variable / file name substitutions..How?

hi,

In this case you should use " instead of ' to allowd variable substitution, and no $ for variable assignation :

TransLog="/u/ai/logs/pr.tr$TransNum.log"

Regards.
It works for me (© Bill McNAMARA ...)
RAC_1
Honored Contributor

Re: Shell Script & variable / file name substitutions..How?

TransNum=`cat $ErrMsg | cut '-c 40-45'`
export TransNum
TransLog="/u/ai/logs/pr.tr${TransNum}.log"
cat $TransLog

There is no substitute to HARDWORK
John Jayaseelan
Super Advisor

Re: Shell Script & variable / file name substitutions..How?

Try this
$TransLog='u/ai/logs/pr.tr'$TransNum'.log'

G. Vrijhoeven
Honored Contributor

Re: Shell Script & variable / file name substitutions..How?

Hi,

I think you need to set the $TransNum
var first.

TransNum=`cat $ErrMsg | cut '-c 40-45'`

Gideon
RAC_1
Honored Contributor

Re: Shell Script & variable / file name substitutions..How?

Read export TransNum as export $TransNum
There is no substitute to HARDWORK
john korterman
Honored Contributor

Re: Shell Script & variable / file name substitutions..How?

Hi,
I think you already have the solution, as pointed out by RAC, change the line
$TransLog='/u/ai/logs/pr.tr$TransNum.log'
to
TransLog="/u/ai/logs/pr.tr${TransNum}.log"

because:
a) you have to specify by curly braces where the variable begins and ends since it is not limited by a space.
b) you have to use double quotes around your variable assignment, as a dollar sign cannot be expanded inside single qoutes; try e,g, the difference between echo "$x" and echo '$x'
c) a dollar sign expands a variable, but when you assign content to a variable, you must use its name, hence
TransLog=

I hope it works; if not I am aware that my explanation looks ridiculous!

regards,
John K.
it would be nice if you always got a second chance
Michael Schulte zur Sur
Honored Contributor

Re: Shell Script & variable / file name substitutions..How?

Hi,

in case the file you grep has more than one line to grep.

while read LINE
do
TransNum=`echo ${LINE} | cut -c40-45`
TransLog="/u/ai/logs/pr.tr${TransNum}.log"
cat ${TransLog}
done < ${ErrMsg}

greetings,

Michael