Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # +-----------------------------------------------------------------------------------------+
- # | A simple chmod calculator for lazy engineers |
- # | July 2011 flip hess [email protected] |
- # +-----------------------------------------------------------------------------------------+
- # Global variables:
- PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
- SCRIPT_PATH="${0}"
- # Functions:
- # exit function
- function die()
- {
- echo "${1}"
- exit "${2}"
- }
- ################################################################
- # The ask function.
- ################################################################
- function fAsk()
- {
- local SECTION="${1}"
- local ANSWER=""
- local COUNT='1'
- local MAXCOUNT='2'
- echo "#############################################"
- echo "Calculating permissions for ${SECTION}"
- echo "#############################################"
- # --------------------- READ PERMISSIONS --------------------- #
- # while loop for answer:
- local READ='unknown'
- while [ ${READ} = 'unknown' ]
- do
- # exit if max count is reached:
- if [ ${COUNT} -gt ${MAXCOUNT} ]
- then
- die "Too many non-related answers or enters... Exiting script!" "1"
- fi
- # ask for answer
- echo "Dou you want READ permissions for ${SECTION}?"
- echo "Please type Yes or No [Y/N]"
- read ANSWER
- # startover if no answer
- if [ -z ${ANSWER} ]
- then
- COUNT="$(echo $(( ${COUNT} +1)))"
- continue
- fi
- # define yes or no
- if [ ${ANSWER} = YES ] || [ ${ANSWER} = yes ] || [ ${ANSWER} = Y ] || [ ${ANSWER} = y ] || [ ${ANSWER} = Yes ]
- then
- READ='1'
- elif [ ${ANSWER} = 'NO' ] || [ ${ANSWER} = 'no' ] || [ ${ANSWER} = 'N' ] || [ ${ANSWER} = 'n' ] || [ ${ANSWER} = 'No' ]
- then
- READ='0'
- else
- # error message and start over
- echo "Answer not taken, please retry or type CTRL+C for exit"
- COUNT="$(echo $(( ${COUNT} +1)))"
- continue
- fi
- done
- # --------------------- WRITE PERMISSIONS -------------------- #
- # Write permissions
- local WRITE='unknown'
- while [ ${WRITE} = 'unknown' ]
- do
- # exit if max count is reached:
- if [ ${COUNT} -gt ${MAXCOUNT} ]
- then
- die "Too many non-related answers... Exiting script!" "1"
- fi
- # ask for answer
- echo "Dou you want WRITE permissions for ${SECTION}?"
- echo "Please type Yes or No [Y/N]"
- read ANSWER
- # startover if no answer
- if [ -z ${ANSWER} ]
- then
- COUNT="$(echo $(( ${COUNT} +1)))"
- continue
- fi
- # define yes or no
- if [ ${ANSWER} = 'YES' ] || [ ${ANSWER} = 'yes' ] || [ ${ANSWER} = 'Y' ] || [ ${ANSWER} = 'y' ] || [ ${ANSWER} = 'Yes' ]
- then
- WRITE='1'
- elif [ ${ANSWER} = 'NO' ] || [ ${ANSWER} = 'no' ] || [ ${ANSWER} = 'N' ] || [ ${ANSWER} = 'n' ] || [ ${ANSWER} = 'No' ]
- then
- WRITE='0'
- else
- echo "Answer not taken, please retry or type CTRL+C for exit"
- COUNT="$(echo $(( ${COUNT} +1)))"
- continue
- fi
- done
- # --------------------- EXECUTE PERMISSIONS ------------------ #
- # EXECUTE permissions
- local EXECUTE='unknown'
- while [ ${EXECUTE} = 'unknown' ]
- do
- # exit if max count is reached:
- if [ ${COUNT} -gt ${MAXCOUNT} ]
- then
- die "Too many non-related answers... Exiting script!" "1"
- fi
- # ask for answer
- echo "Dou you want EXECUTE permissions for ${SECTION}?"
- echo "Please type Yes or No [Y/N]"
- read ANSWER
- # startover if no answer
- if [ -z ${ANSWER} ]
- then
- COUNT="$(echo $(( ${COUNT} +1)))"
- continue
- fi
- # if answer:
- if [ ${ANSWER} = 'YES' ] || [ ${ANSWER} = 'yes' ] || [ ${ANSWER} = 'Y' ] || [ ${ANSWER} = 'y' ] || [ ${ANSWER} = 'Yes' ]
- then
- EXECUTE='1'
- elif [ ${ANSWER} = 'NO' ] || [ ${ANSWER} = 'no' ] || [ ${ANSWER} = 'N' ] || [ ${ANSWER} = 'n' ] || [ ${ANSWER} = 'No' ]
- then
- EXECUTE='0'
- else
- echo "Answer not taken, please retry or type CTRL+C for exit"
- COUNT="$(echo $(( ${COUNT} +1)))"
- continue
- fi
- done
- # --------------------- PROCESS RESULTS FOR ------------------ #
- # Make results:
- if [ ${READ} = 0 ] && [ ${WRITE} = 0 ] && [ ${EXECUTE} = 0 ]
- then
- RESULT='0'
- elif [ ${READ} = 0 ] && [ ${WRITE} = 0 ] && [ ${EXECUTE} = 1 ]
- then
- RESULT='1'
- elif [ ${READ} = 0 ] && [ ${WRITE} = 1 ] && [ ${EXECUTE} = 0 ]
- then
- RESULT='2'
- elif [ ${READ} = 0 ] && [ ${WRITE} = 1 ] && [ ${EXECUTE} = 1 ]
- then
- RESULT='3'
- elif [ ${READ} = 1 ] && [ ${WRITE} = 0 ] && [ ${EXECUTE} = 0 ]
- then
- RESULT='4'
- elif [ ${READ} = 1 ] && [ ${WRITE} = 0 ] && [ ${EXECUTE} = 1 ]
- then
- RESULT='5'
- elif [ ${READ} = 1 ] && [ ${WRITE} = 1 ] && [ ${EXECUTE} = 0 ]
- then
- RESULT='6'
- elif [ ${READ} = 1 ] && [ ${WRITE} = 1 ] && [ ${EXECUTE} = 1 ]
- then
- RESULT='7'
- else
- die "Something went wrong processing results, exiting ${SCRIPT_PATH}!" "1"
- fi
- # --------------------- EXPORT VARIABLES ------------------ #
- # export var
- if [ ${SECTION} = OWNER ]
- then
- export OWNER="${RESULT}"
- elif [ ${SECTION} = GROUP ]
- then
- export GROUP="${RESULT}"
- elif [ ${SECTION} = OTHER ]
- then
- export OTHER="${RESULT}"
- else
- die "Wronk results!" "1"
- fi
- # clear screen
- clear
- return 0
- }
- ################################################################
- # processes results
- ################################################################
- function fResult()
- {
- for SECTION in OWNER GROUP OTHER
- do
- # run function with name
- fAsk "${SECTION}"
- done
- echo "###############################"
- echo
- echo "File permissions are: ${OWNER}${GROUP}${OTHER} "
- echo
- echo "###############################"
- return 0
- }
- ################################################################
- # Shows usage.
- ################################################################
- function fShowUsage()
- {
- echo "Usage: ${SCRIPT_PATH}"
- return 0
- }
- # Start the program:
- fResult "${@}"
- # Exit with previous return code:
- exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment