👤
Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Function Script

👤 Login to reply

 
Thread Tools Search this Thread
# 1  
Old 03-22-2017
Function Script

First time doing a function script and I am getting an error. Anyone knows the problem?

Code:
#!/bin/bash
hello()
{ echo "Executing function hello"
}
echo "Script has started now"
hello
echo "Script will end"

# 2  
Old 03-22-2017
If you are getting "not found" error, then likely your system does not have /bin/bash. As example, on my system it is /usr/bin/bash
# 3  
Old 03-22-2017
What error do you get?

I get normal completion.
The cat command displays the scrtipt. the ./t.shl command runs the script
Code:
$ cat t.shl
#!/bin/bash
hello()
{ echo "Executing function hello"
}
echo "Script has started now"
hello
echo "Script will end"
$ ./t.shl
Script has started now
Executing function hello
Script will end

# 4  
Old 03-23-2017
Quote:
Originally Posted by rpiboy
First time doing a function script and I am getting an error. Anyone knows the problem?
As has already been stated: knowing what the error is makes it somewhat easier to tell you the reason. Save for that, you might consider putting an explicit return-command at the end of your functions:

Code:
hello()
{
echo "Executing function hello"

return 0
}

This will give back a return code you can check in the main program and base a reaction on it. Like this:

Code:
#! /bin/bash
myfunc()
{
if [ $1 -eq 1 ] ; then
    return 0
else
    return 1
fi
}

# main()
echo "Script starts"

echo calling myfunc() with 1:
if myfunc 1 ; then
     echo "myfunc 1 returned $?"
fi

echo calling myfunc() with 0:
if myfunc 0 ; then
     echo "myfunc returned $?"
fi

I hope this helps.

bakunin
👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script to pass the config file lines as variable on the respective called function on a script sadique.manzar Shell Programming and Scripting 6 11-16-2017 06:32 AM
Will files, creaetd in one function of the same script will be recognized in another function? digioleg54 Shell Programming and Scripting 1 03-02-2017 10:30 AM
Help with function - is placed in script correctly? SIMMS7400 UNIX for Beginners Questions & Answers 1 11-15-2016 08:48 AM
What is the function of the following lines at the top of a shell script file: Directory and Script? remytom Shell Programming and Scripting 4 05-04-2016 07:49 AM
Shell Script function to use script name for log file output SIMMS7400 Shell Programming and Scripting 11 04-03-2016 06:01 PM
Call shell script function from awk script aishsimplesweet Shell Programming and Scripting 2 08-25-2011 10:42 AM
SHELL SCRIPT Function Calling Another Function Please Help... omkar.sonawane Shell Programming and Scripting 2 04-13-2010 10:20 AM
call function in one script from another script minifish Shell Programming and Scripting 6 10-23-2008 09:04 AM
Function Bug in script - need help gzs553 Shell Programming and Scripting 4 06-05-2007 01:35 AM
random function script prash_b Shell Programming and Scripting 4 09-15-2006 04:54 AM


All times are GMT -4. The time now is 09:16 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?