我有一个随机生成密码的脚本,我已将其修改为使用zenity
在GUI中显示。当我使用原始脚本时,我可以生成用户选择的任意长度的随机密码(我用50,000测试)。这是代码:
#!/bin/bash
number=10
echo "hello"
echo "Please enter your number: $number"
read newnumber
# read newnumber
[ -n "$newnumber" ] && number=$newnumber
MATRIX="0123456789<?/_+-!@#$%^&*>ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
# Password will consist of standard characters.
LENGTH=$newnumber
#This variable can be changed for password lenth (need to try get zenity to let user choose that number)
while [ "${n:=1}" -le "$LENGTH" ]
# := is "default substitution" operator.
# So, if 'n' has not been initialized, set it to 1.
do
PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}"
# Very clever, but tricky.
# Starting from the innermost nesting...
# ${#MATRIX} returns length of array MATRIX.
# $RANDOM%${#MATRIX} returns random number between 1
# and [length of MATRIX] - 1.
# ${MATRIX:$(($RANDOM%${#MATRIX})):1}
# returns expansion of MATRIX at random position, by length 1.
# See {var:pos:len} parameter substitution in Chapter 9.
# and the associated examples.
# PASS=... simply pastes this result onto previous PASS (concatenation).
# to let zenity show the password being built one character at a time, uncomment the following line
# zenity --info --text="$PASS"
let n+=1
# Increment 'n' for next pass.
done
echo "$PASS"
(我从[另一个问题] [1]
开始[1]:Using a variable as another variable in a bash script?)
我现在的问题是,当尝试生成更长的密码时,它们不会显示在zenity --info
对话框中,超过30的任何东西都会被击中或遗漏。我通过重复运行脚本并且每次选择60来显示60个字符,但是它不够好被击中或错过,10次中的9次将产生少于20个字符并且在长度越高时越努力它必须生成。但仅限于zenity
当然,非常感谢任何帮助。
答案 0 :(得分:0)
我找到了答案并且它非常简单......字符</@#&*
会导致生成器在与zenity一起使用时崩溃,但是有人知道为什么吗?