0

嗨,我对 bash 脚本编写有点陌生,并且在编写自己的脚本时不是很技术,我有一个可以在终端中完美运行的脚本。我想用 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
4

2 回答 2

2

zenityread命令后面的命令不会zenity 读取——它仍然像往常一样从标准输入读取。

相反,您可能想要:

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")
于 2014-08-26T14:34:45.667 回答
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
于 2014-08-26T18:07:52.310 回答