创建一个脚本来备份文件或目录树,方法是压缩文件并将其复制到$ HOME / Backups。 zipfile名称应包括备份内容以及创建文件的日期。该脚本应该使用随机数量的参数来指定要备份的内容。如果没有给出至少一个要包含在备份中的项目,它应该抱怨。我已经完成了大部分工作但是我遇到了多个文件的问题,即同时备份file1 file2
#!/bin/bash
clear
echo
echo "Use this script to backup files to your home/backups directory"
echo
ls -la
echo
echo "================================================================"
echo
echo -n "Input file(s)/dir to backup: " ; read filez
while [ "$filez" == "" ] ; do
echo -n "You didnt input a filename, try again: " ; read filez
done
while [ ! -e "$filez" ] ; do
echo -n "No such file/dir, try again: " ; read filez
while [ "$filez" == "" ] ; do
echo -n "You didnt input a filename, try again: " ; read filez
done
done
echo
echo "================================================================"
echo
echo -n "Input name of backup file you wish to create(date automatically included): " ; read filezname
while [ "$filezname" == "" ] ; do
echo -n "You didnt input a filename, try again: " ; read filezname
done
zip -r $HOME/backups/$filezname"_$(date +%F)" $filez
答案 0 :(得分:0)
就个人而言,我是bash
脚本的粉丝,他们直接从命令行中获取参数:
script arg1 arg2 arg3 ...
Bash使用特殊数组$@
获取参数,您可以使用shift
进行参数处理。像这样:
#!/bin/bash
filezname=$1
shift
filez=""
for file in "$@"
do
filez="$filez $file"
done
if [[ $filez == "" ]]
then
echo "Give me argz! Nom nom nom!"
exit 0
fi
# Do stuff
这样做是需要第一个参数,将其作为zip文件的名称,然后将您想要压缩的其余文件名啜饮成一个可以播放的大的长空格分隔字符串。