这个bash脚本正在备份文件或文件夹,我需要添加一个选项来压缩用户选择的文件。
这是我的代码,我可以选择在正确的地方为脚本询问是或否,以及什么是参数?
#!/bin/bash
#
backup_file=`date +%F_%R`_my_backup.tar
clear
echo *******************************************
echo
echo "**********Today's date is $(date)********"
echo "The current directory $(pwd)"
echo
echo "***files in this directory are***"
echo -e "A full list of contents follows: $(ls -R)\n"
echo
echo
echo *******Custom Backup Program*******
echo
echo ":Option 1: Create a backup"
echo ":Option 2: Exit backup program"
echo -n "Enter your choice 1, 2 : "
read choice
case $choice in
1) echo "perform a backup"
echo "Enter full path for backup"
read filepath
echo " Enter destination"
read location
if [ -d $filepath ] || [ -f $filepath ]
then
echo "would you like to compress ? y/n ***** this is where i think it should be followed by if and else ?
tar -cvzf $location/$backup_file $filepath
echo "File $filepath$backup_file has been created."
echo "Here are you backup files"
else
echo "the $filepath does not exist"
fi
2) echo "Backup cancelled " ;;
esac
答案 0 :(得分:0)
是的,你非常接近。请参阅代码修订
echo *******Custom Backup Program*******
echo
echo ":Option 1: Create a backup"
echo ":Option 2: Exit backup program"
echo -n "Enter your choice 1, 2 : "
read choice
case $choice in
1) echo "perform a backup"
echo "Enter full path for backup"
read filepath
echo " Enter destination"
read location
if [ -d $filepath ] || [ -f $filepath ]
then
echo "would you like to compress ? y/n"
# ***** this is where i think it should be followed by if and else ?
read compressed
if [[ $compressed ~= [Yy]* ]] ; then
tar -cvzf $location/$backup_file $filepath
echo "File $filepath$backup_file.gz has been created."
echo "Here are you backup files"
else
tar -cvf $location/$backup_file $filepath
echo "File $filepath$backup_file has been created."
echo "Here are you backup files"
fi
else
echo "the $filepath does not exist"
fi
2) echo "Backup cancelled " ;;
esac
你也可以用嵌套的case语句来实现,这是我的偏好,即
. . . .
read choice
case $choice in
1) echo "perform a backup"
echo "Enter full path for backup"
read filepath
echo " Enter destination"
read location
if [ -d $filepath ] || [ -f $filepath ]
then
echo "would you like to compress ? y/n"
# ***** this is where i think it should be followed by if and else ?
read compressed
case $compressed in
[Yy]* )
tar -cvzf $location/$backup_file $filepath
echo "File $filepath$backup_file.gz has been created."
echo "Here are you backup files"
;;
* )
tar -cvf $location/$backup_file $filepath
echo "File $filepath$backup_file has been created."
echo "Here are you backup files"
;;
esac
else
echo "the $filepath does not exist"
fi
2) echo "Backup cancelled " ;;
esac
我不确定你的消息是什么意思"这是你的备份文件",但那不是'#34;影响您的问题或脚本的功能。