脚本练习一:备份与删除

第一次练习,代码比较臃肿,主要就是用for循环来写的。
#!/bin/bash while true do echo "##############################" echo "##### 1 Copy ######" echo "##### 2 Delete ######" echo "##### 3 Quit ######" echo "##############################" read -p "Please select you want to do: " num if [ $num -eq 1 ];then echo "You choose is:COPY" echo "**********************************************************" echo "*Note: the file name you entered can only is the current directory or the absolute path of files.*" echo "**********************************************************" read -p "Please enter your copy of the file or directory:" file if [ ! -e $file ];then echo "$file is not exist" echo "please input again: " continue fi read -p "Please enter a target directory: " dir if [ ! -e $dir ];then echo "$dir is not exist" echo "please input again: " continue fi if [ -f $dir ];then echo "$dir is a file,not a directory" echo "please input again: " continue fi if [ -d $dir ];then echo "Are you sure want to copy file to: $dir" echo "Yes! input 'y' " echo "No! input 'n'" read -n 1 keys if [ $keys == y ];then echo cp -a $file $dir if [ $? ];then echo "*******`date '+%Y-%m-%d %H:%M:%S'`*******" echo "Copy $file success" echo "*********************************" sleep 2 else echo "Copy $file failed" sleep 2 exit 13 fi elif [ $keys == n ];then echo "Program exits" sleep 2 exit 14 fi fi continue fi if [ $num -eq 2 ];then echo "You choose is:Delete" read -p "Please input you want to delete the target file directory:" dir_del if [ ! -e $dir_del ];then echo "$dir_del is not exist" echo "please input again! " continue elif [ -f $dir_del ];then echo "Input $dir_del is a file,Please enter the directory first" continue elif [ -d $dir_del ];then read -p "Please input you want to delete the target file_name: " file_del if [ ! -e $file_del ];then echo "$file_del is not exist" echo "please input again! " continue elif [ -d $file_del ];then echo "Input $file_del is a directory,Please enter a file" continue elif [ -f $file_del ];then echo "input 'y'" echo "input 'n'" echo read -n 1 keys_del if [ $keys_del == y ];then cd $dir_del rm -f $file_del if [ $? ];then echo echo "*******`date '+%Y-%m-%d %H:%M:%S'`*******" echo "Delete file success!" echo "*********************************" sleep 2 else echo "Delete file failed" sleep 2 exit 15 fi elif [ $keys_del == n];then echo "Program exits" sleep 2 exit 16 fi fi fi continue fi if [ $num -eq 3 ];then exit fi done

  

posted @ 2016-03-17 21:51  要让熊猫飞上天  阅读(304)  评论(0编辑  收藏  举报