Unix/Linux Go Back    


Shell Programming and Scripting 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

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-22-2017   -   Original Discussion by rpiboy
rpiboy's Unix or Linux Image
rpiboy rpiboy is offline
Registered User
 
Join Date: Mar 2017
Last Activity: 22 March 2017, 7:44 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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"

Sponsored Links
    #2  
Old Unix and Linux 03-22-2017   -   Original Discussion by rpiboy
migurus's Unix or Linux Image
migurus migurus is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 12 December 2017, 9:36 PM EST
Location: US
Posts: 296
Thanks: 58
Thanked 33 Times in 31 Posts
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
Sponsored Links
    #3  
Old Unix and Linux 03-22-2017   -   Original Discussion by rpiboy
jim mcnamara's Unix or Linux Image
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 15 January 2018, 9:34 AM EST
Location: NM
Posts: 11,284
Thanks: 581
Thanked 1,127 Times in 1,038 Posts
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 Unix and Linux 03-23-2017   -   Original Discussion by rpiboy
bakunin's Unix or Linux Image
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 21 January 2018, 4:46 PM EST
Location: In the leftmost byte of /dev/kmem
Posts: 5,678
Thanks: 112
Thanked 1,633 Times in 1,198 Posts
Quote:
Originally Posted by rpiboy View Post
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
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
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 11:30 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 08:49 AM
Shell Script function to use script name for log file output SIMMS7400 Shell Programming and Scripting 11 04-03-2016 07:01 PM
Call shell script function from awk script aishsimplesweet Shell Programming and Scripting 2 08-25-2011 11:42 AM
SHELL SCRIPT Function Calling Another Function Please Help... omkar.sonawane Shell Programming and Scripting 2 04-13-2010 11:20 AM



All times are GMT -4. The time now is 08:39 PM.