标题:Linux Shell脚本获取当前路径遍历所有指定文件 出处:刘新修 时间:Fri, 11 Mar 2016 10:13:59 +0000 作者:刘新修 地址:http://liuxinxiu.com:80/Linux_Shell_for/ 内容: C#代码 #!/bin/bash #获取当前SH脚本的绝对路径 Cur_Dir=$(pwd) #echo $Cur_Dir function ergodic(){ for file in ` ls $1` do # echo $file if [ -d $1"/"$file ] then ergodic $1"/"$file else local path=$1"/"$file #得到文件的完整的目录 local name=$file #得到文件的名字 #echo ${file##*.} /**** 打印返回的文件名 ****/ #echo ${file##*.} /**** 打印返回的文件名 ****/ #判断所匹配的文件扩展名 if [ "${file##*.}" = "gz" ]||[ "${file##*.}" = "bz2" ]||[ "${file##*.}" = "zip" ]; then #警告:中括号内不前后必须流空格、等号前后必须留空格! #echo ${1#*/software} /**** 截取字符串处理当前路径 ****/ echo "http://down.liuxinxiu.com"${1#*/software}"/"$file >>a.txt echo "http://down.liuxinxiu.com"${1#*/software}"/"$file fi fi done } INIT_PATH=$Cur_Dir ergodic $INIT_PATH 通用脚本,当前目录名为二级域名,一级域名自行定义 C#代码 #!/bin/bash #获取当前SH脚本的绝对路径 Cur_Dir=$(pwd) Sub_Path=${Cur_Dir##*/} #echo $Cur_Dir #先清空一下文件,注明:如果不清空则一直在原文件尾部追加 : > b.txt #当前目录下遍历循环文件的方法 function ergodic(){ for file in ` ls $1` do # echo $file if [ -d $1"/"$file ] then ergodic $1"/"$file else local path=$1"/"$file #得到文件的完整的目录 local name=$file #得到文件的名字 #echo ${file##*.} /**** 打印返回的文件名 ****/ #echo ${file##*.} /**** 打印返回的文件名 ****/ #判断所匹配的文件扩展名 if [ "${file##*.}" = "gz" ]||[ "${file##*.}" = "bz2" ]||[ "${file##*.}" = "zip" ]; then #警告:中括号内不前后必须流空格、等号前后必须留空格! #echo ${1#*/software} /**** 截取字符串处理当前路径 ****/ echo "http://"$Sub_Path".liuxinxiu.com"${1#*/software}"/"$file >>b.txt echo "http://"$Sub_Path".liuxinxiu.com"${1#*/software}"/"$file fi fi done } INIT_PATH=$Cur_Dir ergodic $INIT_PATH Generated by Bo-blog 2.1.1 Release