commit 433589b86b7db1ce73d6d25f1d58dd88e658ae24 Author: Chris Date: Thu Feb 20 22:01:49 2025 +0200 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..7453b55 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Shell Scripts used on Truenas Scale \ No newline at end of file diff --git a/chm.sh b/chm.sh new file mode 100644 index 0000000..70a9c11 --- /dev/null +++ b/chm.sh @@ -0,0 +1,49 @@ +#!/bin/zsh + +SCRIPTNAME="~/chm.sh" +ALIASNAME="chm" + +_help() { + +echo -n "chm: performs chmod recursively on specified folder with specified values for folder and files. Executable permissions will be set for all folders\n\n" +echo "Usage: $SCRIPTNAME + or: $ALIASNAME +folder : this is the folder on which to execute the command +octalvalue : this is the FILE permissions to be set in the specified folder and its subfolders. Use the same octal value you would use as a chmod argument + +Example: + $SCRIPTNAME ./testfolder 664 ; this will set testfolder and its subfolders to 775 and files in those subfolders to 664 + $SCRIPTNAME ./testfolder 764 ; this will set testfolder and its subfolders to 775 and files in those subfolders to 764 +" +} + +_process() { + folder=$1 + declare -i val=$2 + declare -i rwmask=8#111 + declare -i dirval=$val\|$rwmask + filevalstr=$(printf "%03o" $val) + dirvalstr=$(printf "%03o" $dirval) + echo chmod -R $dirvalstr "'"$folder"'" + find "$folder" -type d -exec chmod $dirvalstr {} \; + echo chmod -R $filevalstr "'"$folder"'" + find "$folder" -type f -exec chmod $filevalstr {} \; +} + +main() { + [[ -z "$1" ]] && _help && return + if [[ $# -lt 2 ]]; then + _help + return + fi + declare -i modval=0 + { # try + let modval=8#$2 && + _process "$1" $modval + } || { # catch + echo 'ERROR: Check if second argument is a proper octal value!' + echo 'If not, fix the value and try again.' + } +} + +main "$@"