加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – “无类型”是否与“动态类型”相同?

发布时间:2020-12-15 16:59:58 所属栏目:安全 来源:网络整理
导读:参见英文答案 Does “untyped” also mean “dynamically typed” in the academic CS world?9个 根据 Advanced Bash-Scripting Guide, bash变量是无类型的: Unlike many other programming languages,Bash does not segregate its variables by “type.”
参见英文答案 > Does “untyped” also mean “dynamically typed” in the academic CS world?9个
根据 Advanced Bash-Scripting Guide,

> bash变量是无类型的:

Unlike many other programming languages,Bash does not segregate its variables by “type.” Essentially,Bash variables are character
strings,but,depending on context,Bash permits arithmetic
operations and comparisons on variables. The determining factor is
whether the value of a variable contains only digits.

该链接还给出了示例.

“无类型”是否与“动态类型”的概念相同
在编程语言?如果没有,那么关系是什么
两者之间的差异?
>

To lighten the burden of keeping track of variable types in a script,Bash does permit 07002 variables.

例如,通过declare -i将变量声明为整数类型
MYVARIABLE.

这被称为“打字”变量吗? “打字”是什么意思
与“静态打字”的概念相同?

这里的大部分都得到了很好的回答……

Does “untyped” also mean “dynamically typed” in the academic CS world?

由至少两个非常熟悉此事的人.对于我们大多数尚未研究类型系统等的人而言,“无类型”意味着动态打字,但这在学术界是个用词不当,见上文. untyped实际上意味着没有类型,即思考程序集,键入Bash,它在运行时计算出它的类型.让我们从Advanced Bash Scripting Guide中获取以下句子,强调我的…

http://tldp.org/LDP/abs/html/untyped.html

Unlike many other programming languages,Bash does not segregate its
variables by “type.” Essentially,Bash permits arithmetic operations
and comparisons on variables. The determining factor is whether the
value of a variable contains only digits
.

Bash发现某些东西在运行时是一个数字,即它是动态类型的.
在64位机器上的汇编我可以存储任何8个字节的寄存器和减少它,它不检查,看看是否事情字符等,没有关于它是关于递减它只是递减64位的东西上下文,它不会检查或计算出它正在递减的东西的类型.

Perl不是一种无类型语言,但下面的代码可能会使它看起来像将整数视为整数

#!/usr/bin/perl
use strict;
use warnings;
my $foo = "1";
my $bar = $foo + 1;
print("$barn");

$foo被分配了一个字符串,但是增加了?这是否意味着Perl是无类型的,因为基于上下文它可以做你想做的事情?我不这么认为.

这与Python不同,如果您尝试相同的事情,Python实际上会给您以下错误…

Traceback (most recent call last):
  File "py.py",line 2,in <module>
  bar = foo + 1

如果Python是动态类型的,并且Perl是动态类型的,为什么我们会看到不同的行为.是因为他们的类型系统不同或者他们的类型转换语义不同.在汇编中我们有类型转换指令,将字符串更改为整数,反之亦然?

Bash有不同的类型转换规则

#!/bin/bash
set -e
MYVAR=WTF
let "MYVAR+=1"
echo "MYVAR == $MYVAR";

这将为MYVAR分配1而不是递增它,即如果你增加一个字符串bash将字符串设置为整数零,然后执行增量.它正在执行类型转换,这意味着它是键入的.

对于仍然认为Bash是无类型的人来说,试试这个….

#!/bin/bash
declare -i var1=1
var1=2367.1

你应该得到这样的东西……

foo.sh: line 3: 2367.1: syntax error: invalid arithmetic operator (error token is ".1")

但以下显示没有这样的错误

#!/bin/bash
var1=2367.1

以下输出

#!/bin/bash
var1=2367.1
echo "$var1"
let "var1+=1"
echo "$var1"

是没有声明类型的相同警告……

2367.1
foo.sh: line 4: let: 2367.1: syntax error: invalid arithmetic operator (error token is ".1")
2367.1

这是一个更好的例子

#!/bin/bash
arg1=1234
arg2=abc

if [ $arg1 -eq $arg2 ]; then
  echo "wtf";
fi

为什么我得到这个……

foo.sh: line 5: [: abc: integer expression expected

Bash问我一个整数表达式.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读