bash script, create array of all files in a directory

前端 未结 3 1212
你的背包
你的背包 2020-12-08 10:40

I have a directory myDir of many .html files. I am trying to create an array of all the files in the directory so I might be able to index the array and be ab

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 11:09

    Your solution will work for generating the array. Instead of using a while loop, use a for loop:

    #!/bin/bash
    files=($( ls * )) #Add () to convert output to array
    counter=0
    for i in $files ; do
      echo Next: $i
      let counter=$counter+1
      echo $counter
    done
    

提交回复
热议问题