KSH - different syntax for function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH - different syntax for function
# 1  
Old 06-01-2010
KSH - different syntax for function

Hi,

I wanted to know what's the difference between the below two syntax used for writing ksh function:

e.g. 1
------
Code:
 
function fn1
{
echo "Hello World"
}

e.g. 2
------
Code:
 
fn1 ()
{
echo "Hello World"
}

-dips
# 2  
Old 06-01-2010
The first is the more powerful KSH style function. The second is the Bourne style POSIX compliant version. The first form can have local variables and traps and $0 is set to the name of the function instead of the shell plus it supports recursion.
# 3  
Old 06-01-2010
Hi Scrutinizer,

Can you please explain the below in detail? as I didn't quite get it! Smilie

Quote:
The first is the more powerful KSH style function. The second is the Bourne style POSIX compliant version. The first form can have local variables and traps and $0 is set to the name of the function instead of the shell plus it supports recursion.
I tried calling function within itself
Code:
fn2 ()
{
echo "Hello World"
fn2
}
fn2

Its able to recursively call itself within itself, the thing is that I get Segmentation fault (core dumped) message.

Also I tried to include trap command inside the function as below
Code:
fn2 ()
{
        echo "Hello World"
        var=0
        while [ $var -le 3 ]
        do
                trap "echo 'Hello'" INT
                var=$(( $var + 1 ))
                echo $var
                sleep 5
        done
}

and fn2 was able to trap crtl-c.

Thanks for your patience. Please excuse me if I mis-interpreted something.

-dips
# 4  
Old 06-01-2010
Hi dips_ag,

Here is an example of the difference for local variables and $0, but perhaps that was already clear:
Code:
#!/bin/ksh
function f1
{
  f1v1=f1v1
  typeset f1v2=f1v2
  echo inside f1
  echo "f1v1: $f1v1"
  echo "f1v2: $f1v2"
  echo "\$0: $0"
}

f2()
{
  f2v1=f2v1
  typeset f2v2=f2v2
  echo inside f2
  echo "f2v1: $f2v1"
  echo "f2v2: $f2v2"
  echo "\$0: $0"
}

f1
echo outside f1
echo "f1v1: $f1v1"
echo "f1v2: $f1v2"
f2
echo outside f2
echo "f2v1: $f2v1"
echo "f2v2: $f2v2"

Output:
Code:
inside f1
f1v1: f1v1
f1v2: f1v2
$0: f1
outside f1
f1v1: f1v1
f1v2:
inside f2
f2v1: f2v1
f2v2: f2v2
$0: ./test
outside f2
f2v1: f2v1
f2v2: f2v2

========================
The trap that you set inside the POSIX function is a global trap. Have a look at this example about the difference between local and global trap:

Code:
#!/bin/ksh
function fn3
{
  trap 'echo "You triggered the trap that was set inside function fn3"' INT
  echo "sleeping 30 seconds inside fn3"
  sleep 30
}

fn4()
{
  trap 'echo "You triggered the trap that was set inside function fn4"' INT
  echo "sleeping 30 seconds inside fn4"
  sleep 30
}

trap 'echo "You triggered the trap that was set outside the function"' INT
fn3
echo "sleeping 30 seconds outside"
sleep 30
fn4
echo "sleeping 30 seconds outside"
sleep 30

Output, while pressing ctrl-C in the various stages:
Code:
sleeping 30 seconds inside fn3
^CYou triggered the trap that was set inside function fn3
You triggered the trap that was set outside the function
sleeping 30 seconds outside
^CYou triggered the trap that was set outside the function
sleeping 30 seconds inside fn4
^CYou triggered the trap that was set inside function fn4
You triggered the trap that was set inside function fn4
sleeping 30 seconds outside
^CYou triggered the trap that was set inside function fn4

===

About recursion: the difference lies in the use of local and global contexts. E.g. the use of local variables is essential for recursion to work properly, which is not possible with the POSIX style function in ksh..

S.
# 5  
Old 06-02-2010
Thanks Scrutinizer for the detail explanation.

-dips
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash function using variable in it syntax error

The below bash function uses multiple variables CODING, SAMPLE, SURVEY, andvariant in it. The user selects the cap function and details are displayed on the screen using the $SURVEY variable, the directory is changed to $SAMPLE and the samples.txt is opened so the user can select the sample to... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Help with ksh syntax error Unexpected Fi

Issue resolved, thanks (6 Replies)
Discussion started by: dangell82
6 Replies

3. Shell Programming and Scripting

Syntax Error while using CMP function

Hi All, I am getting a syntax error message while trying to compare 2 files using the compare function (LINUX) command substitution: line 79: syntax error near unexpected token `(' command substitution: line 79: `cmp -s <(tr , \n < $COMMON_TMP/nt_per_gs.done | sort) <(tr , \n <... (5 Replies)
Discussion started by: dsfreddie
5 Replies

4. UNIX for Dummies Questions & Answers

Problem syntax with user-defined function

Hi ! I got a script from Arabic to Roman numeral conversion - .comp.lang.awk, that I would like to modify to apply it on my input file. input ("|"-delimited fields): AAAAAA|1, 10, 13, 14, 25, 60 wanted output: AAAAAA|I, X, XIII, XIV, XXV, LX script.awk: #!/usr/bin/gawk -f ... (11 Replies)
Discussion started by: lucasvs
11 Replies

5. Shell Programming and Scripting

KSH syntax error

Hi all, Anyone know why I get the error below under ksh? $ EXCLUDES=( $(< "$EXCLUDES_FILE") ) sh: syntax error: `(' unexpected I'm trying to use the above line in a script and it's not working. The guy that gave me the script was running in a linux environment, so I'm thinking this might... (1 Reply)
Discussion started by: mmw
1 Replies

6. Shell Programming and Scripting

AWK Function syntax

Hi, I would like to know what is the correct syntax to perform a function in awk. Although I have seen several examples, not get it to work, this is what I'm trying: #!/bin/bash awk function multi (number) { return number * 3 } print multi (4)Thanks (2 Replies)
Discussion started by: Godie
2 Replies

7. Shell Programming and Scripting

ksh syntax error: `(' unexpected

So I am trying to convert my bash script into ksh, and this is what I have in the file so far: #!/bin/ksh login() { if then sendcmd BETA else sendcmd "$(xxd -c 32 -g 0 ${ZETA_ZETA} | awk '{print $2}')" fi } But when I run it: $ ./test.sh ... (1 Reply)
Discussion started by: guitarscn
1 Replies

8. Shell Programming and Scripting

syntax for CAT in ksh

Just want to ask if the below code is correct; if ; then echo "No log found from " $data exit 0 else cat out.txt fi this is to test if the content of out.txt is 0 then message appear that there is no log. Is this correct? (3 Replies)
Discussion started by: harry0013
3 Replies

9. Shell Programming and Scripting

syntax issue in ksh file

Hi all, I am struck with syntax for long time, Need to purge some lines from given file Not able to use value of $x Example of ksh script... Facing some syntax issue. Have Tried ‘with single , double ,backtick “” ` and \ escape character , doesn't seem to work. <line 1> echo $x #... (2 Replies)
Discussion started by: manav666
2 Replies

10. Shell Programming and Scripting

ksh syntax explanation - from mimetool

Hi I got this part of the script from the mimetool by Perderabo. I have difficulty in decyphering the syntax specially lines 4,5 & 9. Also the test condition in line 3. Could someone help me on this please. -------------------------------------- pwentry=$(grep "^$(logname):" /etc/paswd)... (2 Replies)
Discussion started by: chaandana
2 Replies
Login or Register to Ask a Question