LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-15-2011, 12:36 PM   #1
nesrin
LQ Newbie
 
Registered: Nov 2010
Posts: 15

Rep: Reputation: 0
skip execution of a function in a script.


Hello,

I have a bash script which is composed of different functions. I want to able to
select which functions to be executed when i run the script, for that i thought about using a flag in the function. but i dont know how to skip the function whose flag is set to zero. any idea? or any other way to do this?
I would appreciate much any help

Code:
myfunc1(){
local flag=1
commands
{
func

myfunc2(){
local flag=1
commands
{
func

myfunc3(){
local flag=0
commands
{
func
 
Old 03-15-2011, 12:49 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693
From what I understand, a function will only run if you call it. So, some decent if-then sections, or a 'case' section, and I'm sure there is something that can be done.

Code:
function one (){
commands }
function two (){
commands }
function three (){
commands }

one=0
two=0
three=1

if [ $one -ne 0 ]
then
 one;
fi

if [ $two -ne 0 ]
then
 two;
fi

if [ $three -ne 0 ]
then
 three;
fi

Last edited by szboardstretcher; 03-15-2011 at 12:54 PM.
 
Old 03-15-2011, 01:19 PM   #3
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112
As said before, you can choose to run particular functions, but one thing I haven't seen yet is how you pass which functions you want to run into the script. This is where command-line arguments come in. Taking the previous example and expanding on it...

Code:
function one (){
commands }
function two (){
commands }
function three (){
commands }


case "$1" in
  1)
        one
        ;;
  2)
        two
        ;;
  3)
        three
        ;;
  *)
        echo "Usage: /path/script.sh {1|2|3}"
        exit 1
esac
If there are cases where you might want to run function 1 and 2, but not 3, you can build those cases into the script as well. For example, I have some scripts to start and stop applications, and I also have a "restart" case that does both.
 
  


Reply

Tags
shell script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: skip files bigger than xGB laurens Linux - Newbie 3 10-30-2009 12:23 PM
Shell script next/skip/do nothing knockout_artist Linux - Newbie 6 11-04-2007 12:45 AM
calcualte the execution time of a function George2 Programming 4 06-15-2006 03:13 AM
recursive execution of function in subdirectories Melsync Programming 3 01-17-2006 06:13 AM
Timed execution of a function arvind_tyche Programming 1 05-07-2004 08:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration