zoukankan      html  css  js  c++  java
  • shell 将文件名读入数组

    shell 将文件名读入数组

    我想得到一个文件列表,然后将结果读入一个数组,其中每个数组元素对应一个文件名。

    shopt -s nullglob
    array=(*)
    array2=(file*)
    array3=(dir/*)
    

    如果没有匹配项,则nullglob选项会使数组为空。

    以下将在当前目录中创建一个带ls输出的数组arr:

    arr=( $(ls) )
    

    虽然使用ls的输出根本不安全。

    比ls更好更安全,你可以使用echo *:

    arr=( * )
    
    echo ${#arr[@]} # will echo number of elements in array
    
    echo "${arr[@]}" # will dump all elements of the array
    
    path="" # could set to any absolute path
    declare -a array=( "${path}"/* )
    
  • 相关阅读:
    模块和包
    mysql视图、存储过程等
    mysql 索引
    sql语句
    HTTP协议
    Django中的form组件
    数据结构
    一些常用函数
    C/C++中tag和type
    什么是compile-time-constant
  • 原文地址:https://www.cnblogs.com/michaelcjl/p/15673381.html
Copyright © 2011-2022 走看看