Results 1 to 4 of 4
Dear All,
I need a help regarding writing a function in a shell, what exactly a function does!!
i am new to scripting so little bit confused!!
Regards
Keshari...
- 09-22-2010 #1Just Joined!
- Join Date
- Sep 2010
- Posts
- 3
Help needed to write a function in a shell script!!
Dear All,
I need a help regarding writing a function in a shell, what exactly a function does!!
i am new to scripting so little bit confused!!
Regards
Keshari
- 09-22-2010 #2
Welcome to the forums!
Basic function form is this:
But you can find a wealth of information in the Advanced Bash-Scripting GuideCode:FunctionName() { command1 command2 command3 }Can't tell an OS by it's GUI
- 09-22-2010 #3Just Joined!
- Join Date
- Sep 2010
- Posts
- 3
RE:Help needed to write a function in a shell script!!
Hi,
Thanks a lot for your reply.
Just need one clarification:--
If i want to write some commands like "grep" "cat".. in a function and i want to call that function, then how i can do that, if possible can you give me one example..
Regards
Keshari
- 09-22-2010 #4
Ok, an example

As you see, you call a function as if it where a command. And it'll execute whatever is between the brackets { }Code:#!/bin/bash ExampleFunction() { # Remember, this is just an example # Most direct: echo "First: You've used 'ls' `grep -c ls ~/.bash_history` times" # Adding variable: count=`grep -c ls ~/.bash_history` echo "Second: You've used 'ls' $count times" # Using a pipe: count=`cat ~/.bash_history | grep -c ls` echo "Third: You've used 'ls' $count times" # Added whitespace for readability: count=` cat ~/.bash_history |\ grep ls |\ wc -l ` echo "Fourth: You've used 'ls' $count times" } echo "Calling function:" ExampleFunctionCan't tell an OS by it's GUI


Reply With Quote