Sponsored Content
Full Discussion: Function Script
Top Forums Shell Programming and Scripting Function Script Post 302994469 by bakunin on Thursday 23rd of March 2017 09:02:07 AM
Old 03-23-2017
Quote:
Originally Posted by rpiboy
First time doing a function script and I am getting an error. Anyone knows the problem?
As has already been stated: knowing what the error is makes it somewhat easier to tell you the reason. Save for that, you might consider putting an explicit return-command at the end of your functions:

Code:
hello()
{
echo "Executing function hello"

return 0
}

This will give back a return code you can check in the main program and base a reaction on it. Like this:

Code:
#! /bin/bash
myfunc()
{
if [ $1 -eq 1 ] ; then
    return 0
else
    return 1
fi
}

# main()
echo "Script starts"

echo calling myfunc() with 1:
if myfunc 1 ; then
     echo "myfunc 1 returned $?"
fi

echo calling myfunc() with 0:
if myfunc 0 ; then
     echo "myfunc returned $?"
fi

I hope this helps.

bakunin
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function Bug in script - need help

My script is erroring with: testtapemgr.sh: FTP_RETURNS: not found I cannot see what I am doing wrong..when it calls that function from the Volume returns function and says taht FTP_RETURNS is not found and exits out of the script. What am I not seeing here? #### Return Volume Function ... (4 Replies)
Discussion started by: gzs553
4 Replies

2. Shell Programming and Scripting

call function in one script from another script

Hello experts, Is it possible to call function in one script from another script? Example. Script 1: #!/bin/bash function1(){ } Script 2: #!/bin/bash #code to call function1 in Script 1 :confused: thanks you (6 Replies)
Discussion started by: minifish
6 Replies

3. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

4. Shell Programming and Scripting

Call shell script function from awk script

hi everyone i am trying to do this bash> cat abc.sh deepak() { echo Deepak } deepak bash>./abc.sh Deepak so it is giving me write simply i created a func and it worked now i modified it like this way bash> cat abc.sh (2 Replies)
Discussion started by: aishsimplesweet
2 Replies

5. Shell Programming and Scripting

SH Script for Logical OR function

Using good ole fashion bourne shell scripting I have to take 2 files and compare them 1:1 in a Logical OR "gate" or function and then output the info into a 3rd file. I have never tried anything like this before :eek: so any hand holding will be much "Thanked" OR Gate being: A, B, Result 0,... (2 Replies)
Discussion started by: Alivadoro
2 Replies

6. Shell Programming and Scripting

Help for exiting the function not script

function2() { cmd1 cmd2 cmd3 .... cmdn } function2() { cmd11 cmd12 cmd13 .... .... } for i in {1,2} (7 Replies)
Discussion started by: yanglei_fage
7 Replies

7. Shell Programming and Scripting

Shell Script function to use script name for log file output

Hi Team - I"m very new to Shell Scripting so I have a rather novice question. My forte is Windows Batch Scripting so I was just wondering what the Shell Script equivalent is to the DOS command %~n? %~n is a DOS variable that dispayed the script name. For instance (in DOS): REM... (11 Replies)
Discussion started by: SIMMS7400
11 Replies

8. Shell Programming and Scripting

What is the function of the following lines at the top of a shell script file: Directory and Script?

The file starts like this: Directory: <path to the script> Script: <script fife name> #!bin/ksh ##Comments <actual script> What is the use of the first two lines in the script? What if I save the file without them? What will be the effect? They are not comments. Im very new to this,... (4 Replies)
Discussion started by: remytom
4 Replies

9. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

10. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies
BACKTRACE(3)						     Linux Programmer's Manual						      BACKTRACE(3)

NAME
backtrace, backtrace_symbols, backtrace_symbols_fd - support for application self-debugging SYNOPSIS
#include <execinfo.h> int backtrace(void **buffer, int size); char **backtrace_symbols(void *const *buffer, int size); void backtrace_symbols_fd(void *const *buffer, int size, int fd); DESCRIPTION
backtrace() returns a backtrace for the calling program, in the array pointed to by buffer. A backtrace is the series of currently active function calls for the program. Each item in the array pointed to by buffer is of type void *, and is the return address from the corre- sponding stack frame. The size argument specifies the maximum number of addresses that can be stored in buffer. If the backtrace is larger than size, then the addresses corresponding to the size most recent function calls are returned; to obtain the complete backtrace, make sure that buffer and size are large enough. Given the set of addresses returned by backtrace() in buffer, backtrace_symbols() translates the addresses into an array of strings that describe the addresses symbolically. The size argument specifies the number of addresses in buffer. The symbolic representation of each address consists of the function name (if this can be determined), a hexadecimal offset into the function, and the actual return address (in hexadecimal). The address of the array of string pointers is returned as the function result of backtrace_symbols(). This array is malloc(3)ed by backtrace_symbols(), and must be freed by the caller. (The strings pointed to by the array of pointers need not and should not be freed.) backtrace_symbols_fd() takes the same buffer and size arguments as backtrace_symbols(), but instead of returning an array of strings to the caller, it writes the strings, one per line, to the file descriptor fd. backtrace_symbols_fd() does not call malloc(3), and so can be employed in situations where the latter function might fail. RETURN VALUE
backtrace() returns the number of addresses returned in buffer, which is not greater than size. If the return value is less than size, then the full backtrace was stored; if it is equal to size, then it may have been truncated, in which case the addresses of the oldest stack frames are not returned. On success, backtrace_symbols() returns a pointer to the array malloc(3)ed by the call; on error, NULL is returned. VERSIONS
backtrace(), backtrace_symbols(), and backtrace_symbols_fd() are provided in glibc since version 2.1. CONFORMING TO
These functions are GNU extensions. NOTES
These functions make some assumptions about how a function's return address is stored on the stack. Note the following: * Omission of the frame pointers (as implied by any of gcc(1)'s nonzero optimization levels) may cause these assumptions to be violated. * Inlined functions do not have stack frames. * Tail-call optimization causes one stack frame to replace another. The symbol names may be unavailable without the use of special linker options. For systems using the GNU linker, it is necessary to use the -rdynamic linker option. Note that names of "static" functions are not exposed, and won't be available in the backtrace. EXAMPLE
The program below demonstrates the use of backtrace() and backtrace_symbols(). The following shell session shows what we might see when running the program: $ cc -rdynamic prog.c -o prog $ ./prog 3 backtrace() returned 8 addresses ./prog(myfunc3+0x5c) [0x80487f0] ./prog [0x8048871] ./prog(myfunc+0x21) [0x8048894] ./prog(myfunc+0x1a) [0x804888d] ./prog(myfunc+0x1a) [0x804888d] ./prog(main+0x65) [0x80488fb] /lib/libc.so.6(__libc_start_main+0xdc) [0xb7e38f9c] ./prog [0x8048711] Program source #include <execinfo.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> void myfunc3(void) { int j, nptrs; #define SIZE 100 void *buffer[100]; char **strings; nptrs = backtrace(buffer, SIZE); printf("backtrace() returned %d addresses ", nptrs); /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) would produce similar output to the following: */ strings = backtrace_symbols(buffer, nptrs); if (strings == NULL) { perror("backtrace_symbols"); exit(EXIT_FAILURE); } for (j = 0; j < nptrs; j++) printf("%s ", strings[j]); free(strings); } static void /* "static" means don't export the symbol... */ myfunc2(void) { myfunc3(); } void myfunc(int ncalls) { if (ncalls > 1) myfunc(ncalls - 1); else myfunc2(); } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "%s num-calls ", argv[0]); exit(EXIT_FAILURE); } myfunc(atoi(argv[1])); exit(EXIT_SUCCESS); } SEE ALSO
gcc(1), ld(1), dlopen(3), malloc(3) COLOPHON
This page is part of release 3.25 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU
2008-06-14 BACKTRACE(3)
All times are GMT -4. The time now is 09:35 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy