相关疑难解决方法(0)

bash:$ [<arithmetic-expression>]与$((<arithmetic-expression>))

我刚刚偶然发现了bash语法:

foo=42
bar=$[foo+1] # evaluates an arithmetic expression
Run Code Online (Sandbox Code Playgroud)

当我用Google搜索时,我找到了 http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html#sect_03_04_05:

3.4.6.算术扩展

算术扩展允许评估算术表达式和结果的替换.算术扩展的格式是:

$(( EXPRESSION )) 
Run Code Online (Sandbox Code Playgroud)

...

只要有可能,Bash用户应尝试使用方括号的语法:

$[ EXPRESSION ] 
Run Code Online (Sandbox Code Playgroud)

但是,这只会计算EXPRESSION的结果,并且不进行测试......

在我的bash手册页中,我只能找到如下$(( EXPRESSION ))形式:

foo=42
bar=$((foo+1)) # evaluates an arithmetic expression
Run Code Online (Sandbox Code Playgroud)

那么什么测试不是用$[...]那个做的$((...)),或者$[...]只是遗留版本$((...))

legacy syntax bash arithmetic-expressions operators

29
推荐指数
2
解决办法
6257
查看次数

Bash中的循环子shell困境

我想计算给定目录中的所有*bin文件.最初我正在使用for-loop:

var=0
for i in *ls *bin
do
   perform computations on $i ....
   var+=1
done
echo $var
Run Code Online (Sandbox Code Playgroud)

但是,在某些目录中有太多文件导致错误: Argument list too long

因此,我正在尝试使用管道while-loop:

var=0
ls *.bin | while read i;
do
  perform computations on $i
  var+=1
done
echo $var
Run Code Online (Sandbox Code Playgroud)

现在的问题是使用管道子壳创建.因此,echo $var回报0.
我该如何处理这个问题?
原始代码:

#!/bin/bash

function entropyImpl {
    if [[ -n "$1" ]]
    then
        if [[ -e "$1" ]]
        then
            echo "scale = 4; $(gzip -c ${1} | wc -c) / $(cat ${1} …
Run Code Online (Sandbox Code Playgroud)

bash shell

24
推荐指数
2
解决办法
2万
查看次数

标签 统计

bash ×2

arithmetic-expressions ×1

legacy ×1

operators ×1

shell ×1

syntax ×1