Python 在bash中获取变量类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29840525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:05:35  来源:igfitidea点击:

Get variable type in bash

pythonbashvariablestypes

提问by tlorin

In Python I can get variable type by:

在 Python 中,我可以通过以下方式获取变量类型:

>>> i = 123
>>> type(i)
<type 'int'>

I saw on this pagethat there are no variable types in bash. The explanation given is:

我在这个页面上看到bash 中没有变量类型。给出的解释是:

Untyped variables are both a blessing and a curse. They permit more flexibility in scripting and make it easier to grind out lines of code (and give you enough rope to hang yourself!). However, they likewise permit subtle errors to creep in and encourage sloppy programming habits.

无类型变量既是福也是祸。它们允许更灵活地编写脚本并使代码行更容易(并为您提供足够的绳索来吊死自己!)。然而,它们同样允许细微的错误潜入并助长草率的编程习惯。

But I'm not sure what it means and what are the real advantages (and drawbacks).

但我不确定这意味着什么以及真正的优点(和缺点)是什么。

采纳答案by cdarke

Bash doesn't have types in the same way as Python (although I would say that Python has classes rather than types). But bash variables do have attributesthat are given (mostly) through declare, but the range of attributes is fairly small. You can find an attribute using declare -p, for example, declare -icreates an integer:

Bash 没有与 Python 相同的类型(尽管我会说 Python 有类而不是类型)。但是bash的变量确实有属性被给予了(大部分)通过declare,但属性的范围是相当小的。declare -p例如,您可以使用declare -i创建一个整数来查找属性:

declare -i num
num=42
declare -p num

Gives:

给出:

declare -i num="42"

But this is a poor feature compared to Python, or almost any modern language. The problem is that in something like Bash the basic type is a text string, and that's fine if all you need is text strings for things like filenames. But once you start needing to do heavy processing you need other types. Bash doesn't support floating point, for example. You also need compoundtypes, like a class describing a file with all the attributes that a file can have.

但与 Python 或几乎任何现代语言相比,这是一个糟糕的功能。问题是在像 Bash 这样的东西中,基本类型是一个文本字符串,如果你只需要文件名之类的文本字符串就可以了。但是一旦您开始需要进行繁重的处理,您就需要其他类型。例如,Bash 不支持浮点数。您还需要复合类型,例如描述具有文件可以具有的所有属性的文件的类。

Bash 4 does have associative arrays(declare -A), similar to Python dictionaries, which extends functionality considerably.

Bash 4 确实有关联数组( declare -A),类似于 Python 字典,它大大扩展了功能。

Even so, most would agree that Object Orientation is pretty much impossible in Bash, although some would argue that it can be done in Korn shell (which has much more powerful features). http://en.wikipedia.org/wiki/Object-oriented_programming

即便如此,大多数人还是同意在 Bash 中面向对象几乎是不可能的,尽管有些人会争辩说它可以在 Korn shell 中完成(它具有更强大的功能)。http://en.wikipedia.org/wiki/Object-oriented_programming

What bash has is fine for what it is meant for - simple processing that is quick and easy to get working. But there is a critical mass beyond which using such a language becomes unwieldy, error prone, and slow. That critical mass can be one of scale, i.e. large amount of data, or complexity.

bash 所具有的功能非常适合它的用途——简单的处理,快速且容易上手。但是有一个临界点,超过这个临界点,使用这种语言变得笨拙、容易出错且速度缓慢。该临界质量可以是规模之一,即大量数据或复杂性。

There is no simple cut-off point where you should stop using Bash and switch to Python. Its just that as programs get more complex and larger the case for using Python gets stronger.

没有简单的分界点让您停止使用 Bash 并切换到 Python。只是随着程序变得越来越复杂和更大,使用 Python 的情况变得更强大。

I should add that shell scripts rarely get smaller and less complex over time!

我应该补充一点,随着时间的推移,shell 脚本很少会变得更小和更简单!