2016-01-13 70 views
1

我對SHELL腳本非常新鮮。我不知道是否有人可以解釋我,我怎樣才能將用戶轉換成願望組。我想什麼 腳本是這樣的:如何爲usermod製作shell腳本

$ ./sample_script 

這將檢查/etc/passwd並列出用戶名這樣

1. user1 
2. user2 
3. user3 
4. user4 

然後:

Enter the username : 1 

然後,腳本會問

What do you want to do (Deny or Grant)? : deny 

如果用戶選擇user1user2,該腳本將使用 sudo usermod -G DENY_GROUP USER_NAMEsudo usermod -G ACCESS_GROUP USER_NAME和 打印這樣的輸出:

"User1 is successfully denied or granted" 

如果用戶選擇user3user4,該腳本將打印輸出是這樣的:

"You don't have a permission to deny the user3 or user4. Please contact your 
administrator." 
+0

初學者有許多shell教程。嘗試使用它們編寫代碼並返回到具有更具體問題的SO。 (堆棧溢出不是代碼編寫器服務。) – Tsyvarev

回答

1

下面應該讓你開始。

,我認爲這是一個有益的借鑑:

Advanced Bash-Scripting Guide

Shell Style Guide

我的答案的格式從谷歌殼牌風格指南大量借鑑。

#!/bin/bash 

CHOSEN_USER="" 
CHOSEN_USER_NAME="" 
CHOSEN_PERMISSION="" 
CHOSEN_GROUP="" 

# Constants 
readonly SUCCESS_EXIT=0 
readonly ERROR_EXIT=64 
readonly PASSWORD_FILE=/etc/passwd 
readonly DENY_INPUT="Deny" 
readonly GRANT_INPUT="Grant" 
readonly DENY_GROUP="DENY_GROUP" 
readonly ACCESS_GROUP="ACCESS_GROUP" 
readonly PERMISSION_LIST="user1,user2" 
readonly SUCCESS_MESSAGE="is successfully denied or granted" 
readonly FAIL_MESSAGE="was not successfully denied or granted" 

function write_error_message() { 
    echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: [email protected]" >&2 
} 

function write_progress_message() { 
    if [[ "${VERBOSE}" = "true" ]]; then 
     echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: [email protected]" >&1 
    fi 
} 

function display_users() { 
    awk -F':' '{ print NR ". " $1; }' $PASSWORD_FILE 
} 

function get_user() { 
    read -p "Enter the username : " CHOSEN_USER 

    write_progress_message "Selected user number: $CHOSEN_USER" 
} 

function get_chosen_user_name() { 
    CHOSEN_USER_NAME=`awk -F':' -v user_num="${CHOSEN_USER}" '{ if (NR == user_num) print $1; }' $PASSWORD_FILE` 
} 

function check_can_set_user_name() { 
    echo "${PERMISSION_LIST}" | grep -q "$CHOSEN_USER_NAME" 

    if [[ $? -ne 0 ]]; then 
     local FAIL_MESSAGE 
     PERM_FAIL_MESSAGE="You don't have a permission to deny the user ${CHOSEN_USER_NAME}. Please contact your administrator." 
     write_error_message "${PERM_FAIL_MESSAGE}" 
     exit "${ERROR_EXIT}" 
    fi 
} 

function get_group_permission() { 
    read -p "What do you want to do (Deny or Grant)? : " CHOSEN_PERMISSION 

    if [[ "${CHOSEN_PERMISSION}" = "${DENY_INPUT}" ]] || [[ "${CHOSEN_PERMISSION}" = "${GRANT_INPUT}" ]]; then 
     if [[ "${CHOSEN_PERMISSION}" = "${DENY_INPUT}" ]]; then 
      CHOSEN_GROUP="${DENY_GROUP}" 
     else 
      CHOSEN_GROUP="${ACCESS_GROUP}" 
     fi 
    else 
     get_group_permission 
    fi 

    write_progress_message "Selected permission: $CHOSEN_PERMISSION" 
} 
function set_group_permission() { 
    local command 
    command="sudo usermod -G ${CHOSEN_GROUP} ${CHOSEN_USER_NAME}" 

    eval "${command}" 

    if [[ $? -eq 0 ]]; then 
     echo "${CHOSEN_USER_NAME} ${SUCCESS_MESSAGE}" 
    else 
     write_error_message "${CHOSEN_USER_NAME} ${FAIL_MESSAGE}" 
     exit "${ERROR_EXIT}" 
    fi 
} 

function print_usage() { 
    echo "NAME" 
    echo "  add_user_to_group.sh - Add user to group" 
    echo 
    echo "SYNOPSIS" 
    echo "  add_user_to_group.sh [OPTIONS]" 
    echo 
    echo "DESCRIPTION" 
    echo "  Display list of users and allow operator to add one to a group" 
    echo 
    echo "OPTIONS" 
    echo "  -v Explain what is being done" 
    echo "  -u display a help message and exit" 
} 

function main() { 
    VERBOSE="false" 

    # read arguments 
    while getopts "vu" flag; do 
     case "${flag}" in 
      v) VERBOSE="true" ;; 
      u) print_usage 
       exit "${SUCCESS_EXIT}" ;; 
      *) exit "${ERROR_EXIT}" ;; 
     esac 
    done 

    readonly VERBOSE 

    display_users 
    get_user 
    get_chosen_user_name 
    check_can_set_user_name 
    get_group_permission 
    set_group_permission 

    exit ${SUCCESS_EXIT} 
} 

main "[email protected]"