在bash中使用/ home /目录从所有用户创建菜单

时间:2019-07-08 14:39:49

标签: linux bash ubuntu

我正在尝试编写bash脚本,以从共享Ubuntu计算机上已安装的浏览器中删除cookie和缓存。我面临的问题是创建一个菜单,您可以在其中选择所有用户或单个用户。

我正在尝试创建一个主菜单,该菜单调用这两个功能(正在进行的工作)中的任何一个以执行任务(我已经注释了同时运行的命令)。

#!/bin/bash

# Remove Browser cache from Ubuntu 16.04 or Ubuntu 18.04

# Check running as root/sudo
if [ "$EUID" -ne 0 ] ;then
echo -e "Please run with;\nsudo $0"
  exit
fi

# Enable extended globbing for the +(...) pattern
shopt -s extglob

## Check Ubuntu version
VERSION=$(lsb_release -d | awk -F":" '/Description/ {print $2}')
if [[ "$VERSION" = *"Ubuntu 18.04"* ]]; then
HOME_DIR="/home/ANT.DOMAIN.COM"
else
[[ "$VERSION" = *"Ubuntu 16.04"* ]]
HOME_DIR="/home/local/ANT"
fi

# Set Colours
RED='\033[1;31m'
YELLOW='\033[1;33m'
GREEN='\033[1;32m'
NC='\033[0m' # No Color

## Clear Browser Cache for ALL Users
clear_cache_all () {
  mapfile -t PROFILES < <(find "$HOME_DIR" -mindepth 1 -maxdepth 1 -type d)
    for PRO in "${PROFILES[@]}"
    do
    # Check FireFox installed
    dpkg -s firefox &> /dev/null
    if [ $? -eq 0 ]; then
      #rm -rf "$PRO"/.mozilla/firefox/*.default/*.sqlite "$PRO"/.mozilla/firefox/*default/sessionstore.js
      #rm -rf "$PRO"/.cache/mozilla/firefox/*.default/*
      echo -e "FireFox Cookies & Cache Cleared for user ${GREEN}$USERNAME${NC}"
      else
      echo -e "${YELLOW}FireFox Not Installed...moving on${NC}"
    fi
    # Check Chromium installed
    dpkg -s chromium-browser &> /dev/null
    if [ $? -eq 0 ]; then
      #rm -rf "$PRO"/.config/chromium/Default/
      #rm -rf "$PRO"/.cache/chromium
      echo -e "Chromium Cookies & Cache Cleared for user ${GREEN}$USERNAME${NC}"
      else
      echo -e "${YELLOW}Chromium Not Installed...moving on${NC}"
    fi
    # Check Chrome installed
    dpkg -s google-chrome-stable &> /dev/null
    if [ $? -eq 0 ]; then
      #rm -rf "$PRO"/.config/google-chrome/Default/
      #rm -rf "$PRO"/.cache/google-chrome
      echo -e "Google Chrome Cookies & Cache Cleared for user ${GREEN}$USERNAME${NC}"
      else
      echo -e "${YELLOW}Google Chrome Not Installed...moving on${NC}"
    fi
    done
}

## Clear Cache for Individual Users
clear_cache_user () {
echo "stuff!"
}

# main menu function
main_menu () {
  clear
    if [ -d "$HOME_DIR" ]
    then
    mapfile -t USERS < <(find "$HOME_DIR" -mindepth 1 -maxdepth 1 -type d)
    # Get basename for users
    USERNAME="${USERS[@]##*/}"
    string="@(${USERNAME[0]}"
    for((i=1;i<${#USERNAME[@]};i++))
    do
      string+="|${USERNAME[$i]}"
    done
    string+=")"
    select NAME in "Clear ALL" "${USERNAME[@]}" "Quit"
    do
        case $NAME in
        "Clear ALL")
            # Call clear_cache_all Function
            clear_cache_all
            exit
            ;;
        $string)
            # Call clear_cache_user Function
            clear_cache_user
            ;;
        "Quit")
            exit
            ;;
            *)
            echo "Invalid option, please try again";;
        esac
    done
    else
      echo -e "${RED}Error: Cannot find home directories...exiting${NC}"
    fi
}

### SCRIPT COMMANDS ###
main_menu

1 个答案:

答案 0 :(得分:1)

好的,所以我可以考虑两种解决您的问题的方法。我将尝试遵循您变量的名称。

正如我在您的代码中看到的那样,您已经将所有用户名都放入了变量“ string”,所以我的第一个想法是使用read和if进行简单操作:

read -P "Insert ALL for all users, the Username for a single user, or Quit to exit: " NAME

if [ $NAME = "ALL" ]
then
    clear_cache_all
    exit
elif [ $NAME = "Quit" ]
then
    echo "Bye!"
    exit
else
    for i in "${string[@]}"
    do
        if [ "$i" == "$NAME" ] ; then
            clear_cache_user($NAME) #Guessing you'll pass the username as a variable to the function
            exit
        fi
    done
    echo "Invalid option, please try again"
fi

另一个选择是使用case语句,就像您以前使用的一样。问题在于,case不适用于数组,因此虽然是“ case / in”,但这并不意味着它正在检查变量是否为数组的元素。如果您被迫使用案例(或喜欢它),请查看以下两个链接以获取一些解决方案:this onethis one

希望这会有所帮助!祝你好运!