首页 > 代码库 > Linux shell 脚本小记2

Linux shell 脚本小记2

1、从文件读取while read linedo    echo "line=$line"done < file.txt2、将字符串转换为数组,并进行遍历str="html, css, javascript, java, php, go, python"arr=(${str//,/ })for item in ${arr[@]}do    echo "item=$item"done3、将目录转换为字符串ls | xargs -d\t echo |awk BEGIN{i=0;} { if(i > 0){ printf(",");} i++;printf("%s", $0);}4、递归function find(){    local dir=$1    local arr=( $(ls $dir) )    for item in ${arr[@]}    do        local path="$dir/$item"        if [ -L $path ]        then            echo "ln $path"            find $path        elif [ -d $path ]        then            echo "dir $path"            find $path        else            echo "file path=$path, dir=${path%/*}, filename=${path##*/}"        fi    done}备注:获取当前路径下的目录:${path%/*}获取当前路径下的文件名:${path##*/}

 

Linux shell 脚本小记2