在bash脚本中使用一个变量作为另一个变量



嗨,我对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

zenity命令后面的read命令不会从zenity读取,它仍然像往常一样从stdin读取。

相反,你可能想要:

newnumber=$(zenity --entry 
  --text="Please enter a number (no limitations!) :" 
  --entry-text="$number")

不需要以下CCD_ 4命令。

也就是说,如果你确实出于某种原因想要使用read,你仍然可以这样做:

read -r newnumber < <(zenity --entry 
  --text="Please enter a number (no limitations!) :" 
  --entry-text="$number")

如果你真的想显示特殊字符,你可以使用zenity——文本信息,如下所示。它在美学上不那么令人愉悦,但它是可以做到的。

只需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似乎不喜欢打印带有特殊字符的变量。

这个脚本应该可以工作

我做了两个改动。

1 newnumber=` zenity。。。。这将读取zenity的输入。

2从矩阵中删除了一些特殊字符

我用#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

相关内容

  • 没有找到相关文章

最新更新