我对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
如果用户选择user1
或user2
,脚本将使用
sudo usermod -G DENY_GROUP USER_NAME
或
sudo usermod -G ACCESS_GROUP USER_NAME
和
像这样打印输出:
"User1 is successfully denied or granted"
如果用户选择user3
或user4
,脚本将按如下方式打印输出:
"You don't have a permission to deny the user3 or user4. Please contact your
administrator."
答案 0 :(得分:1)
以下内容可以帮助您入门。
我发现这些是有用的参考资料:
我的答案格式大量借鉴了谷歌外壳风格指南。
#!/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')]: $@" >&2
}
function write_progress_message() {
if [[ "${VERBOSE}" = "true" ]]; then
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&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 "$@"