LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-28-2015, 05:39 PM   #1
rcb8dboy
LQ Newbie
 
Registered: Feb 2015
Posts: 2

Rep: Reputation: Disabled
bash more commands in one function


hello

first year of learning linux and bash script
Now i wrote a bash script with a function.
Today i recieved a email from my teacher that 1 function only can have 1 command and no more then 1?

thats a part of the script

Code:
function tx_unreachable_msg()
{
LOGLINE="[$DATE] no ip service unreachable"
typewriter "$NOIPSERVICE niet bereikbaar" .1
sleep 1 
typewriter "Mail wordt verzonden naar $email" .1
mail -s "NO ip mail error" "$email"<<EOF
dynupdate.no-ip.com niet bereikbaar @ $DATE
EOF
}
it's part dutch part english but the point is that the first LOGLINE is forbidden in the function because i use the functie to send a mail end not to write a logline but that's a part of the command in my case?

So what does this function?
If the no ip service is unreachable then it's write to /etc/var/log/no-ip and after the sleep it's send a simpel mail to the adres in the variabele mail.

i read here at the first line:
Quote:
Shell functions are a way to group commands for later execution using a single name for the group.
so is it true that my function is wrong and so no can someone point me to a website where it's say that more commands are allow in 1 function0

grts b

Last edited by rcb8dboy; 02-28-2015 at 05:41 PM.
 
Old 02-28-2015, 05:59 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,609

Rep: Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702
It is possible to put more than one command in a function, but your teacher is trying to teach you how to write good programs. A function should have a single purpose. It should not be just a bunch of commands one after the other.
 
Old 02-28-2015, 06:27 PM   #3
rcb8dboy
LQ Newbie
 
Registered: Feb 2015
Posts: 2

Original Poster
Rep: Reputation: Disabled
ok thx

so what i read in gnu.org is correct and it's possible to write more commands in one function.
So saying that 1 function can only have no more than 1 functie is not correct even if he try to teach me the "good way".
Before i have a class of java programming and we did this all the time to clean up or code.
And it's not that i write my whole script in one function :-) i have more functions depend on the parameter that was given.

The challenge was to write some functions,us a case statement and appand some parameters in the ./script.sh
When i append the parameter -m for example it's only execute the mail function so writing a error to the log file and then mail the user is not so big.
I will ask my teacher why he say that only one functie can be append to 1 function.

grts b,
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to end the bash script using commands in bash not manually by pressing ctrl+c Sanpreet Singh Linux - Newbie 1 07-03-2013 02:04 PM
[SOLVED] Setting function default in a bash script - Bash sixtensixone Linux - General 13 01-08-2011 02:44 PM
Bash array Add function example using indirect array reference as function argument bobywelsh Programming 10 07-05-2010 05:44 AM
Bash looping or function within a function FirmbIT Programming 2 04-24-2006 02:06 PM
Bash Commands List by Function? TBennettcc Linux - Newbie 6 01-06-2006 03:08 AM


All times are GMT -5. The time now is 04:47 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration