在bash中获取变量类型 [英] Get variable type in bash

查看:84
本文介绍了在bash中获取变量类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Python I can get variable type by:

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

我在此页面上看到,bash中没有变量类型.给出的解释是:

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

无类型变量既是福也是祸.它们在脚本编写方面提供了更大的灵活性,并使其更容易磨合代码行(并为您提供了足够的绳索来吊死自己!).但是,它们同样允许微妙的错误蔓延并鼓励草率的编程习惯.

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).

推荐答案

Bash的类型与Python不同(尽管我会说Python具有类而不是类型).但是bash变量确实具有属性(大多是通过 declare 给出的),但是属性的范围很小.您可以使用 declare -p 查找属性,例如, declare -i 创建一个整数:

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 attributes that are given (mostly) through declare, but the range of attributes is fairly small. You can find an attribute using declare -p, for example, declare -i creates an integer:

declare -i num
num=42
declare -p num

赠予:

declare -i num="42"

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

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 compound types, like a class describing a file with all the attributes that a file can have.

Bash 4确实具有关联数组( declare -A ),类似于Python词典,该词典大大扩展了功能.

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

即使如此,大多数人还是认为对象定向在Bash中几乎是不可能的,尽管有人认为它可以在Korn shell(具有更强大的功能)中完成.http://en.wikipedia.org/wiki/面向对象的编程

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有什么用,它意味着什么-易于处理且易于操作的简单处理.但是有一个临界点,使用该语言变得笨拙,容易出错且运行缓慢.该临界质量可以是规模之一,即大量数据或复杂性.

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并切换到Python.只是随着程序变得越来越复杂和越来越大,使用Python的情况也越来越强大.

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.

我应该补充一点,就是说随着时间的推移,shell脚本很少变得越来越小,越来越复杂!

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

这篇关于在bash中获取变量类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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