LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   issue with function or call to function (https://www.linuxquestions.org/questions/linux-newbie-8/issue-with-function-or-call-to-function-4175580614/)

sysmicuser 05-24-2016 08:38 PM

issue with function or call to function
 
Hey Guys,

Here is my script and its output. Having issue with it. Any guidance would be much appreciated !

Code:

set -x
export Blackout_Group=$1
export Duration=$2
export Change_INC_Number=$3

export conversion_value=60
export calc_in_minutes=$(echo "${Duration}"*"${conversion_value}"|bc|python -c "print int(round(float(raw_input())))")
echo ${calc_in_minutes}

LOG_FILE=${LOGS_DIR}/$(date +"%d-%^b-%Y-%H%M%S")_${Blackout_Group}_${calc_in_minutes}mins.log


case ${Blackout_Group} in
CCA)
        set OEM_Blackout_Group="CCB Asset Blackout"
        create_blackout(${OEM_Blackout_Group},${calc_in_minutes},${Change_INC_Number})
        ;;
CCR)
        set OEM_Blackout_group="CCB Retail Blackout"
        create_blackout(${OEM_Blackout_Group},${calc_in_minutes},${Change_INC_number})
        ;;
FUSION)
        set OEM_Blackout_Group="FUSION"
        create_blackout(${OEM_Blackout_Group},${calc_in_minutes},${Change_INC_number})
        ;;
Reporting)
        set OEM_Blackout_Group="Reporting Blackout"
        create_blackout(${OEM_Blackout_Group},${calc_in_minutes},${Change_INC_number})
        ;;
PM)
        set OEM_Blackout_Group="SOA Service Manager Blackout"
        create_blackout(${OEM_Blackout_Group},${calc_in_minutes},${Change_INC_number})
        ;;
ALL)
        set OEM_Blackout_Group="Blackout Entire PRD02 except DBs"
        create_blackout(${OEM_Blackout_Group},${calc_in_minutes},${Change_INC_number})
        ;;
esac

exit 0

create_blackout(${OEM_Blackout_Group},${calc_in_minutes},${Change_INC_Number})
{

emcli create_blackout -add_targets="${OEM_Blackout_Group}" -name="${Change_INC_Number}" -reason="Apps: Application Patch/Maintenance" -schedule="frequency:once;duration::"${calc_in_minutes};tzinfo:specified;tzregion:Australia/Victoria" -jobs_allowed

}
sh-4.1$

Output:
Code:

sh-4.1$ !.
./test2.sh CCA 0.1 12345
++ export Blackout_Group=CCA
++ Blackout_Group=CCA
++ export Duration=0.1
++ Duration=0.1
++ export Change_INC_Number=12345
++ Change_INC_Number=12345
++ export conversion_value=60
++ conversion_value=60
+++ echo '0.1*60'
+++ bc
+++ python -c 'print int(round(float(raw_input())))'
++ export calc_in_minutes=6
++ calc_in_minutes=6
++ echo 6
6
+++ date '+%d-%^b-%Y-%H%M%S'
++ LOG_FILE=/25-MAY-2016-111043_CCA_6mins.log
./test2.sh: line 16: syntax error near unexpected token `${OEM_Blackout_Group},${calc_in_minutes},${Change_INC_Number}'
./test2.sh: line 16: `  create_blackout(${OEM_Blackout_Group},${calc_in_minutes},${Change_INC_Number})'
sh-4.1

Any idea what is going wrong here ?

Jim

dunne 05-24-2016 09:28 PM


Is this supposed to be a shell function?
Code:

create_blackout(${OEM_Blackout_Group},${calc_in_minutes},${Change_INC_Number})
{

emcli create_blackout -add_targets="${OEM_Blackout_Group}"
-name="${Change_INC_Number}" -
reason="Apps: Application Patch/Maintenance"
-schedule="frequency:once;duration::"${calc_
in_minutes};tzinfo:specified;tzregion:Australia/Victoria" -jobs_allowed

}

I ask because that's not the right syntax. So, unless I am missing something:
1) Put the definition before it is called, and write it like so:
Code:

create_blackout()
{
...
}

2) You call it like so:
Code:

create_blackout $OEM_Blackout_Group $calc_in_minutes $Change_INC_Number
3) And in the function, you refer to the arguments using positional parameters:
first arg passed in is $1, second is $2, and so on.

sysmicuser 05-24-2016 09:33 PM

Yes it is a shell function. How can I call it and how to pass arguments while calling? As I would need the variables when i call and pass three parameters
can you guide me for that?

Turbocapitalist 05-24-2016 09:47 PM

It seems to be missing the first line to declare which script interpreter it needs. It could be this?

Code:

#!/bin/sh
  • The the ^b probably should be b
  • Functions have to be declared before use
  • Function parameters get passed just like with a regular program, as arguments and not inside parenthesis.
  • There is an unclosed double quote somewhere.
  • The case statement ought to have a catch-all default in case the wrong or no parameters are passed.

sysmicuser 06-02-2016 01:04 AM

Thank you very much all of you !! Issue is resolved.

grail 06-02-2016 09:25 AM

Please remember to mark question as SOLVED.

I would add that there is entirely to little quoting used around variables and not sure why you needed both bc and python to perform a calculation, surely only one is needed.
I was also curious which shell you are using as the use of 'set' for a variable is not a bashism and also why the use of export?


All times are GMT -5. The time now is 04:43 PM.