Operating System - HP-UX
1839415 Members
3669 Online
110145 Solutions
New Discussion

Problem with Korn Shell Script

 

Problem with Korn Shell Script

Hello All:

I need your help again. Any suggestions will be much appreciated.

The script below used to work up until a week ago. The only change to the environment is a new version of Server Express (cobol compiler) was installed. I didn't think this would affect anything though. The problem appears in the "rm" logic.

This script rips through a directory looking for all programs that begin with "i" and end in ".pco". It removes all the compile-generated files and recreates them with the compile process. The original files are: ioparm.pco, ioparm2.pco, ioparm3.pco, ioparm4.pco, and ioparm5.pco. One one system it processes the files in the order ioparm, ioparm2, ioparm3, then 4, then 5. On Another it reverses the order and when it comes to the ioparm program, not only does it delete the ioparm files, it delete the files generated for ioparm2-3-4-5. Can you think of a reason why it is processing the files in a different order now? Thank you in advance.

#!/bin/ksh
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# Compile all embedded SQL programs.
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
echo ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"|tee -a compile.log
echo " COMPILATION OF COBOL/SQL SOURCE CODE"|tee -a compile.log
echo ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"|tee -a compile.log
echo `date`|tee -a compile.log
cp ascii.dir cobol.dir
export _rtncode=`echo $?`
case $_rtncode in
0) ;;
*) echo Setup for compilation of embedded SQL programs ERROR!!!!!!!!|tee -a compile.log ;
echo SCRIPT TERMINATED ! COMPILES UNSUCCESSFUL !|tee -a compile.log ;
exit ;;
esac
for i in $(echo *.pco)
do
echo ""|tee -a compile.log
_name=${i%.*}
for j in $_name*
do
if [[ $j != *.pco ]]
then
rm -f $j
fi
done
make -f procob.mk build COBS=${i%%.*}.cob EXE=${i%%.*} >$i.err 2>&1
export _rtncode=`echo $?`
case $_rtncode in
0) echo $i successful compilation.......|tee -a compile.log ;;
*) echo $i did NOT compile!!!!!! Errors exist!!!! |tee -a compile.log ;;
esac
echo ""|tee -a compile.log
done
3 REPLIES 3
Rodney Hills
Honored Contributor

Re: Problem with Korn Shell Script

Is it at all possible that a "set -f" was done somewhere before hand?

This turns off filename "glob"ing which I notice certain statements behave very strangely.

my 2 cents

-- Rod Hills
There be dragons...
John Palmer
Honored Contributor

Re: Problem with Korn Shell Script

Hi,

This bit of code in your script is the problem...
for i in $(echo *.pco)
do
echo ""|tee -a compile.log
_name=${i%.*}
for j in $_name*
do
if [[ $j != *.pco ]]
then
rm -f $j
fi
done

The '$(echo *.pco)' bit provides you with a list of ALL files in the current directory ending .pco. Note these could be in any order and you are NOT limiting the selection to files starting with "i".

'_name=${i%.*}' simply strips off the ".pco" from the end of the filename so you are left with ioparm, ioparm2 etc.

This bit...
for j in $_name*
do
if [[ $j != *.pco ]]
then
rm -f $j
fi

is attempting (I believe) to remove all files whose name starts with that in $_name which doesn't end in .pco. However the statement:
if [[ $j != *.pco ]]
is causing your problem as *.pco will be expanded by the shell and can't be guaranteed to work correctly.

You could try something like the following...

TMP=tmp.${$} # an arbitrary temp filename
for i in $(ls i*.pco)
do
_name=${i%.*} # remove the .pco
# temp rename the file we want to keep
mv ${i} ${TMP}
# remove all other files
rm -f ${_name}*
# reinstate the original .pco filename
mv ${TMP} ${i}

make -f procob.mk build COBS=${_name}.cob EXE=${_name} >${i}.err 2>&1

Hope this helps.

Regards,
John
Rodney Hills
Honored Contributor

Re: Problem with Korn Shell Script

John,

Wild card expansion does return a sorted list. You stated-
"The '$(echo *.pco)' bit provides you with a list of ALL files in the current directory ending .pco. Note these could be in any order and you are NOT limiting the selection to files starting with "i"."

From "man ksh"-
The form of the patterns is the Pattern Matching Notation defined by regexp(5).
The word is replaced with sorted file names matching the pattern. If no file name is found that matches the pattern, the word is left unchanged.

Also, I think the [[ ]] are magical in that file name expansion does not occur. You state-
if [[ $j != *.pco ]]
is causing your problem as *.pco will be expanded by the shell and can't be guaranteed to work correctly.

From "man ksh"-
A conditional expression is used with the [[ compound command to test attributes of files and to compare strings.
Word splitting and file name generation are not performed on the words between [[ and ]].

I first assumed what you did until and I tried them out and checked "man ksh". I'm still wondering "glob"ing got turned off...

-- Rod Hills
There be dragons...