Coredump when passing file name to ksh function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Coredump when passing file name to ksh function
# 1  
Old 09-13-2015
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

Code:
#!/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)

Code:
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

Code:
alias integer='typeset -i'

Debug output

Code:
+ . ./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)

# 2  
Old 09-13-2015
IMHO it does make more sense to pass the filename to the function
Code:
GetNumberOfRecords "$inputFile"

This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 09-16-2015
I made a stupid mistake Smilie
Thank you.
# 4  
Old 09-16-2015
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...
# 5  
Old 09-16-2015
Indeed, your "stupid mistake" may have found a bug other people missed. The shell shouldn't coredump despite much abuse.
# 6  
Old 09-16-2015
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing Oracle function as file input to sqlplus

Apologies if this is the incorrect forum.There is an issue in the function call I am facing while calling the same from a unix shell scripts. Basically, I want the ref cursor to return values to a variable in sqlpus. The function call is currently saved in a ".txt" file in a unix location. I want... (7 Replies)
Discussion started by: amvip
7 Replies

2. Shell Programming and Scripting

Passing variable value in a function to be used by another function

Hello All, I would like to ask help from you on how to pass variable value from a function that has been called inside the function. I have created below and put the variables in " ". Is there another way I can do this? Thank you in advance. readtasklist() { while read -r mod ver... (1 Reply)
Discussion started by: aderamos12
1 Replies

3. UNIX for Dummies Questions & Answers

Passing Input To Function

May i please know why is it printing the script name for $0 when i pass those parameters to function. #!/bin/bash -x usage() { echo "In Usage Function" echo $0 echo $1 echo $2 } echo "printing first time" echo $0 echo $1 echo $2 usage $0 $1 $2 Output: (2 Replies)
Discussion started by: Ariean
2 Replies

4. Shell Programming and Scripting

Why double quotation marks doesn't work in ksh function parameters passing?

I'm working on AIX 6, ksh shell. The parameters are some strings quotated by double quotation marks which from a file. They are quotated because there may be spaces in them. Example: "015607" "10" " " "A"I want to pass these parameters to a shell function by writing the following command: ... (4 Replies)
Discussion started by: Shimmey
4 Replies

5. Shell Programming and Scripting

passing values to function in Ksh

Hi, I'm trying to work on the script given below #!/bin/ksh -x pfile() { echo "$1" } touch smp19 echo "Hi" > smp19 result=$(pfile $smp19) echo $result As highlighted , when i pass $smp19 as parameter ,it does not display the output.However when i try giving "Hi" instead... (2 Replies)
Discussion started by: Sheema
2 Replies

6. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

7. Shell Programming and Scripting

Passing more than one argument in a function

Hi All, Calling a function with one argument and storing the return value in a shell script is as below:( so far I know) value="`fun_1 "argument1"`" Its working perfectly for me. Can u help me with passing more than one argument and storing the return value Thnaks in advance JS (1 Reply)
Discussion started by: jisha
1 Replies

8. Shell Programming and Scripting

[BASH - KSH] Passing array to a function

Passing a array to a function, a basic feature in modern language, seems to be only possible in KSH. Not in BASH. Depite all my efforts I couldn't come to a solution. See the following examples: It works perfectly in KSH: #!/usr/bin/ksh function print_array { # assign array by indirect... (3 Replies)
Discussion started by: ripat
3 Replies

9. Shell Programming and Scripting

Function Problem and CoreDump

Hi, i have a question: i build this function: function WriteLog { .... ... print $* print $* >> FileLog .... .... } the function are called with this sintax: ..... ..... (8 Replies)
Discussion started by: ZINGARO
8 Replies

10. UNIX for Advanced & Expert Users

Parameter passing in a function

I need to pass a parameter to a function in a script. My parameter is a string. When I display the parameter within my function, I only get the first word from string I pass in. How can I make the function receive the whole string (and not terminate at the first space it encounters)?. part of... (2 Replies)
Discussion started by: fastgoon
2 Replies
Login or Register to Ask a Question