运维联盟俱乐部

 找回密码
 立即注册
查看: 251|回复: 0

shell 3级菜单案例

[复制链接]
  • TA的每日心情
    开心
    2023-8-9 11:05
  • 发表于 2023-12-7 16:08:19 | 显示全部楼层 |阅读模式
    1. #!/bin/bash
    2. bold=$(tput bold)
    3. normal=$(tput sgr0)
    4. PS3="please select a num from menu:"
    5. # #######################################
    6. # Excluded Modules:
    7. # #######################################
    8. EXCLUDED_MODULES="'OGG-USE_OCI_THREAD','OGG-OCI_META_THREAD'"

    9. # #######################################
    10. # Excluded INSTANCES:
    11. # #######################################
    12. # Here you can mention the instances the script will IGNORE and will NOT run against:
    13. # Use pipe "|" as a separator between each instance name.
    14. # e.g. Excluding: -MGMTDB, ASM instances:

    15. EXL_DB="\-MGMTDB|ASM|APX"                           #Excluded INSTANCES [Will not get reported offline].

    16. # ##############################
    17. # SCRIPT ENGINE STARTS FROM HERE ............................................
    18. # ##############################

    19. # #########################
    20. # Check if the OS is Linux: [Disaply the RMAN PAUSE/RESUME command if a backup is currently running]
    21. # #########################
    22. case `uname` in
    23.         Linux ) export SHOW_OS_COMMAND="";;
    24.         *)        export SHOW_OS_COMMAND="--";;
    25. esac

    26. # ###########################
    27. # Listing Available Databases:
    28. # ###########################

    29. # Count Instance Numbers:
    30. INS_COUNT=$( ps -ef|grep pmon|grep -v grep|egrep -v ${EXL_DB}|wc -l )

    31. # Exit if No DBs are running:
    32. if [ $INS_COUNT -eq 0 ]
    33. then
    34.    echo No Database Running !
    35.    exit
    36. fi

    37. # If there is ONLY one DB set it as default without prompt for selection:
    38. if [ $INS_COUNT -eq 1 ]
    39. then
    40.    export ORACLE_SID=$( ps -ef|grep pmon|grep -v grep|egrep -v ${EXL_DB}|awk '{print $NF}'|sed -e 's/ora_pmon_//g'|grep -v sed|grep -v "s///g" )

    41. # If there is more than one DB ASK the user to select:
    42. elif [ $INS_COUNT -gt 1 ]
    43. then
    44.     echo
    45.     echo "Select the ORACLE_SID:[Enter the number]"
    46.     echo ---------------------
    47.     select DB_ID in $( ps -ef|grep pmon|grep -v grep|egrep -v ${EXL_DB}|awk '{print $NF}'|sed -e 's/ora_pmon_//g'|grep -v sed|grep -v "s///g" )
    48.      do
    49.                 integ='^[1-9]+$'
    50.                 if ! [[ ${REPLY} =~ ${integ} ]] || [ ${REPLY} -gt ${INS_COUNT} ]
    51.                         then
    52.                         echo
    53.                         echo "Error: Not a valid number!"
    54.                         echo
    55.                         echo "Enter a valid NUMBER from the displayed list !: i.e. Enter a number from [1 to ${INS_COUNT}]"
    56.                         echo "-----------------------------------------------"
    57.                 else
    58.                         export ORACLE_SID=$DB_ID
    59.                         echo
    60.                         printf "`echo "Selected Instance: ["` `echo -e "\033[33;5m${DB_ID}\033[0m"` `echo "]"`\n"
    61.                                                 sleep 2
    62.                         echo
    63.                         break
    64.                 fi
    65.      done

    66. fi
    67. # Exit if the user selected a Non Listed Number:
    68.         if [ -z "${ORACLE_SID}" ]
    69.          then
    70.           echo "You've Entered An INVALID ORACLE_SID"
    71.           exit
    72.         fi


    73. # #########################
    74. # Getting ORACLE_HOME
    75. # #########################
    76.   ORA_USER=`ps -ef|grep ${ORACLE_SID}|grep pmon|grep -v grep|egrep -v ${EXL_DB}|grep -v "\-MGMTDB"|awk '{print $1}'|tail -1`
    77.   USR_ORA_HOME=`grep -i "^${ORA_USER}:" /etc/passwd| cut -f6 -d ':'|tail -1`

    78. # SETTING ORATAB:
    79. if [ -f /etc/oratab ]
    80.   then
    81. ORATAB=/etc/oratab
    82. export ORATAB
    83. ## If OS is Solaris:
    84. elif [ -f /var/opt/oracle/oratab ]
    85.   then
    86. ORATAB=/var/opt/oracle/oratab
    87. export ORATAB
    88. fi

    89. # ATTEMPT1: Get ORACLE_HOME using pwdx command:
    90. export PGREP=`which pgrep`
    91. export PWDX=`which pwdx`
    92. if [[ -x ${PGREP} ]] && [[ -x ${PWDX} ]]
    93. then
    94. PMON_PID=`pgrep  -lf _pmon_${ORACLE_SID}|awk '{print $1}'`
    95. export PMON_PID
    96. ORACLE_HOME=`pwdx ${PMON_PID}|awk '{print $NF}'|sed -e 's/\/dbs//g'`
    97. export ORACLE_HOME
    98. fi

    99. # ATTEMPT2: If ORACLE_HOME not found get it from oratab file:
    100. if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
    101. then
    102. ## If OS is Linux:
    103. if [ -f /etc/oratab ]
    104.   then
    105. ORATAB=/etc/oratab
    106. ORACLE_HOME=`grep -v '^\#' ${ORATAB} | grep -v '^$'| grep -i "^${ORACLE_SID}:" | perl -lpe'$_ = reverse' | cut -f3 | perl -lpe'$_ = reverse' |cut -f2 -d':'`
    107. export ORACLE_HOME

    108. ## If OS is Solaris:
    109. elif [ -f /var/opt/oracle/oratab ]
    110.   then
    111. ORATAB=/var/opt/oracle/oratab
    112. ORACLE_HOME=`grep -v '^\#' ${ORATAB} | grep -v '^$'| grep -i "^${ORACLE_SID}:" | perl -lpe'$_ = reverse' | cut -f3 | perl -lpe'$_ = reverse' |cut -f2 -d':'`
    113. export ORACLE_HOME
    114. fi
    115. fi

    116. # ATTEMPT3: If ORACLE_HOME is in /etc/oratab, use dbhome command:
    117. if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
    118. then
    119. ORACLE_HOME=`dbhome "${ORACLE_SID}"`
    120. export ORACLE_HOME
    121. fi

    122. # ATTEMPT4: If ORACLE_HOME is still not found, search for the environment variable: [Less accurate]
    123. if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
    124. then
    125. ORACLE_HOME=`env|grep -i ORACLE_HOME|sed -e 's/ORACLE_HOME=//g'`
    126. export ORACLE_HOME
    127. fi

    128. # ATTEMPT5: If ORACLE_HOME is not found in the environment search user's profile: [Less accurate]
    129. if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
    130. then
    131. ORACLE_HOME=`grep -h 'ORACLE_HOME=\/' ${USR_ORA_HOME}/.bash_profile ${USR_ORA_HOME}/.*profile | perl -lpe'$_ = reverse' |cut -f1 -d'=' | perl -lpe'$_ = reverse'|tail -1`
    132. export ORACLE_HOME
    133. fi

    134. # ATTEMPT6: If ORACLE_HOME is still not found, search for orapipe: [Least accurate]
    135. if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
    136. then
    137.         if [ -x /usr/bin/locate ]
    138.          then
    139. ORACLE_HOME=`locate -i orapipe|head -1|sed -e 's/\/bin\/orapipe//g'`
    140. export ORACLE_HOME
    141.         fi
    142. fi

    143. # TERMINATE: If all above attempts failed to get ORACLE_HOME location, EXIT the script:
    144. if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
    145. then
    146.   echo "Please export ORACLE_HOME variable in your .bash_profile file under oracle user home directory in order to get this script to run properly"
    147.   echo "e.g."
    148.   echo "export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1"
    149. exit
    150. fi

    151. # Neutralize login.sql file:
    152. # #########################
    153. # Existance of login.sql file under current working directory eliminates many functions during the execution of this script:

    154.         if [ -f ./login.sql ]
    155.          then
    156. mv ./login.sql   ./login.sql_NeutralizedBy${SCRIPT_NAME}
    157.         fi
    158.                
    159. # CSS LOGO函数
    160. function csslogo {
    161. echo -e "\033[31m*************************************************************************************************************************\033[0m"
    162. echo
    163. echo -e "                                          \033[31m ${bold}ORACLE CSS DATABASE TOOLBOX\033[0m                                            "
    164. echo
    165. echo -e "\033[31m b\033[0m: back to aboove menu                                                                                                  "
    166. echo -e "\033[31m q\033[0m: exit the program                                                                                                     "
    167. echo -e "\033[31m*************************************************************************************************************************\033[0m"
    168. }

    169. # 一级菜单
    170. function menu1() {
    171.     clear
    172.         csslogo
    173.     echo "=== 一级菜单 ==="
    174.     echo "1. 数据库运行状况检查"
    175.         echo "2. 数据库空间状况检查"
    176.         echo "3. 数据库dataguard检查"
    177.         echo "4. 数据库运行状况检查"
    178.         echo "5. 数据库运行状况检查"
    179.         echo "6. 数据库运行状况检查"
    180.     echo "================"
    181.     read -p $'\e\033[33;5m$请输入选项:\e\033[0m' choice

    182.     case $choice in
    183.         1)
    184.             menu21;;
    185.         q)
    186.             exit;;
    187.         *)
    188.             echo "无效的选项!"
    189.             sleep 1
    190.             menu1;;
    191.     esac
    192. }

    193. # 二级菜单
    194. function menu21() {
    195.     clear
    196.         csslogo
    197.     echo "=== 二级菜单 ==="
    198.     echo "1. 进入三级菜单31"
    199.     echo "================"
    200.     read -p $'\e\033[33;5m$请输入选项:\e\033[0m' choice

    201.     case $choice in
    202.         1)
    203.             menu31;;                       
    204.         b)
    205.             menu1;;
    206.         q)
    207.             exit;;
    208.         *)
    209.             echo "无效的选项!"
    210.             sleep 1
    211.             menu2;;
    212.     esac
    213. }

    214. # 三级菜单
    215. function menu31() {
    216.     while true; do
    217.         clear
    218.                 csslogo
    219.         echo "=== 三级菜单 ==="
    220.         echo "1. 执行命令1"
    221.         echo "2. 执行命令2"
    222.         echo "================"
    223.         read -p $'\e\033[33;5m$请输入选项:\e\033[0m' choice

    224.         case $choice in
    225.             1)        echo "commond1 execute successly"
    226.                 # 在这里添加你想要执行的命令,例如:
    227.                 # command
    228.                 read -p "命令执行完毕,按任意键继续..."
    229.                 ;;

    230.             2)        echo "commond2 execute successly"
    231.                 # 在这里添加你想要执行的命令,例如:
    232.                 # command
    233.                 read -p "命令执行完毕,按任意键继续..."
    234.                 ;;
    235.                         b)
    236.                 menu21;;
    237.             q)
    238.                 exit;;
    239.             *)
    240.                 echo "无效的选项!"
    241.                 sleep 1;;
    242.         esac
    243.     done
    244. }

    245. # 主程序入口
    246. menu1
    复制代码


    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    运维联盟俱乐部 ( 冀ICP备19036648号 )

    GMT+8, 2024-5-2 18:56 , Processed in 0.052630 second(s), 21 queries , Gzip On.

    Powered by Discuz! X3.4

    © 2001-2023 Discuz! Team.

    快速回复 返回顶部 返回列表