【发布时间】:2019-08-24 23:12:52
【问题描述】:
我想在 Bash 中编写一个简短的备份脚本,让我选择一个要保存的目录,然后对其进行压缩。我已经完成了。
接下来,我想制作它以便我可以比较要复制的文件的大小。我用du -b /example/directory | cut -f1 。这让我知道了该目录中文件夹的大小,没有它们的名字。但我无法真正将它与使用 if 语句的值进行比较,因为它不是整数语句。
这是我目前的代码。
#!/bin/bash
#Which folders to backup
backup_files="/home"
# Where to save
dest="/home/student"
# Check size of each folder
file_size=$(du -b /example/directory | cut -f1)
# Size limit
check_size=1000
# Archive name
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tar.gz"
# Here's the problem I have
if [ "$file_size" -le "$check_size" ]; then
tar -vczf /$dest/$archive_file $backup_files
fi
echo "Backup finished"
【问题讨论】:
标签: bash shell comparison backup