有人能告诉我为什么这个脚本无法正确运行吗?似乎是 if 语句有问题,因为我一直收到错误:
/home/sinttx/Development/backup/ddbackup.sh: line 18: syntax error near unexpected token `then'
/home/sinttx/Development/backup/ddbackup.sh: line 18: if [ "$exit" -eq 0 ] ; then'
剧本:
#!/bin/bash
#(SINTTX) Function and if statement to backup/clone /dev/???.
echo "
THIS WILL COMPLETE A BACKUP OF DESIRED /DEV "
echo -n "Enter desired name for saved file (File will be saved with extention .iso) and press [ENTER]: "
read iso
echo -n "Enter the device to be duplicated/backed up (Eg /dev/sda) and press [ENTER]: "
read dev
echo -n "Enter the desired path/location for the backup file (Eg /home/USERNAME/backups/) and press [ENTER]: "
read path
echo "PLEASE ENTER THE SUDO PASSWORD (IF PROMPTED) AND WAIT :-)
NOTE: THIS COULD TAKE SOME TIME DEPENDING ON FILE SIZE AND HARDWARE SPEED!!"
sudo dd if="$dev" of="$path"/"$iso"
exit=$?
if [ "$exit" -eq 0 ] ; then
echo "SUCCESS"
else
echo "BACKUP HAS SUFFERED AN ERROR AND DID NOT COMPLETE SUCCESSFULLY"
fi
答案1
作为@Cyrus 建议,您可能有一个非打印字符导致出现问题。最可能的原因是,您在某个时候从 Windows 计算机编辑了此脚本,并\r
在行尾添加了一个。您可以通过运行来检查该行中是否有任何内容
grep 'if [ "$exit"' script.sh | od -c
这将显示所有字符,包括非打印字符。
如果确实存在\r
,你可以通过运行“来修复它”
sed -i 's/\r//' script.sh
或者,安装dos2unix
( sudo apt-get install dos2unix
) 并运行
dos2unix script.sh > fixed.sh