LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2016, 11:52 PM   #1
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311
Blog Entries: 2

Rep: Reputation: 16
Question sub functions not executing


Sub functions can not execute.

I would like to use functions to produce the following output
Code:
a/Watermelon
a/Apple
a/Fire-Dragon
a/Papaya
Loop b:
b/Watermelon
b/Apple
b/Fire-Dragon
b/Papaya
Loop c:
c/Watermelon
c/Apple
c/Fire-Dragon
c/Papaya
Loop d:
d/Watermelon
d/Apple
d/Fire-Dragon
d/Papaya
Loop e:
e/Watermelon
e/Apple
e/Fire-Dragon
e/Papaya
=======================================================================
ATTEMPT 01
Code:
#!/bin/bash
#GNU bash, version 4.3.11(1)-release (i686-pc-linux-gnu)

#VARIABLES
	#INITIALIZE
		unset FF i w x y z
		
	#DEFINE
		z=Watermelon
		y=Apple
		x=Fire-Dragon
		w=Papaya

#FUNCTIONS
	f1 ()
	{
		echo="${1}/${z}"
	}
	
	f2 ()
	{
		echo="${1}/${y}"
	}
	
	f3 ()
	{
		echo "${1}/${x}"
	}
	
	f4 ()
	{
		echo "${1}/${w}"
	}
		
bkp ()
{
	for i in `echo $1`; do 
		echo "Loop ${i}:"
		f1 "${i}";
		f2 "${i}";
		f3 "${i}";
		f4 "${i}";
	done
}


FF="a b c d e"

bkp "${FF}"

The results are not ideal, with f1 and f2 not executing:
Code:
$ funcPassStr_SubFuncInForInFunc.sh 
Loop a:
a/Fire-Dragon
a/Papaya
Loop b:
b/Fire-Dragon
b/Papaya
Loop c:
c/Fire-Dragon
c/Papaya
Loop d:
d/Fire-Dragon
d/Papaya
Loop e:
e/Fire-Dragon
e/Papaya
***********************************************************************


In this next attempt, I define the sub functions inside of the function "bkp":

=======================================================================
ATTEMPT 02
Code:
#!/bin/bash
#GNU bash, version 4.3.11(1)-release (i686-pc-linux-gnu)

#VARIABLES
	#INITIALIZE
		unset FF i w x y z
		
	#DEFINE
		z=Watermelon
		y=Apple
		x=Fire-Dragon
		w=Papaya

		
#FUNCTIONS
bkp ()
{
	#SUB-FUNCTIONS
		f1 ()
		{
			echo="${1}/${z}"
		}
		
		f2 ()
		{
			echo="${1}/${y}"
		}
		
		f3 ()
		{
			echo "${1}/${x}"
		}
		
		f4 ()
		{
			echo "${1}/${w}"
		}
	
	for i in `echo $1`; do 
		echo "Loop ${i}:"
		f1 "${i}";
		f2 "${i}";
		f3 "${i}";
		f4 "${i}";
	done
}


FF="a b c d e"

bkp "${FF}"
Results:
Code:
$ funcPassStr_SubFuncInForInFunc.sh 

Loop a:
a/Fire-Dragon
a/Papaya
Loop b:
b/Fire-Dragon
b/Papaya
Loop c:
c/Fire-Dragon
c/Papaya
Loop d:
d/Fire-Dragon
d/Papaya
Loop e:
e/Fire-Dragon
e/Papaya
***********************************************************************

In the next attempt I define the variables x-z w/in the main function bkp:

=======================================================================
ATTEMPT 03
Code:
#!/bin/bash
#GNU bash, version 4.3.11(1)-release (i686-pc-linux-gnu)

#VARIABLES
	#INITIALIZE
		unset FF i w x y z
		
#FUNCTIONS
bkp ()
{
	#VARIABLES
		#DEFINE
			z=Watermelon
			y=Apple
			x=Fire-Dragon
			w=Papaya
	#SUB-FUNCTIONS
		f1 ()
		{
			echo="${1}/${z}"
		}
		
		f2 ()
		{
			echo="${1}/${y}"
		}
		
		f3 ()
		{
			echo "${1}/${x}"
		}
		
		f4 ()
		{
			echo "${1}/${w}"
		}
	
	#MAIN
		for i in `echo $1`; do 
			echo "Loop ${i}:"
			f1 "${i}";
			f2 "${i}";
			f3 "${i}";
			f4 "${i}";
		done
}


FF="a b c d e"

bkp "${FF}"
Results:

Code:
$ funcPassStr_SubFuncInForInFunc.sh 
Loop a:
a/Fire-Dragon
a/Papaya
Loop b:
b/Fire-Dragon
b/Papaya
Loop c:
c/Fire-Dragon
c/Papaya
Loop d:
d/Fire-Dragon
d/Papaya
Loop e:
e/Fire-Dragon
e/Papaya
***********************************************************************
Why do only functions f3 and f4 execute? How can I get f1 and f2 to execute?
 
Old 07-22-2016, 12:30 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Each function is executing exactly as written in each example.

They all produce the same unexpected result because you copy/pasted the same error into each file.

With that hint, see what you can figure out!
 
2 members found this post helpful.
Old 07-22-2016, 12:52 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Nice catch astrogeek ... I had to re-read the examples a few times to spot the issue
 
2 members found this post helpful.
Old 07-22-2016, 01:42 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,841

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
additionally you can use set -xv at the beginning of your script to see what's happening
and one more comment:
Code:
# use:
for i in $1; do
# instead of:
for i in `echo $1`; do
 
2 members found this post helpful.
Old 07-22-2016, 01:59 AM   #5
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by astrogeek View Post
Each function is executing exactly as written in each example.

They all produce the same unexpected result because you copy/pasted the same error into each file.

With that hint, see what you can figure out!
A-ha! That was actually a pretty hard bug to spot!

A couple of years ago I made a similar error in php. Took me bloody AGES to find.

I second what pan64 said. Insert
Code:
set -xv
at the beginning of your script and you will find this yourself (another hint: the errors are WITHIN the f1 and f2 functions).

Also, my hat is off to the OP. This is a great example of how a question should be written!

Best regards,
HMW
 
1 members found this post helpful.
Old 07-22-2016, 07:18 AM   #6
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by HMW View Post
Also, my hat is off to the OP. This is a great example of how a question should be written!
That was about the only thing, I wanted to remark. Unable to spot the error I thought of other things.., what's my neighbor doing in this heat working on his roof? BAMM! There is the coding error. In deed, not what I had expected, first.
 
1 members found this post helpful.
Old 07-22-2016, 08:30 AM   #7
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Talking I had been using the wrong -xv

Quote:
Originally Posted by pan64 View Post
additionally you can use set -xv at the beginning of your script to see what's happening
I had been using bash -xv, but not in my actual program, yet in the terminal. I would first have to execute "bash -xv" to launch a sub-shell, and from this I had been launching my program. Unfortunately the "bash -xv" sub-shell hardly gives any information at all.

Using "set -xv" right in the programs' script is an outstanding debugging technique, and it looks like I won't have to worry about anymore about going to the trouble of first having to launch that super verbose sub-shell "bash +xv".

Moreover, with "set -xv" you can refine the suspected area of your program with:
Code:
set -xv           #debugging on

suspect cmd1
suspect cmd2
suspect cmd3

set +xv           #debugging off
I'm sure you told me about this before, thanks for reminding me again. I'll definitely put this into my daily repertoire. Better late than never.
 
Old 07-22-2016, 08:36 AM   #8
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Talking brevity

Quote:
Originally Posted by pan64 View Post
# use:
Code:
for i in $1; do
# instead of:
Code:
for i in `echo $1`; do
Thanks very much, writing
Code:
$1
is far more convenient than
Code:
`echo $1`
.

Easier on one's eyes during debugging also!

Last edited by andrew.comly; 07-22-2016 at 08:37 AM. Reason: clarity
 
Old 07-22-2016, 08:42 AM   #9
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Lightbulb

Quote:
Originally Posted by pan64 View Post
additionally you can use set -xv at the beginning of your script to see what's happening
Thanks again for this new debugging tool, and after only a few minutes of utilizing this I found the problem.

Now my program outputs the choice results.
 
  


Reply

Tags
for loop, functions, pass 1, variables



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
OSE system call functions to Linux Sytem Call functions required roshantraj30 Linux - General 0 06-08-2009 02:06 AM
LXer: OpenOffice.org Calc functions, part 1: Understanding functions LXer Syndicated Linux News 0 03-31-2007 12:01 PM
Slackpkg: missing something in /usr/libexec/slackpkg/functions.d/dialog-functions.sh michelino Slackware 4 03-20-2007 12:22 PM
Crawling a directory and executing Python functions. Travis86 Programming 2 07-06-2005 01:06 AM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration