Calling function, based on agrument passed.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calling function, based on agrument passed.
# 1  
Old 03-04-2018
Calling function, based on agrument passed.

Dears,

Code:
#!/bin/bash
func1()
{
echo "func1"
}

func2()
{
echo "func2"
}

func3()
{
echo "func3"
}

I want to call this function based on argument passed at run time.

like if default:
func1
func2
else
func3

how can i do that? kindly guide

Regards,
sadique
# 2  
Old 03-04-2018
What would "argument passed at run time" look like? And, what does "like if default" mean?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-04-2018
Dear RudiC,

when I should run the script, i want to pass an parameter.
based on the parameter, it should check and run only those function which is pass for that parameter.

---------- Post updated at 06:41 AM ---------- Previous update was at 06:40 AM ----------

like sh test.sh default,
so it should call only func1 and func2
or
sh test.sh xxxxx
then func2 func3 like that.
# 4  
Old 03-04-2018
"Like that" is not too precise a specification. With a vague request like above I'm afraid yo need to be content with fuzzy answers. Like:
Use a case statement to scan through the possible parameter values and execute the functions accordingly.

"Default" usually is the fallback case when nothing is specified; it would be used / executed if NO parameters are given, i.e. $# == 0. You could test that outside the case construct.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 03-04-2018
Code:
#!/bin/bash

func1()
{
echo "func1"
}

func2()
{
echo "func2"
}


func3()
{
echo "func3"
}


if [[ $# == 0 ]]
then
func1
func2
func3
fi

how do i apply case, kindly help.

---------- Post updated at 07:21 AM ---------- Previous update was at 07:10 AM ----------

Thanks RUdiC for the kind help.

I am able to get what i want now.


Code:
#!/bin/bash

func1()
{
echo "func1"
}

func2()
{
echo "func2"
}


func3()
{
echo "func3"
}


if [[ $# == 0 ]]
then
func1
func2
func3
elif [[ -n $1 ]]
then
option=$1
fi
case $option in test)
        func2
        ;;
                check)
        func3
        func1
        ;;
esac

# 6  
Old 03-04-2018
OK, brilliant, that's a start (talking of post#5)! You did not yet specify how those parameter(s) should look like ... one parameter, several characters/numbers?

Did you read bash's man page on the case ... esac (a professional approach, btw)? It would lead you to something like
Code:
case $1 in 
        12)     func1
                func2
                ;;
        13)     func1
                func3
                ;;
        23)     func2
                func3
                ;;
        *)      echo failure / usage
                ;;
esac

EDIT: Cross- Posted, unfortunately.

Last edited by RudiC; 03-04-2018 at 08:32 AM..
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To take file based on date passed

Hi Guys, I have certain files in my directory which gets appended with dates something like this T1_aug17.txt T1_Aug17.txt T1_Sep17.txt config.txt T1 T2 my code: curr_date=`date -d "$date" +%Y-%m-%d` path=mydir for file in `cat config.txt` do final_file=$(ls $path/ | grep -i... (12 Replies)
Discussion started by: rohit_shinez
12 Replies

2. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

3. Shell Programming and Scripting

Function is calling only once

In my prog if i enter the input for the 1st time it is executing correctly, but for the second time entire script is not executing it just exiting my code is #!/bin/sh checkpo() { echo "Checking the entered PO to create output text file "; IFS=$'\n' set -f var=0 for i in $(cat... (3 Replies)
Discussion started by: Padmanabhan
3 Replies

4. UNIX for Advanced & Expert Users

Function not called when no arguments is passed

Hi Guys, I am trying to pass arguments to the script i am wrinting. When no argument is passed or wrong argument is passed, the script needs to output the way it needs to be called and exit. Currently, when no arguments is passed, it is not getting exited but goes on assuming those... (3 Replies)
Discussion started by: mac4rfree
3 Replies

5. 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

6. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

7. Shell Programming and Scripting

count no of arguments passed to a function

hi i have a function abc { //from this function i am passing args to antoher function like def a b c j k l } now i want to count the no of args coming to def() function and iterate over those values is there any way to do this one please help (2 Replies)
Discussion started by: satish@123
2 Replies

8. Shell Programming and Scripting

counting no of argumnts passed to a function

hi i have a function abc() { //from this function i am passing some args to another function def() } def() { //Now i need to calculate the no of input args and iterate through them } please help (4 Replies)
Discussion started by: satish@123
4 Replies

9. Shell Programming and Scripting

How to read values that are passed to the shell function in ksh.

In ksh shell, There is a function f1. function f1 { How to read here?? .... .... } I am passing values to fuunction f1 as f1 "A" "B" Please tell me how to read the passed values in function f1. Advance Thanks & Regards Prashant (2 Replies)
Discussion started by: prashant43
2 Replies

10. UNIX for Dummies Questions & Answers

Calling a function

I have created a file generic.func and it has lots of functions. One of the functions is this: Check_backup_size() { dsmc q b $BACKUP_DIR/"*.Z" | awk '{print $1}'|sed 's///g' > outputfile X=`awk '{sum += $1} END {print sum }' outputfile'` echo "$X" ls -ltr $BACKUP_DIR/"*.Z" | awk... (5 Replies)
Discussion started by: ashika
5 Replies
Login or Register to Ask a Question