Shell functions mystery


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell functions mystery
# 1  
Old 01-02-2016
Shell functions mystery

so i noticed that when a shell script has a function defined in it, running "sh -x" on that shell script from the command line doesnt show what the function is doing. i like this.

is there anyway for anyone to get around that? to be able to see exactly what a function or functions are doing?
# 2  
Old 01-02-2016
Put set -x inside the function's definition. Or, if you just want to trace certain parts of the function:
Code:
fcn() {
	echo untraced
	set -x
	echo traced
	set +x
	echo 'more untraced'
}

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 01-02-2016
Quote:
Originally Posted by SkySmart
so i noticed that when a shell script has a function defined in it, running "sh -x" on that shell script from the command line doesnt show what the function is doing. i like this.

is there anyway for anyone to get around that? to be able to see exactly what a function or functions are doing?
Yes. Issue set -x (or set -xv) inside the function, like this:

Code:
#! /bin/ksh

func1 ()
{
     set -x     # scope is inside function func1()
     ... rest of the function...
     return 0
}

# main()

set -x     # scope is only in main()

do_something
func1
do something_else

exit 0

My personal solution for this is to execute a variable "$chFullDebug" inside each function i write. Under normal conditions this variable is empty and the command does nothing. When i need the extra debugging i set the variable to "set -xv":

Code:
#! /bin/ksh

func1 ()
{
     $chFullDebug
     ...
     return 0
}


func2 ()
{
     $chFullDebug
     ...
     return 0
}

# main()

# typeset -x chFullDebug="set -xv"         # debug mode
typeset -x chFullDebug=""                  # normal mode

$chFullDebug

... rest of the code...
func1
func2

exit 0

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
Old 01-03-2016
thanks so much guys!

are these the only ways that a shell script inner workings can be pried into? are there other ways?

strace?
# 5  
Old 01-03-2016
If you want the functions to run with the same settings that are being used in the shell script that defines those functions, you can try something like the following:
Code:
#!/bin/ksh
settings=$(set +o)
func() {
	$settings
	printf '\t%s\n' "$@"
}
func "$@"

And see what happens when you invoke the script by name:
Code:
./tester a b c "d e"

and when you invoke its with tracing enabled:
Code:
ksh -xv tester a b c "d e"

This was written and tested with the Korn shell, but will work with any shell that conforms to the POSIX requirements for the set utility.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Functions in a shell script

so i have a very big script that has the following format: functionA () { .... ... .... } Results=$(functionA) the code inside of functionA is very huge. so by the time the script gets to the "Results=" part, several seconds have already passed. the script is about 15MB in size.... (4 Replies)
Discussion started by: SkySmart
4 Replies

2. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

3. Shell Programming and Scripting

Shell functions usage

Hi all, I have a doubt.. If we create shell functions through a script itself, can we use the same functions in command line also.. for example: $ cat a.sh ##### Functions function system_info { } function show_uptime { } (4 Replies)
Discussion started by: raghu.iv85
4 Replies

4. Shell Programming and Scripting

using library functions in shell script

hey guys, i made up a library file called common.lib so as to reuse the same code without typing it again. here is the code. its pretty basic . ## This is the second function compare() { file1 = $1 file2 = $2 cmp $file1 $file2 if then echo "comparison is possible"... (1 Reply)
Discussion started by: Irishboy24
1 Replies

5. Shell Programming and Scripting

Mathematical functions in bash shell

Hi, How can i do the mathematical calculations in bash shell? Are the mathematical functions available in bash shell? Ex: pow ceil floor sqrt (5 Replies)
Discussion started by: cola
5 Replies

6. Shell Programming and Scripting

How to call C functions in shell scripts?

How can i call dynamic C functions in shell scripts? (5 Replies)
Discussion started by: agarwal
5 Replies

7. Shell Programming and Scripting

Calling shell functions from another shell script

Hi, I have a query .. i have 2 scripts say 1.sh and 2.sh 1.sh contains many functions written using shell scripts. 2.sh is a script which needs to call the functions definded in 1.sh function calls are with arguments. Can some one tell me how to call the functions from 2.sh? Thanks in... (6 Replies)
Discussion started by: jisha
6 Replies

8. Shell Programming and Scripting

Functions in C-Shell

Hi All, I have a very long code called myfunction -> "if ..... else if .... else if ..end if " And i have several other codes which need to call the "myfunction" code. How can C-shell call a function "B]myfunction" ? Can any body give me an example ?? (1 Reply)
Discussion started by: Raynon
1 Replies

9. Shell Programming and Scripting

functions in c shell??

i've been told that c shell does not support functions/subroutines is that true? if not can somebody give me the basic syntax for creating a function. it would very much appreciated! thanks in advance (1 Reply)
Discussion started by: ballazrus
1 Replies

10. Shell Programming and Scripting

Shell script functions

Simple shell script : date test_fn() { echo "function within test shell script " } on the shell prompt I run > . test Then I invoke the function on the command line as below : test_fn() It echos the line function within test shell script and works as expected. ... (5 Replies)
Discussion started by: r_subrahmanian
5 Replies
Login or Register to Ask a Question