1

我正在创建一个自定义脚本来使用 Clonezilla 运行备份。该脚本按预期工作(到目前为止),但我在变量分配方面遇到了一些麻烦。

变量赋值行 (maxBackups=4) 在当前目录中创建了一个名为“4”的文件,并且 if 测试无法正常工作。

(按照我的理解,使用这种目录计数方法,指向父目录的链接算作一个目录。)

我究竟做错了什么?我知道这很简单...

谢谢

#!/bin/bash

# Automated usb backup script for Clonezilla
# by DRC


# Begin script

# Store date and time for use in folder name
# to a variable called folderName


folderName=$(date +"%Y-%m-%d--%H-%M")

# mount second partition of USB media for storing
# saved image

mount /dev/sdb2 /home/partimag/ 


# Determine if there are more than 3 directories
# and terminate the script if there are

cd /home/partimag
maxBackups=4
numberOfBackups=$(find -maxdepth 1 -type d | wc -l)

if [ $numberOfBackups > $maxBackups ]; then
echo "The maximum number of backups has been reached."
echo "Plese burn the backups to DVD and remove them"
echo "from the USB key."
echo "Press ENTER to continue and select 'Poweroff'"
echo "from the next menu."

# Wait for the user to press the enter key

read

# If there are three or less backups, a new backup will be made

else
/usr/sbin/ocs-sr -q2 -c -j2 -a -z0 -i 2000 -sc -p true savedisk $folderName sda

fi
4

1 回答 1

0

maxBackups=4没有创建4在您的目录中命名的文件。创建该文件的是if [ $numberOfBackups > $maxBackups ]位。>是重定向到输出文件,因为[是命令,而不是关键字。您可以尝试其中一种:

if [ $numberOfBackups -gt $maxBackups ]     # -gt is test's version of greater than

if [[ $numberOfBackups > $maxBackups ]]     # double brackets are keywords, not commands

if (( numberOfBackups > maxBackups ))       # arithmetic context doesn't even require $
于 2014-01-13T20:55:29.443 回答