How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it�s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line

if [ "$VAL" == "1" ]
then
fun1
elif [ "$VAL" == "2" ]
then
fun2
elif [ "$VAL" == "3" ]
then
fun3
elif [ "$VAL" == "4"]
then
fun4
elif [ "$VAL" ==" 5" ]
then
fun5
elif [ "$VAL" == "6" ]
then
fun6
elif [ "$VAL" == "7" ] then
fun7
else
echo -e "Invalid input.............\n"
Help
  exit
fi

OR

case "$VAL" in
 "1")
    fun1
    ;;
 "2")
    fun2
    ;;
 "3")
    fun3
    ;;
 *)
    echo -e "Invalid input.............\n" 
    ;;
 esac

The first argument specified to the script will be stored in the first positional parameter - $1

How are you populating the value of $VAL?

How to initiate the function :confused:, check how simple functions should work for your choosen inputs

#!/bin/sh
fun1(){
echo " i am in fun-1"
}
fun2(){
echo " i am in fun-2"
}
fun3(){
echo " i am in fun-3"
}
echo "please input your value"
read var
case $var in
1)fun1;;
2)fun2;;
3)fun3;;
*)echo "pleaes input the correct values"
esac
 

my requirement is below :

If i excecute script like below it should take view name as a command line parameter and
execute 2 function of the script .please suggest if logic is not correct .

./script saku 2
 
#!/bin/bash
 
PARAMETER=$#
VIEW_NAME=$1
 
Help()
{
  echo "Usage:"
  echo "./build <view-name> <Val > "
  echo " "
}
 
 
label=cleartool lsstream -fmt "%[found_bls]NXp\n" -view $VIEW_NAME
echo -e "\n"
 
read $Val
 
case $Val in
  "1")
    fun1
    ;;
  "2")
    fun2
    ;;
  "3")
    fun3
    ;;
  *)
    echo -e "Invalid input.............\n"
    Help
    ;;
esac
 
 
fun1()
{
  cleartool diffbl -ver -pred $label
}
fun2()
{
  cleartool diffbl -activities -pred $label
}
fun3()
{
  cleartool diffbl -ele -pred $label
}
 
 
check_parameters()
{
  if [ "$PARAMETER" == "2" ]
  then
    existing_baseline
  elif [ "$PARAMETER" == "1" ]||[ "$VIEW_NAME" == "help" ]
  then
    Help
  else
    Help
  fi
}
 
clear
check_parameters

A quick scan:

  • function existing_baseline is not defined
  • $2 is not assigned to $Val. Instead there is a separate read operation that asks for the user's input
  • The parameters are checked after the functions are called.
  • The assignment to the variable $label is syntactically incorrect.

--
BTW did you read Scott' s warning on code tags?

#!/bin/bash

PARAMETER=$#
VIEW_NAME=$1

Help()
{
echo "Usage:"
echo "./build <view-name> <Val > "
echo "        "
}

check_parameters()
{
if [ "$PARAMETER" == "2" ]
then
existing_baseline
elif [ "$PARAMETER" == "1" ]||[ "$VIEW_NAME" == "help" ]
then
Help
else
Help
fi
}


existing_baseline()
{
label=`cleartool lsstream -fmt "%[found_bls]NXp\n" -view $VIEW_NAME | sed 's/,/\n/g' 
}

fun1()
{
cleartool  diffbl -ver -pred  $label
 }
fun2()
{
cleartool  diffbl -activities -pred $label 
}
fun3()
{
cleartool  diffbl -ele -pred  $label
 }

case $Val in
 "1")
   fun1
    ;;
 "2")
   fun2
    ;;
 "3")
    fun3
   ;;
 *)
    echo -e "Invalid input.............\n" 
	Help
    ;;
 esac
 


clear
check_parameters

i have updated my code still not getting how the script will execute 2 nd function for below command

./script saku 2

I see you addressed the first and last point but not nrs. 2 and 3. Also, $label does now get initialized from "check_parameters" after the case statement for the fun[123] function calls

#!/bin/bash

PARAMETER=$#
VIEW_NAME=$1

Help()
{
echo "Usage:"
echo "./build <view-name> <Val > "
echo "        "
}

check_parameters()
{
if [ "$PARAMETER" == "2" ]
then
existing_baseline
elif [ "$PARAMETER" == "1" ]||[ "$VIEW_NAME" == "help" ]
then
Help
else
Help
fi
}

clear
check_parameters


existing_baseline()
{
label=`cleartool lsstream -fmt "%[found_bls]NXp\n" -view $VIEW_NAME | sed 's/,/\n/g' 
}

fun1()
{
cleartool  diffbl -ver -pred  $label
 }
fun2()
{
cleartool  diffbl -activities -pred $label 
}
fun3()
{
cleartool  diffbl -ele -pred  $label
 }

case $Val in
 "1")
   fun1
    ;;
 "2")
   fun2
    ;;
 "3")
    fun3
   ;;
 *)
    echo -e "Invalid input.............\n" 
	Help
    ;;
 esac
 

I am not sure what need's to be done for 2nd point .

Where do you set $Val ?