小编use*_*885的帖子

(Bash) 从函数内编辑数组的内容

我试图用空格将数组的所有项目填充到 20 个字符,但似乎无法让我的循环正常工作。它似乎通过数组项正确递增,但不会更改项。我哪里出错了?

#!/bin/bash

testArray=( "bish" "bash" "bosh")

padLine () {
array=( "${@}" )
testLength=20
counter=0

##loop begins here##
for i in "${array[@]}";
do
size=${#array[$counter]}
testLength=20

#echo ""
#echo "size: " $size
#echo "Tlength: " $testLength
#echo "count: " ${array[$counter]}
#echo ""

if [ $size -lt $testLength ]
then 
    offset=$( expr $testLength - $size )

    #echo "Offset: " $offset

    case $offset in
        0)
            l0=""
            ;;
        1)
            l1=" "
            array[$counter]=${array[$counter]/%/$l1};;
        2)
            l2="  "
            array[$counter]="${array[$counter]/%/$l2}";;
        3)
            l3="   "
            array[$counter]=${array[$counter]/%/$l3};;
        4)
            l4="    " …
Run Code Online (Sandbox Code Playgroud)

bash shell-script

4
推荐指数
1
解决办法
2129
查看次数

标签 统计

bash ×1

shell-script ×1