diff options
author | Dennis Eriksen <d@ennis.no> | 2023-09-05 13:40:40 +0200 |
---|---|---|
committer | Dennis Eriksen <d@ennis.no> | 2023-09-05 13:40:40 +0200 |
commit | f10cf1a4c542ab4402a1155638d1dec908d15ce6 (patch) | |
tree | e1063d9fad5c75f26d4261eac449f0bde32bf152 | |
parent | adding golang-version (diff) | |
download | makepass-f10cf1a4c542ab4402a1155638d1dec908d15ce6.tar.gz |
fixing some shellcheck-errors and some tiny cleanup on sh- and bash-versions
-rwxr-xr-x | makepass.bash | 13 | ||||
-rwxr-xr-x | makepass.sh | 20 |
2 files changed, 18 insertions, 15 deletions
diff --git a/makepass.bash b/makepass.bash index 04d4f49..c2edf82 100755 --- a/makepass.bash +++ b/makepass.bash @@ -21,19 +21,21 @@ function makepass { MAKEPASS_WORDLIST=${MAKEPASS_WORDLIST:-/usr/share/dict/words} # if $l is not a number, then exit [[ ! $l =~ ^[0-9]+$ ]] && [[ ! "$l" == "" ]] && echo "not a number" && return 1 + (( l <= 1 || l > 255 )) && echo "Argument must be between 0 and 255" && return 1 # if $1 is actually empty, set $l to random value for each output + echo "Normal passwords:" - for i in {1..10}; do + for _ in {1..10}; do [ "$1" = "" ] && l=$(shuf -i 8-44 -n 1) - head -n10 /dev/urandom | tr -dc _A-Z-a-z-0-9 | cut -c-${1:-$l}; + head -n10 /dev/urandom | tr -dc _A-Z-a-z-0-9 | cut -c-"${1:-$l}"; done | column echo "" echo "Passwords with special characters:" - for i in {1..6}; do + for _ in {1..6}; do [ "$1" = "" ] && l=$(shuf -i 16-64 -n 1) first=$(head -n10 /dev/urandom | tr -dc A-Za-z | cut -c-1) - words=$(head -n10 /dev/urandom | tr -dc '!#$%&/()=?+-_,.;:<>[]{}|\@*^A-Z-a-z-0-9' | cut -c-${1:-$l}) + words=$(head -n10 /dev/urandom | tr -dc '!#$%&/()=?+-_,.;:<>[]{}|\@*^A-Z-a-z-0-9' | cut -c-"${1:-$l}") last=$(head -n10 /dev/urandom | tr -dc A-Za-z | cut -c-1) echo "${first}${words}${last}" done | column @@ -42,7 +44,7 @@ function makepass { echo "" echo "Passphrases:" - for i in {1..5}; do + for _ in {1..5}; do words=$(shuf -n 8 "${MAKEPASS_WORDLIST}" | tr '\n' '-' | tr -dc '_A-Z-a-z-0-9') echo "${words:0:-1}" done; @@ -52,4 +54,3 @@ function makepass { makepass "${@:-}" ## END OF FILE ################################################################# -# vim:syntax=bash filetype=bash diff --git a/makepass.sh b/makepass.sh index 3af6830..bb56aef 100755 --- a/makepass.sh +++ b/makepass.sh @@ -21,9 +21,11 @@ makepass() { # We only take one argument [ "$#" -gt 1 ] && printf '%s\n' 'only one argument' && return 1 - # if $1 is not empty and is not a number - if [ ! -z "$1" ] && ! printf '%d' "$1" >/dev/null 2>&1; then printf '%s\n' 'not a number' && return 1; fi - if [ ! -z "$1" ] && [ ! ${1:-0} -gt 0 ]; then printf '%s\n' 'not a number above 0'; return 1; fi + # check if $1 is a number or whatnot + if [ -n "$1" ]; then + if ! printf '%d' "$1" >/dev/null 2>&1; then printf '%s\n' 'not a number' && return 1; fi + if [ "$1" -le 0 ] || [ "$1" -ge 255 ]; then printf '%s\n' 'not a number above 0'; return 1; fi + fi # Go! len=$1 @@ -31,7 +33,7 @@ makepass() { printf '%s\n' 'Normal passwords:' i=0 while [ $i -lt 10 ]; do - _random "${len:-}" '_A-Z-a-z-0-9' true + _random "${len:-}" 'A-Z-a-z-0-9_-' true i=$((i + 1)) done | column printf '\n' @@ -46,7 +48,7 @@ makepass() { if [ -r "${MAKEPASS_WORDLIST}" ]; then printf '\n' printf '%s\n' 'Passphrases:' - lines=$(wc -l < ${MAKEPASS_WORDLIST}) + lines=$(wc -l < "${MAKEPASS_WORDLIST}") i=0 while [ $i -lt 5 ]; do # shuf is the best solution here, but it is very much not portable. @@ -54,7 +56,7 @@ makepass() { words="" j=0 while [ $j -lt 8 ]; do - words="${words}-$(sed -n $(($(_RANDOM) % $lines + 1))p "${MAKEPASS_WORDLIST}" | tr -dc '_A-Z-a-z-0-9')" + words="${words}-$(sed -n $(($(_RANDOM) % lines + 1))p "${MAKEPASS_WORDLIST}" | tr -dc 'A-Z-a-z-0-9_-')" j=$((j + 1)) done printf '%s\n' "${words#-}" @@ -89,15 +91,15 @@ _random() ( fla=${3:-'false'} if [ "$fla" = "true" ]; then - if [ $len -le 2 ]; then - string="$(head -n 10 /dev/urandom | tr -cd '[:alpha:]' | tail -c $len)" + if [ "$len" -le 2 ]; then + string="$(head -n 10 /dev/urandom | tr -cd '[:alpha:]' | tail -c "$len")" else string="$(head -n 10 /dev/urandom | tr -cd '[:alpha:]' | tail -c 1)" string="${string}$(head -n 100 /dev/urandom | tr -cd "$chars" | tail -c $((len-2)))" string="${string}$(head -n 10 /dev/urandom | tr -cd '[:alpha:]' | tail -c 1)" fi else - string="$(head -n 100 /dev/urandom | tr -cd "$chars" | tail -c $len)" + string="$(head -n 100 /dev/urandom | tr -cd "$chars" | tail -c "$len")" fi printf '%s\n' "$string" |