Function Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Function Script
# 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 or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

2. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

3. Shell Programming and Scripting

What is the function of the following lines at the top of a shell script file: Directory and Script?

The file starts like this: Directory: <path to the script> Script: <script fife name> #!bin/ksh ##Comments <actual script> What is the use of the first two lines in the script? What if I save the file without them? What will be the effect? They are not comments. Im very new to this,... (4 Replies)
Discussion started by: remytom
4 Replies

4. Shell Programming and Scripting

Shell Script function to use script name for log file output

Hi Team - I"m very new to Shell Scripting so I have a rather novice question. My forte is Windows Batch Scripting so I was just wondering what the Shell Script equivalent is to the DOS command %~n? %~n is a DOS variable that dispayed the script name. For instance (in DOS): REM... (11 Replies)
Discussion started by: SIMMS7400
11 Replies

5. Shell Programming and Scripting

Help for exiting the function not script

function2() { cmd1 cmd2 cmd3 .... cmdn } function2() { cmd11 cmd12 cmd13 .... .... } for i in {1,2} (7 Replies)
Discussion started by: yanglei_fage
7 Replies

6. Shell Programming and Scripting

SH Script for Logical OR function

Using good ole fashion bourne shell scripting I have to take 2 files and compare them 1:1 in a Logical OR "gate" or function and then output the info into a 3rd file. I have never tried anything like this before :eek: so any hand holding will be much "Thanked" OR Gate being: A, B, Result 0,... (2 Replies)
Discussion started by: Alivadoro
2 Replies

7. Shell Programming and Scripting

Call shell script function from awk script

hi everyone i am trying to do this bash> cat abc.sh deepak() { echo Deepak } deepak bash>./abc.sh Deepak so it is giving me write simply i created a func and it worked now i modified it like this way bash> cat abc.sh (2 Replies)
Discussion started by: aishsimplesweet
2 Replies

8. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

9. Shell Programming and Scripting

call function in one script from another script

Hello experts, Is it possible to call function in one script from another script? Example. Script 1: #!/bin/bash function1(){ } Script 2: #!/bin/bash #code to call function1 in Script 1 :confused: thanks you (6 Replies)
Discussion started by: minifish
6 Replies

10. Shell Programming and Scripting

Function Bug in script - need help

My script is erroring with: testtapemgr.sh: FTP_RETURNS: not found I cannot see what I am doing wrong..when it calls that function from the Volume returns function and says taht FTP_RETURNS is not found and exits out of the script. What am I not seeing here? #### Return Volume Function ... (4 Replies)
Discussion started by: gzs553
4 Replies
Login or Register to Ask a Question