Bash中字典,字符串,文件夹递归遍历操作记录

1.字典操作

1.1定义字段

declare -A mapfile

mapfile=( 
[file1]=/home/abc/test1.txt'
[file2]='/home/abc/test2.txt'
[file3]='/home/abc/test2.txt'
[file4]='/home/abc/test3.txt'
[file5]='/home/abc/test4.txt'
)

1.2遍历字典

for node_file in ${mapfile[@]}
do
    echo ${node_file}
done

2.字符串操作

opt_path=$1
pattern=".cpp"

2.1为空判断

if [ -z "$opt_path" ]; then
 echo "run param is empty"
 exit
fi

2.2包含处理

if [[ $opt_path =~ $pattern ]]; then
    echo "ok"
fi

2.3字符串以.h或者.cpp结尾

if [[ $opt_path = *.h ]] || [[ $opt_path = *.cpp ]]; then
    echo "this file ok"
fi

3.递归遍历文件夹

function read_dir()
{
    for file in `ls $1`
    do
            cur=$1/$file
        if [ -d $cur ]; then
            #echo $cur
          read_dir $cur
        else
            echo $cur
        fi
    done
}

4.数字相等判断

findflag=1
if [ $findflag -eq 1 ]; then
    echo "findflag == 1"
fi

5.当前目录下文件类型判断

for i in `ls`; 

    if [ -f $i ]; then 
        echo $i--file;
    else 
        echo $i--directory
    fi
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值