mirror of
https://github.com/chubin/cheat.sh.git
synced 2025-01-06 04:16:04 +03:00
commit
83d30141bc
@ -140,3 +140,28 @@ class AdapterOeis(CommandAdapter):
|
||||
|
||||
def is_found(self, topic):
|
||||
return True
|
||||
|
||||
class AdapterChmod(CommandAdapter):
|
||||
"""
|
||||
Show chmod numeric values and strings
|
||||
Exported as: "/chmod/NUMBER"
|
||||
"""
|
||||
|
||||
_adapter_name = "chmod"
|
||||
_output_format = "text"
|
||||
_cache_needed = True
|
||||
_command = ["share/adapters/chmod.sh"]
|
||||
|
||||
def _get_command(self, topic, request_options=None):
|
||||
cmd = self._command[:]
|
||||
if not cmd[0].startswith("/"):
|
||||
cmd[0] = _get_abspath(cmd[0])
|
||||
|
||||
# cut chmod/ off
|
||||
if topic.startswith("chmod/"):
|
||||
topic = topic[4:]
|
||||
|
||||
return cmd + [topic]
|
||||
|
||||
def is_found(self, topic):
|
||||
return True
|
||||
|
@ -86,6 +86,7 @@ _CONFIG = {
|
||||
"learnxiny",
|
||||
"rfc",
|
||||
"oeis",
|
||||
"chmod",
|
||||
],
|
||||
"adapters.mandatory": [
|
||||
"search",
|
||||
@ -116,6 +117,7 @@ _CONFIG = {
|
||||
("^[^/]*/rosetta(/|$)", "rosetta"),
|
||||
("^rfc/", "rfc"),
|
||||
("^oeis/", "oeis"),
|
||||
("^chmod/", "chmod"),
|
||||
("^:", "internal"),
|
||||
("/:list$", "internal"),
|
||||
("/$", "cheat.sheets dir"),
|
||||
|
56
share/adapters/chmod.sh
Normal file
56
share/adapters/chmod.sh
Normal file
@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Contributed by Erez Binyamin (github.com/ErezBinyamin)
|
||||
|
||||
# Translate between chmod string and number
|
||||
# Contrib to chubin - cheat.sh
|
||||
chmod_calc(){
|
||||
p_s=""
|
||||
p_n=""
|
||||
R=()
|
||||
W=()
|
||||
X=()
|
||||
# If permission number is given calc string
|
||||
if [[ $1 =~ ^-?[0-9]+$ ]]
|
||||
then
|
||||
p_n=$1
|
||||
for (( i=0; i<${#1}; i++ ))
|
||||
do
|
||||
num=$(echo "obase=2;${1:$i:1}" | bc | xargs printf '%03d')
|
||||
[ ${num:0:1} -eq 1 ] && p_s+='r' || p_s+='-'
|
||||
[ ${num:1:1} -eq 1 ] && p_s+='w' || p_s+='-'
|
||||
[ ${num:2:1} -eq 1 ] && p_s+='x' || p_s+='-'
|
||||
[ ${num:0:1} -eq 1 ] && R+=('X') || R+=(' ')
|
||||
[ ${num:1:1} -eq 1 ] && W+=('X') || W+=(' ')
|
||||
[ ${num:2:1} -eq 1 ] && X+=('X') || X+=(' ')
|
||||
done
|
||||
# If permission string is given calc number
|
||||
else
|
||||
p_s=$1
|
||||
for (( i=0; i<${#1}; i+=0 ))
|
||||
do
|
||||
num=0
|
||||
[[ ${1:$i:1} == 'r' ]] && R+=('X') || R+=(' ')
|
||||
[[ ${1:$((i++)):1} == 'r' ]] && let num++
|
||||
num=$(( num << 1 ))
|
||||
[[ ${1:$i:1} == 'w' ]] && W+=('X') || W+=(' ')
|
||||
[[ ${1:$((i++)):1} == 'w' ]] && let num++
|
||||
num=$(( num << 1 ))
|
||||
[[ ${1:$i:1} == 'x' ]] && X+=('X') || X+=(' ')
|
||||
[[ ${1:$((i++)):1} == 'x' ]] && let num++
|
||||
p_n+="$num"
|
||||
done
|
||||
fi
|
||||
printf "
|
||||
Linux Permissions String:\t${p_s}
|
||||
Linux Permissions Number:\t${p_n}
|
||||
|
||||
Owner\t\tGroup\t\tPublic
|
||||
|
||||
Read [${R[0]}]\tRead [${R[1]}]\tRead [${R[2]}]
|
||||
Write [${W[0]}]\tWrite [${W[1]}]\tWrite [${W[2]}]
|
||||
Execute [${X[0]}]\tExecute [${X[1]}]\tExecute [${X[2]}]
|
||||
"
|
||||
}
|
||||
|
||||
chmod_calc $@
|
Loading…
Reference in New Issue
Block a user