Sponsored Content
Top Forums Shell Programming and Scripting Problem using function in awk Post 302451090 by kristinu on Sunday 5th of September 2010 04:33:41 PM
Old 09-05-2010
Yes, I think you are right. Can only return one value. But it does not matter in my case. Since they are both random numbers I can just use one of them.

---------- Post updated at 03:33 PM ---------- Previous update was at 02:47 PM ----------

I have this awk script, and I output mean and sigma, but the result is not what I assigned to mean and sigma.

Code:
 
 function rgaussian1() {
      a = rand()
      b = rand()
      c = rand()
      r1 = a + b + c
      return r1
  }

  function rgaussian11(mean, sigma) {
      a = rgaussian1()
      b = mean + (a * sigma)
      print "VALUES: ",mean,sigma
      return b
  }

  NF == 2 {
      a = 5
      b = 0.5
      r1 = rgaussian1()
      r2 = rgaussian11(a,b)
      print $0,$2,r1,r2
  }

As you can see below I am not getting the values of mean and sigma that I have passed to the function.



Code:
VALUES:  0.319071 0.692849
10 0  0 1.50904 -0.654771 1.0627 0.833281
VALUES:  0.244565 0.468954
13 5.74484  5.74484 2.47868 1.11852 0.990984 -0.889183
VALUES:  0.787803 0.202516
16 10.1769  10.1769 0.570419 -0.736454 1.48426 1.09622
VALUES:  0.187611 0.611989
19 13.8527  13.8527 1.40469 -0.309851 0.53924 -1.40168
VALUES:  0.669174 0.450629
22 16.8957  16.8957 1.38384 0.0894231 -0.804437 -0.168806
VALUES:  0.523561 0.360272
25 19.3552  19.3552 1.05967 1.17779 2.0299 -0.459928
VALUES:  0.749601 0.872226
28 21.3932  21.3932 1.02497 -0.17011 -1.12271 0.916729
VALUES:  0.39957 0.0115686
31 23.0869  23.0869 0.713036 -0.15845 -0.852606 -0.867672
VALUES:  0.488232 0.786265
34 24.5867  24.5867 1.38646 0.907416 0.215826 -0.207289
VALUES:  0.721516 0.283772
37 25.9775  25.9775 1.2652 0.18478 0.643813 1.46849
VALUES:  0.796942 0.08114
40 27.2779  27.2779 1.07348 2.44546 -0.369929 0.784368
VALUES:  0.32347 0.847031
43 28.5486  28.5486 1.86091 -0.337079 0.683475 0.124624
VALUES:  0.858226 0.607827
46 29.754  29.754 1.81017 0.697858 -0.547066 1.12782
VALUES:  0.096079 0.495649
49 30.9075  30.9075 1.82609 1.88842 -0.843563 0.535018
VALUES:  0.601441 0.696511
52 32.0179  32.0179 1.4897 0.233247 -0.0728211 -1.17591
VALUES:  0.460437 0.181952
55 33.0682  33.0682 0.657609 -0.594157 -0.79817 -0.281863
VALUES:  0.630648 0.42759
58 34.082  34.082 1.48051 0.869132 0.319669 -0.426564
VALUES:  0.446626 0.770096
61 35.0402  35.0402 1.70818 -0.775314 -0.0754717 -0.160226
VALUES:  0.127291 0.985517
64 35.9725  35.9725 1.59898 -1.89246 -0.441951 0.257042
VALUES:  0.970425 0.0665038

 

10 More Discussions You Might Find Interesting

1. Programming

Problem with aio_write() function

Hello, How to execute a call back function after aio_write() or aio_read() in Sun Solaris 5.7? I have filled the control block struct aiocb as follows: aio_sigevent.sigev_signo = SIGEV aio_sigevent.sigev_notify = SIGEV_THREAD Then I have filled the call back function in ... (0 Replies)
Discussion started by: hmurali
0 Replies

2. Shell Programming and Scripting

problem in awk int() function

awk -vwgt=$vWeight -vfac=$vFactor ' BEGIN { printf("wgt:" wgt "\n"); printf("factor:" fac "\n"); total = sprintf("%.0f", wgt * fac); total2 = sprintf("%.0f", int(wgt * fac)); printf("total:" total "\n"); printf("total2:" total2 "\n"); } ' if vWeight=326.4 vFactor=100 the result... (2 Replies)
Discussion started by: qa.bingo
2 Replies

3. Shell Programming and Scripting

awk , function call problem

#!/bin/bash awk ' function ad(t,r){ return (t+r); } BEGIN{ print ad(5,3); } { print ad(5,3); } ' Doesn't print anything for the last print ad(5,3); (6 Replies)
Discussion started by: cola
6 Replies

4. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

5. Shell Programming and Scripting

splice function problem

Hi All, I am using splice function in for loop to delete particular element from array with one condition. my $cnt=0; foreach my $elem (@result) { if (condition){ splice(@result, $cnt, 1);} else{ $cnt++;} } Now when in array, two elements comes sequentially with the... (3 Replies)
Discussion started by: gentleDean
3 Replies

6. UNIX for Advanced & Expert Users

AWK sub function curious problem under bash

I need to detect the number of pages in a print job when it is available so I can warn users when they try to print a report much larger than they expected. Sometimes they are trying to print 1000 page reports when they thought they were getting a 10 page report. Under linux I am scanning the... (5 Replies)
Discussion started by: Max Rebo
5 Replies

7. Shell Programming and Scripting

AWK Problem in recursive function

Hi, I have a file like this SPF_HC00001|iCalcular_Monto_Minimo|--->|SPF_HC00028|pstcObtener_Monto_Minimo SPF_HC00004|iCalcular_Incrementos|--->|SPF_HC00032|pstcObtener_Num_Incrementos SPF_HC00005|iCalcular_Articulo_167_Reformado|--->|SPF_HC00031|pstcObtener_Por_CB_Inc... (2 Replies)
Discussion started by: kcoder24
2 Replies

8. UNIX for Dummies Questions & Answers

Explanation on problem "match" function awk

Hello Unix experts, If I could get any explanations on why the code below doesn't work it would be great ! My input looks like that ("|" delimited): Saaaaabbbbbccccc|ok Sdddddfffffggggg|ok The goal is, if $2 is "ok", to remove everything before the pattern given in the match function... (5 Replies)
Discussion started by: lucasvs
5 Replies

9. Shell Programming and Scripting

Function problem

hey guys, im trying to learn bourne shell atm and I'm having some issues with functions. so heres my code: #!/bin/bash ##functions memory () { free -m } space () { df -h } ip () { (5 Replies)
Discussion started by: hawkfro12
5 Replies

10. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies
shell(1F)							   FMLI Commands							 shell(1F)

NAME
shell - run a command using shell SYNOPSIS
shell command [command] ... DESCRIPTION
The shell function concatenate its arguments, separating each by a space, and passes this string to the shell ($SHELL if set, otherwise /usr/bin/sh). EXAMPLES
Example 1: A sample output of shell command. Since the Form and Menu Language does not directly support background processing, the shell function can be used instead. `shell "build prog > /dev/null &"` If you want the user to continue to be able to interact with the application while the background job is running, the output of an exe- cutable run by shell in the background must be redirected: to a file if you want to save the output, or to /dev/null if you don't want to save it (or if there is no output), otherwise your application may appear to be hung until the background job finishes processing. shell can also be used to execute a command that has the same name as an FMLI built-in function. NOTES
The arguments to shell will be concatenate using spaces, which may or may not do what is expected. The variables set in local environments will not be expanded by the shell because "local" means "local to the current process." ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
sh(1), attributes(5) SunOS 5.10 5 Jul 1990 shell(1F)
All times are GMT -4. The time now is 02:01 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy