linux – 如何在shell脚本中 *** 作数组

linux – 如何在shell脚本中 *** 作数组,第1张

概述我希望我的脚本定义一个空数组.如果预定义条件为真,则应添加数组值.为此,我所做的就是 declare -a FILESfile_count=0if [ "$file_ext" != "$SUPPORTED_FILE_TYPE" ] ; then echo "$file_ext is not supported for this task."else $FILE 我希望我的脚本定义一个空数组.如果预定义条件为真,则应添加数组值.为此,我所做的就是

declare -a fileSfile_count=0if [ "$file_ext" != "$SUPPORTED_file_TYPE" ] ; then        echo "$file_ext is not supported for this task."else        $fileS[$file_count] = $filename        file_count=$file_count+1fi

执行此脚本时,我收到这样的错误

linux-softwares/launchers/join_files.sh: 51: [0]: not found
解决方法 当数组中的设置数据无法使用$调用时:

declare -a fileSfile_count=0if [ "$file_ext" != "$SUPPORTED_file_TYPE" ] ; then        echo "$file_ext is not supported for this task."else        fileS[$file_count]=$filename        file_count=$file_count+1fi

没有$的文件.

这对我有用:

#!/bin/bashdeclare -a fileSfile_count=0file_ext='jpg'SUPPORTED_file_TYPE='jpg'filename='test.jpg'if [ "$file_ext" != "$SUPPORTED_file_TYPE" ] ; then        echo "$file_ext is not supported for this task."else        fileS[$file_count]=$filename        file_count=$(($file_count+1))fi

如你所见,对数学运算稍加修改$(()),但fileS分配是相同的……

正如经过大量测试所指出的那样,Ubuntu默认的shell似乎是破折号,这就引发了错误.

总结

以上是内存溢出为你收集整理的linux – 如何在shell脚本中 *** 作数组全部内容,希望文章能够帮你解决linux – 如何在shell脚本中 *** 作数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1031309.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存