需求描述:
需要读取到文件夹内的文件名,并根据文件名开头的四个字符串来分别放入不同的路径
实现代码
#!/bin/bash -x
time=$(date "+%Y%m%d%H%M%S")
#print $info in screen and write in file
function printAndWrite(){
info=$1
echo $info
echo $info >> log_$time.txt
}
for i in /home/*.tar.gz;
do
echo $i;
filename=${i##*/}
echo $filename;
if [ ${filename:0:4} = "test" ]
then
printAndWrite "you can do st with $i order by test"
else
if [ ${filename:0:4} = "haha" ]
then
printAndWrite "you can do st with $i order by haha"
else
printAndWrite "you can do st with $i order by others"
fi
fi
done
还有另一个小问题,在Windows机器上直接用notepad新建个sh文件,写好代码后上传到linux系统死活报语法错误,后来才发现是文件格式的问题,如图,务必转成Unix(LF)格式,这样才能正常运行;