基于菜单的Shell脚本问题

时间:2016-01-14 05:07:42

标签: bash shell unix

我是shellcript的新蜜蜂。我的剧本中有一些问题,请不要做什么,请帮助我。

请参阅我的代码并建议如何取消函数main从头开始执行。

function trap_ctrlc ()
{
   echo
   echo
   echo -n " You pressed CTRL+C, Exiting. Please wait.. "
   echo
   echo
   sleep 1
   exit 
}

function user ()
{
   echo -e "Enter Your Name : \c";
   trap "trap_ctrlc" 2
   read usrname
   if [ "$usrname" == 0 ];
   then
      main
   fi
   echo -e "Enter Your Employee-id : \c";
   trap "trap_ctrlc" 2
   read empid
   if [ "$empid" == 0 ];
   then
      main
   fi
   if [ "$usrname" = "" ] || [ "$empid" = "" ];
   then
       echo " Please enter your name & empid."
       user
   fi

}

function main ()
{
  clear
  while true
  do
     echo "MENU" | awk '{
     printf("\n\n");
     printf("=======================\n");
     printf("|%18s|\n","  M A I N - M E N U  ");
     printf("=======================\n");
     printf("|%3s|%17s|\n"," 0 "," Main Menu     ");
     printf("|%3s|%17s|\n"," 1 "," OS Check      ");
     printf("|%3s|%17s|\n"," 2 "," Exit          ");
     printf("=======================\n");
}'

echo -n "Enter your choice [0-3] : "
read option 
case "$option" in
1)
    user
    trap "trap_ctrlc" 2
    read -r -p "Are you sure Login [y/N] or Enter 0 for main menu ??? " response
    if [ "$response" == 0 ];
    then

       main

    fi
    if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]
    then
        echo "**************************" >> $LOG_FILE
        echo "Date : $date_time " >> $LOG_FILE
        echo "OS User : $usr " >> $LOG_FILE
        echo "Login User : $usrname " >> $LOG_FILE
        echo "Emp-Id : $empid " >> $LOG_FILE
        echo "**************************" >> $LOG_FILE
        echo -e "\n" >> $LOG_FILE
        echo -e "\n\n\n"
        break;
    else
        echo "Exiting, Please wait."
        sleep 2
        exit
    fi 
    ;;
2)
    clear
    exit 0;
    ;;
0)
    main
    ;;

*)
    echo "Wrong Options...!!![Try 1, 2 or 3.]"
    echo -n "Enter Options. . .: "                                                      
    read
    clear   
    ;; 
esac
done
}

trap "trap_ctrlc" 2
main    

0 个答案:

没有答案