嗨,我有点新手来编写脚本,而且在编写我自己的脚本方面并不是很技术,我有一个在终端中完美运行的脚本。我想使用zenity使事情变得美观,简单和直接(但也作为一个小小的学习项目)。
该脚本生成随机密码,而zenity是一个非常好的小工具。
我遇到了一个问题,该脚本作为GUI运行良好但是当我想为用户介绍一种选择密码长度的方法时,它无法生成密码。 让用户输入所需号码(密码长度)的代码:
number=32
zenity --entry --text="Please enter a number (no limitations!) :" --entry-text="$number"
read newnumber
[ -n "$newnumber" ] && number=$newnumber
如果在终端中运行,则会显示在终端中输入的号码,但不会显示在zenity框中。我不能使用变量......:
number=$newnumber
...稍后在脚本中如此需要,我改变了一个变量:
LENGTH="32"
致:
LENGTH="$newnumber"
脚本作为GUI正常运行(除了不生成密码),但在终端中我得到(如果用户输入了数字25):
25
/home/server/Desktop/passwd32gen: line 22: [: : integer expression expected
因此,我使用$newnumber
作为LENGTH=
变量中已破坏脚本生成部分的值的事实。我已经尝试了各种不同的方法来解决这个问题,但是我知道它会是一个非常简单的缺失语法(或者我希望如此)。
现在我在我的智慧结束时试图弄明白,我已经尝试了
declare
和
eval
在许多方面,但他们似乎打破了剧本。
提前感谢任何可以提供帮助的人!
请记住,我正在寻找一种方法来使用zenity来允许用户选择生成密码的长度。
整个脚本是:
#!/bin/bash
# May need to be invoked with #!/bin/bash2 on older machines.
#
#Random 32 character password generator
#
zenity --info --title="32 Character Password Generator" --text="Hi, so you want to get yourself a new password? You've the perfect little application here, just click OK to generate your new password."
number=32
zenity --entry --text="Please enter a number (no limitations!) :" --entry-text="$number"
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
zenity --info --title="Your 32 character password" --text="Here is your random 32 character password, you can copy and paste it wherever you wish...
$PASS
The passwords generated by this application are very strong, here are the numbers;
Length: 32 characters
Character Combinations: 96
Calculations Per Second: 4 billion
Possible Combinations: 2 vigintillion
Based on an average Desktop PC making about 4 Billion calculations per second
It would take about 21 quattuordecillion years to crack your password.
As a number that's 21,454,815,022,336,020,000,000,000,000,000,000,000,000,000,000 years!" # you could redirect to a file, to store the password. Use something like $PASS 2> /file/name
exit 0
答案 0 :(得分:2)
zenity命令后面的read
命令不会从 zenity读取 - 它仍然会一直从stdin读取。
相反,你可能想要:
newnumber=$(zenity --entry \
--text="Please enter a number (no limitations!) :" \
--entry-text="$number")
...不需要执行read
命令。
那就是说,如果 因某种原因想要使用read
,你仍然可以这样做:
read -r newnumber < <(zenity --entry \
--text="Please enter a number (no limitations!) :" \
--entry-text="$number")
答案 1 :(得分:0)
如果您真的希望能够显示特殊字符,可以使用zenity --text-info,如下所示。这不是美学上令人愉悦的,但可以做到。
另外2¢
echo "Here is your random $newnumber character password, you can copy and paste it wherever you wish...
$PASS
The passwords generated by this application are very strong, here are the numbers;
Length: $newnumber characters
Character Combinations: 96
Calculations Per Second: 4 billion
Possible Combinations: 2 vigintillion
Based on an average Desktop PC making about 4 Billion calculations per second
It would take about 21 quattuordecillion years to crack your password.
As a number that's 21,454,815,022,336,020,000,000,000,000,000,000,000,000,000,000 years!" | zenity --text-info --title "Your $newnumber character password" --width 600 --height 500`
附录,
再玩一遍之后,似乎zenity不喜欢用特殊字符打印变量。
我做了2次更改。
1 newnumber =`zenity ....这将读取zenity的输入。
2从MATRIX中删除了一些特殊字符
我用#CHANGED
标记了所有更改这是经过修改的脚本。
#!/bin/bash
# May need to be invoked with #!/bin/bash2 on older machines.
#
#Random 32 character password generator
#
zenity --info --title="32 Character Password Generator" --text="Hi, so you want to get yourself a new password? You've the perfect little application here, just click OK to generate your new password."
number=32
# CHANGED
newnumber=`zenity --entry --text="Please enter a number (no limitations!) :" --entry-text="$number"`
# read newnumber
[ -n "$newnumber" ] && number=$newnumber
#CHANGED Removed offending special characters
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
# CHANGED $PASS to '$PASS' below
zenity --info --title="Your 32 character password" --text="Here is your random 32 character password, you can copy and paste it wherever you wish...
$PASS
The passwords generated by this application are very strong, here are the numbers;
Length: 32 characters
Character Combinations: 96
Calculations Per Second: 4 billion
Possible Combinations: 2 vigintillion
Based on an average Desktop PC making about 4 Billion calculations per second
It would take about 21 quattuordecillion years to crack your password.
As a number that's 21,454,815,022,336,020,000,000,000,000,000,000,000,000,000,000 years!" # you could redirect to a file, to store the password. Use something like $PASS 2> /file/name
exit 0