LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 01-08-2013, 11:12 PM   #1
Garrett85
Member
 
Registered: Jan 2011
Posts: 332

Rep: Reputation: 6
BASH - passing arguments


I wrote a date validation program and it worked just fine but the guy teaching me to program want me to modify the program so that all varaible within a function are local and variables the function workes on must be refered by the function as command line arguments ($1 $2, etc) rather than their outside world name ${VARIABLE}. I have also been told that rather than using an else clause with in an if statement to try and set a defualt value first and let the if change that value if the condition is true. Below is one of my functions. The variable I need back from this is DAY and it will hold GOOD or BAD. Since I cannot use global variables within a function I would have to rename DAY="DAY" to local DAY=${DAY} within the function. But then how wold I give it a defualt value since if the next line is DAY="BAD" DAY would no longer point to the value of ${DAY}? I though maybe I could pre set the value befor the function is called, in the outside world I could go ahead and type DAY="BAD" but in order to retrieve the vale to the outside from within the function, don't I need to do something like this DAY=get_day()? if that's the case DAY would immediatly lose it's default value of "BAD" before it ever got to the function. I need some help here. Thanks.


Code:
get_day() {
	DAY="BAD"
	FIRSTDIGIT=${MM:0:1}
	SECONDDIGIT=${MM:1:1}

	if [[ ${FIRSTDIGIT} -eq 0 ]]; then #{
		if [[ ${DD} -gt 0 ]] && [[ ${DD} -le ${CALENDER[SECONDDIGIT-1]} ]]; then #{
			DAY="GOOD"
		fi #}
	else #{
		if [[ ${DD} -gt 0 ]] && [[ ${DD} -le ${CALENDER[MM-1]} ]]; then #{
			DAY="GOOD"
		fi #}
	fi #} }
}
 
Old 01-09-2013, 01:40 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Your explanation seems a little confusing, but my understanding would be:

1. get_day function to return whether day is GOOD or BAD .... to me this would mean placing an echo at the end of the function as you need to return a string

2. internally to the function you will need local variables for the following:

DAY - would pick an alternate name here so to not get confused ... maybe retval (I am using lower case as upper is usually reserved for environment variables)
FIRSTDIGIT
SECONDDIGIT
DD
CALENDER - this one may be tricky as it is an array ... is it used elsewhere?

---
I wanted to mention previously that I am not sure why you use the outer if that tests FIRSTDIGIT ... if you test as a number it will remove the leading zero

Hope some of that helps

If not, please advise which points I am mistaken about?
 
Old 01-09-2013, 09:50 AM   #3
Garrett85
Member
 
Registered: Jan 2011
Posts: 332

Original Poster
Rep: Reputation: 6
using echo

If in the if statement within the function I use echo to return a value, will that not print that value to the screen as echo typacally does? Because I wouldnt actaully be wanting to print anything to the screen from within a function. The reason I bother to remove the 0 from MM is because my script was not getting the right index from CALENDER if 02 was input instead of just 2. I was something that I hadnt thought of originally until I tested it and it didn't work, I stipped the 0 off of MM using FIRST & SECONDDIGIT and it worked after that.

Quote:
Originally Posted by grail View Post
Your explanation seems a little confusing, but my understanding would be:

1. get_day function to return whether day is GOOD or BAD .... to me this would mean placing an echo at the end of the function as you need to return a string

2. internally to the function you will need local variables for the following:

DAY - would pick an alternate name here so to not get confused ... maybe retval (I am using lower case as upper is usually reserved for environment variables)
FIRSTDIGIT
SECONDDIGIT
DD
CALENDER - this one may be tricky as it is an array ... is it used elsewhere?

---
I wanted to mention previously that I am not sure why you use the outer if that tests FIRSTDIGIT ... if you test as a number it will remove the leading zero

Hope some of that helps

If not, please advise which points I am mistaken about?
 
Old 01-09-2013, 10:09 AM   #4
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
Try it out:
Code:
output=$(echo "foo bar")
You'll see it won't echo to the screen, but into the variable; which you can try out with
Code:
echo "output = ${output}"
so, no: calling a function does not necessarily have to output to the screen.
Since you cannot "return" a string, and the scope of a variable is tricky at best, even while using global variables... I think this is your best bet.

Take this example, as how tricky scoping of variables can be (run it in a directory with at least a few files)
Code:
NUMBER=0
ls | while read fn
do
    ((NUMBER++))
done
echo $NUMBER
The value of ${NUMBER} you expected might not really be the number you got... This is something how bash traditionally works with pipes; there are workarounds.

Last edited by Ramurd; 01-09-2013 at 10:09 AM. Reason: use closing tag :-)
 
Old 01-09-2013, 03:03 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
parameter substitution

Bash has a built in substitutions for defining values under various conditions, such as if a string is empty.

Code:
$ aloha(){ local foo=${foo:-goodbye} ; echo "$foo" ;}

$ foo=hello ; aloha
hello

$ unset foo ; aloha
goodbye
 
  


Reply



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
[SOLVED] passing commands as arguments to functions in bash script mastahyeti Linux - General 3 05-31-2010 06:19 PM
passing arguments to a child process in C++ bhanuvrat Programming 7 02-07-2010 10:01 PM
bash, passing arguments Four Linux - Newbie 3 02-06-2006 08:24 AM
passing a list of arguments to a command hdagelic Linux - General 2 05-09-2005 09:30 AM
Handline passing arguments in C AMMullan Programming 9 03-22-2004 01:37 AM

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

All times are GMT -5. The time now is 05:15 PM.

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