下载资源第一版本
- 下载对应的图片并重命名图片资源名称
## replace.sh
#!/bin/bash
index=1
cp $1 temp.md
declare -A newmap
for uri in $(cat temp.md | egrep "img.*http" | awk -F " " '{print $3}' | awk -F "\"" '{print $2}')
do
echo "$uri"
#echo $index
f=$index.${uri##https*.}
echo $f
last_file=${uri##https*/}
echo $last_file
newmap["$index"]="$last_file"
let "index=$index+1"
wget -c $uri -O $f >> /dev/null
done
for key in "${!newmap[@]}"
do
val=${newmap[$key]}
#echo $key, $val, ${val##*.}
f=$key.${val##*.}
sed -e s"/https.*$val/$f/" temp.md >> temp_bak.md
rm -rf temp.md
mv temp_bak.md temp.md
done
mv temp.md 01-local.md
- 执行
./replace.sh 01.md
下载资源第二版本
- 替换脚本
function replace_images() {
index=1
name=./images/$2
if [ ! -d $name ]; then
mkdir -p $name
fi
for uri in $(cat $1 | egrep "\!\[\].*http" | awk -F "\(" '{print $3}' | awk -F "\)" '{print $1}')
do
f=$index.${uri##https*.}
pos=$name/$f
last_file=${uri##https*/}
wget -c $uri -O $pos >> /dev/null
imagePath=../images/$2/$f
target=${imagePath//\//\\/}
echo $target
sed -i "" "s/https.*$last_file/$target/" $1
let "index=$index+1"
done
}
- 执行
replace_images 01base/01.md 01
shell 将换行替换为空格(linux环境)
sed -i ':a;N;s/\n/ /g;ba' test