脚本练习一:备份与删除

本文介绍了一个简单的Bash脚本示例,通过使用for循环实现文件复制和删除功能。脚本提供了用户交互界面,允许用户选择操作并指定文件路径。
第一次练习,代码比较臃肿,主要就是用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

  

转载于:https://www.cnblogs.com/miaoxg/p/5289616.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值