Call function inside this function with new parameter

Discussion in 'Programming/Scripts' started by Poliman, Dec 21, 2017.

  1. Poliman

    Poliman Member

    I am bash beginner. I have done script which gives ability chose which node server need to be restarted (prod or test):
    Code:
    #!/bin/bash
    
    bold=$(tput bold)
    normal=$(tput sgr0)
    
    echo -e "${bold}\e[104mScript for restaring servers production or testing for our application.\e[49m"
    echo -e "As parameter please provide \e[100mprod\e[49m or \e[100mtest\e[49m.${normal}"
    echo ""
    
    #$SCRIPT_PATH = /root/omnibus.bash"
    
    try_restart()
    {
       if [ $# -eq 0 ]
       then
           echo "No parameters for script."
           echo "Exiting..."
           exit 1 #koniec skryptu
       else
           if [ "$1" = "test" ]
           then
               echo -e "${bold}Jak chcesz poznać logi tego serwera to odpal -> \e[100mpm2 logs server.test\e[49m${normal}"
               echo "############################################################################"
               #cd /var/www/test.s1.ubuntu.net/web/tomni/
               #pm2 stop server.test.js > /dev/null 2>&1 #przekierowanie wyjścia programu do śmieci i ewentualnych błędów operacji na standardowe wyjście z którego i tak wszystko leci do dev/null
               echo -e "\e[31mStopped test server." #-e włącza znaki specjalne i \e[31m to czerwony kolor
               #PORT=3001 pm2 start server.test.js > /dev/null 2>&1
               echo -e "\e[32mStarted test server.\e[39m" #kod koloru zielonego i default
               exit 0
           elif [ "$1" = "prod" ]
           then
               echo "${bold}Jak chcesz poznać logi tego serwera to odpal -> pm2 logs server.prod${normal}"
               echo "############################################################################"
               #cd /var/www/prodapp.net/web/
               #pm2 stop server.prod.js > /dev/null 2>&1
               echo -e "\e[31mStopped prod server."
               #PORT=3009 pm2 start server.prod.js > /dev/null 2>&1
               echo -e "\e[32mStarted prod server.\e[39m"
               exit 0
           else
               echo "I can't recognize parameter"
               echo "We can restart the script, do you want?"
               echo "Say 'yes' to restart script or anything another to leaving."
               read answer
               if [ "$answer" = "tak" ] #mozna sprobowac z while do
               then
                   echo "Below should be calling the function inside which is this whole code."
                   try_restart #here is calling function, but how to call it to provide prod or test as parameter?
    
    
                  # echo Produkcja czy testowy? [prod lub test]"
                  # read x
                  # echo `./plik $x`
                   #uruchom_ponownie
                            #!!!można tez zrobic to wszystko w funkcji i ją tu poniżej wywołać
                            #Poniżej skrypt nie może przyjąć parametru, w związku z tym cały skryptu
                            #nie powinien być odpalany z parametrem, a na początku po pytaniu
                            #odpowiedź powinna być w lokalnej zmiennej i ona porównywana czy test czy prod
                            #exec omnibus.bash" #lub
                            #./omnibus.bash
                            #bash omnibus.bash
                            #$SCRIPT_PATH"
                            #./$SCRIPT_PATH"
               else
                   echo "If you don't want try again, we are finishing."
                   exit 1
               fi
           fi
       fi
    }
    
    Need yours help with reorganizing this. Maybe I should pass parameters to function not to script.
     
    Last edited: Dec 21, 2017

Share This Page