使用变量作为一个bash脚本另一个变量? [英] Using a variable as another variable in a bash script?

查看:137
本文介绍了使用变量作为一个bash脚本另一个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还挺新的bash脚本,而不是当谈到写我自己的脚本非常技术性的,我有一个脚本,在终端完美。我想用的zenity使事情变得漂亮和简单和直接的(也可作为一个小的学习项目)。

Hi I'm kinda new to bash scripting and not very technical when it comes to writing my own scripts, I have a script that works perfectly in a terminal. I wanted to use zenity to make things nice and simple and straight forward (but also as a little learning project).

该脚本生成随机密码和的zenity是一个相当不错的小工具。

The script generates random passwords and with zenity is quite a good little tool.

我遇到了一个问题,虽然,运行脚本以及一个GUI,但是当我想介绍供用户选择密码的长度某种程度上,它未能出示密码。
在code,让用户输入自己想要的号码(密码的长度):

I have run into a problem though, the script runs well as a GUI but when I wanted to introduce a way for the user to choose the length of the password it fails to produce the password. The code to let the user input their desired number (length of password) :

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

read newnumber

[ -n "$newnumber" ] && number=$newnumber

其中,如果在终端运行显示终端,但不是在一个盒子的zenity输入的号码。我不能使用变量...

Which, if run in a terminal displays the number entered in the terminal but not in a zenity box. I cannot use the variable...:

number=$newnumber

......后来根据需要像这样的剧本,我从改变一个变量

LENGTH="32"

To :

要:

The script runs normally (except not producing a password) as a GUI, but in the terminal I get (if the user entered the number 25) :

脚本正常运行(除了不产生一个口令),为图形用户界面,但在终端我得到(如果用户输入的号码25):

25 /home/server/Desktop/passwd32gen: line 22: [: : integer expression expected

So it's the fact I've used $newnumberas the value in the LENGTH= variable that has broken the generating part of the script. I have tried various different ways to solve this on my own but know too much, I would assume it will be quite a simple missing piece of syntax (or maybe I just hope so).

所以这是我用事实 $ newnumber 的值length = 变量打破脚本的生成部分。我曾尝试过各种不同的方法来解决这个问题对我自己的,但知道得太多了,我会以为这将是一个非常简单的缺失的那一块语法(或者也许我只是希望如此)。

Now I'm at my wits end trying to figure it out, I've tried

现在我在我无计可施试图算起来,我已经试过

declare

and 

in a number of ways but they just seemed to break the script.

在许多方面,但他们只是似乎破坏脚本

Thankyou in advance to anyone who can help in anyway!

三江源提前任何人谁可以帮助反正在!

And please keep in mind I'm looking for a way to use zenity to allow a user to choose the length of the password being generated.

和请记住,我正在寻找一种方式来使用的zenity允许用户选择所生成的密码长度。

The whole script is :

整个脚本是:


推荐答案

如果你真的想能够显示的特殊字符,你可以使用的zenity --text-信息,如下图所示。这不是因为美观,但它可以做到的。

If you REALLY want to be able to display the special characters, you can use zenity --text-info as shown below. It's not as aesthetically pleasing, but it can be done.

又一个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不喜欢打印变量包含特殊字符。

After playing with it some more, it appears that zenity doesn't like printing variables with special characters.

我做了2变化。

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

1 newnumber=`zenity.... This will read the input from zenity.

2移除了MATRIX一些特殊字符

2 Removed some special characters from the MATRIX

我打上#CHANGED所有更改

I marked all changes with #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

这篇关于使用变量作为一个bash脚本另一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆