Coredump when passing file name to ksh function

Hi,

I'm playing with ksh. I'm trying to do a simple task:
read file name from cli and call a function which calculated number of lines in file.
I'm getting coredump every time when I try to read that file.

Korn shell version

$ print ${.sh.version}
Version AJM 93u+ 2012-08-01

Main program

#!/bin/ksh -x

# Read input file from cli and 
# perform some simple awk manipulations

### Include aliases types ### 

. ./talias

# Used variables 

inputFile=
integer NumberOfRecords=0

### Script functions #######

FPATH=/home/dev/ksh
autoload GetNumberOfRecords

while getopts :f:a Flag
do
	case ${Flag} in
	a)	
		print "${inputFile}"
		GetNumberOfRecords inputFile
		;;
	f)
		if [ -r ${OPTARG} ]
		then
		   print "${OPTARG} exists"
		   inputFile=${OPTARG}
		fi
		;;
	\?)
		print -R "'${OPTARG}' - unkown flag"
		print "${Usage}"
		;;
	:)
		print -R "'-f' requires a value, specify input name"
		exit 2
		;;
	esac
done

function name (stored in file with same name in /home/dev/ksh)

GetNumberOfRecords
{
   Input=${1}
   
   print "Input FIle is ${Input}"
   
   while read Line
   do
     print "${Line}"
   done < ${Input}

   print "Total rows for ${Input} is ${NumberOfRows}"
}

talias is file with alias for integer

alias integer='typeset -i'

Debug output

+ . ./talias
+ alias integer='typeset -i'
+ inputFile=''
+ NumberOfRecords=0
+ typeset -i NumberOfRecords
+ FPATH=/home/dev/ksh
+ typeset -fu GetNumberOfRecords
+ getopts :f:a Flag
+ [ -r emp.data ]
+ print 'emp.data exists'
emp.data exists
+ inputFile=emp.data
+ getopts :f:a Flag
+ print emp.data
emp.data
Segmentation fault (core dumped)

IMHO it does make more sense to pass the filename to the function

GetNumberOfRecords "$inputFile"
1 Like

I made a stupid mistake :rolleyes:
Thank you.

Hi MadeInGermany...

I really don't understand why the OPs code would have caused a seg_fault.
I would have thought the shell would have had that under control...

Could, (in your learned opinion), you possibly explain why so that I can get ny head around it...

TIA.

Bazza...

Indeed, your "stupid mistake" may have found a bug other people missed. The shell shouldn't coredump despite much abuse.

Well, we don't know what the function GetNumberOfRecords does.
At least I would run a file on the core file to see if ksh is to blame - maybe it's an external command?