shell下简单脚本

shell下简单脚本

 

前提有文件有配置,注意变量

 

 

#!/bin/bash
# find file_bak
time_d=`date  +%Y%m%d`
time_ago=`date -d"1 day ago" +"%Y%m%d"`
time=`date`
log=/home/output/shell/data_bak.log
Source_dir=`ls /home/output/data/*$time_d**.txt$*`
Source_wc=`ls /home/output/data/*$time_d**.txt$* | wc -l`
target_dir=/home/output/bak/$time_d/
Source_dir_ago=`ls /home/output/data/*$time_ago**.txt$*`
target_dir_ago=/home/output/bak/$time_ago/
mount_dir=/home/output/mount/

if [ -d $target_dir_ago ];then
 /bin/cp $Source_dir_ago $target_dir_ago
 /bin/mv $Source_dir_ago $mount_dir
else
 mkdir $target_dir_ago
 /bin/cp $Source_dir_ago $target_dir_ago
 /bin/mv $Source_dir_ago $mount_dir
fi
if [ $? -eq 0 ];then
echo " $time cp $time_ago directory /home/output/data/ file to $target_dir ok and mv $Source_wc file to $mount_dir ok" &>> $log
fi

if [ -d $target_dir ];then
 /bin/cp $Source_dir $target_dir
 /bin/mv $Source_dir $mount_dir
else
 mkdir $target_dir
 /bin/cp $Source_dir $target_dir
 /bin/mv $Source_dir $mount_dir
fi

if [ $? -eq 0 ];then
echo " $time cp directory /home/output/data/ file to $target_dir ok and mv $Source_wc file to $mount_dir ok" &>> $log
else
echo " $time warning: /home/output/data/ directory no .txt file   " &>> $log
fi

 

 

前提有文件,有配置(注意变量)

[root@cucrz-8 shell]# cat data_bak_copy.sh
#!/bin/bash
# data file copy
dev="_"
time=`date`
for file_id in `ls -l /home/output/local/ | awk '{print $9}' | sort -n | uniq | sed /^$/d`
do
echo "$file_id"
#time_d=`date  +%Y%m%d`
for area_id in `ls -l /home/output/local/$file_id/ | awk '{print $9}' | awk -F_ '{print$3}' |sort -n |uniq |sed /^$/d`
do
for time_d in `ls -l /home/output/local/$file_id/ | awk '{print $9}' | awk -F_ '{print $5}' | sort -n |uniq |sed /^$/d`
do
#for area_id in `cat /home/output/shell/data_file_area`
#do
echo "$area_id"
source_dir=`ls /home/output/local/$file_id/*$file_id**$time_d**$dev$area_id$dev**.txt*`
target_dir=/home/output/mount/$file_id/$time_d/$area_id/
log=/home/output/shell/data_copy.log
target_dir_bak=/home/output/local_bak/$file_id/$time_d/$area_id
if [ -d $target_dir_bak ];then
 /bin/cp $source_dir $target_dir_bak
else
 /bin/mkdir -p $target_dir_bak
        /bin/cp $source_dir $target_dir_bak
fi

if [ -d $target_dir ];then
 /bin/mv $source_dir $target_dir
else
 /bin/mkdir -p $target_dir
 /bin/mv $source_dir $target_dir
fi
if [ $? -eq 0 ];then
echo "$time cp file $source_dir to $target_dir ok and mv file to $mount_dir ok" &>> $log
else
echo "$time error: ls Don't have access to /home/output/local/$file_id/*$file_id*$area_id**$time_d*.txt* : No files and directories   " &>> $log
fi
done
done
done

 

 

你可能感兴趣的:(linux运维,linux,脚本)