shell 批量处理
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell 批量处理相关的知识,希望对你有一定的参考价值。
参考技术A ### 批量 - 压缩文件夹内文件内容```
#!/bin/bash
# zip-Shell.sh
echo "==============================START================================="
# 0.输入工作目录
echo "input your work dir:"
read work_dir
cd ./$work_dir
path=$1
files=$(ls $path)
a=0
for filename in $files
do
a=$[$a+1]
cd $filename
if [ $? -ne 0 ]; then
echo "处理第$a个包: $filename, cd failed! Please check this file."
else
if [ -d "./__MACOSX" ]; then
rm -rf ./__MACOSX
fi
zip -q -r -m $filename.zip *
if [ $? -ne 0 ]; then
echo "处理第$a个包: $newfilename, zip failed! Please check this file."
cd ..
else
mv $filename.zip ..
cd ..
# 可以自行决定不删除原文件
rm -r $filename
fi
echo "处理完毕第$a个包: $filename"
fi
done
echo "==============================END=============================="
```
### 批量 - 解压缩
```
#!/bin/bash
# unzip-Shell.sh
echo "==============================START================================="
# 0.输入工作目录
echo "input your work dir:"
read work_dir
cd ./$work_dir
path=$1
files=$(ls $path)
a=0
for filename in $files
do
a=$[$a+1]
# 移除字符串中的 ".zip"
newfilename=$filename%.zip
unzip -q $filename -d $newfilename
if [ $? -ne 0 ]; then
echo "处理第$a个包: $newfilename, unzip failed! Please check this zip."
else
cd $newfilename
if [ $? -ne 0 ]; then
echo "处理第$a个包: $newfilename, cd failed! Please check this file."
else
if [ -d "./__MACOSX" ]; then
rm -rf ./__MACOSX
fi
cd ..
# 否要删除原来的zip文件
rm -r $filename
echo "处理完毕第$a个包: $newfilename"
fi
fi
done
echo "==============================END=============================="
```
### 批量 - 替换文件
```
#!/bin/bash
# copyBgPng.sh
# 文件拷贝的目标工作目录
MY_WORK_DIR="./6060"
# 文件拷贝的源工作目录
MY_COPY_SRC_PNG_DIR="./luaScript/bg.png"
MY_COPY_DST_PNG_DIR="./ar/res/bg_0.png"
echo "==============================START================================="
cd $MY_WORK_DIR
path=$1
files=$(ls $path)
a=0
for filename in $files
do
a=$[$a+1]
cd $filename
if [ -d "./__MACOSX" ]; then
rm -rf ./__MACOSX
fi
cp -r -f $MY_COPY_SRC_PNG_DIR $MY_COPY_DST_PNG_DIR
if [ $? -ne 0 ]; then
echo "ERROR:处理第$a个包: $filename,拷贝失败!"
else
echo "处理完毕第$a个包: $filename"
fi
cd ..
done
echo "==============================END=============================="
```
### 批量 - 解析文件(json、xml等)生成对应需要的plist文件
```
#!/bin/bash
# writeRuleConfigPlist.sh
# 工作目录
MY_WORK_DIR="./6060"
MY_XML_DIR="./StickerInfo.xml"
MY_PLIST_FILE="configuration.plist"
echo "==============================START================================="
cd $MY_WORK_DIR
path=$1
files=$(ls $path)
a=0
for filename in $files
do
a=$[$a+1]
cd $filename
if [ -d "./__MACOSX" ]; then
rm -rf ./__MACOSX
fi
# 如果是解析json文件用` cat json文件 | jq '.变量名' ` 语句
width=` cat $MY_XML_DIR | grep '<width>' | awk -F '>' 'print $2' | awk -F '<' 'print $1' `
if [ $? -ne 0 ]; then
width=300
fi
height=` cat $MY_XML_DIR | grep '<height>' | awk -F '>' 'print $2' | awk -F '<' 'print $1' `
if [ $? -ne 0 ]; then
height=300
fi
echo "$width, $height"
echo "<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>AR</key>
<string>ar</string>
<key>BubbleSize</key>
<string>$width,$height</string>
</dict>
</array>
</plist>
" > $MY_PLIST_FILE
if [ $? -ne 0 ]; then
echo "ERROR:处理第$a个包: $filename,写入失败!"
else
echo "处理完毕第$a个包: $filename"
fi
cd ..
done
echo "==============================END=============================="
```
shell 文件读取批量处理
cat ./redis-keys.txt | while read myline do echo $myline /usr/local/bin/redis-cli -h 000.00.00.000 -p 6379 -c set $myline 0 done
cat ./urlget.txt | while read myline do echo $myline curl $myline sleep 100 done
curl http://www.google.com
以上是关于shell 批量处理的主要内容,如果未能解决你的问题,请参考以下文章