LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash more commands in one function (https://www.linuxquestions.org/questions/linux-newbie-8/bash-more-commands-in-one-function-4175535388/)

rcb8dboy 02-28-2015 04:39 PM

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

smallpond 02-28-2015 04:59 PM

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.

rcb8dboy 02-28-2015 05:27 PM

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,


All times are GMT -5. The time now is 01:46 AM.