我已经为raspberry pi编写了一个bash脚本,它将一些文件复制到USB设备,然后将它们移动到硬盘上的新文件夹中。当插入USB设备时,它将通过udev规则进行安装,然后执行复制/移动脚本。如果我等待5秒左右,它的效果非常好。
为了获得更好的反馈,何时拔下设备,我在开始时打开了一个LED,在结束时关闭了。这不行!在那之后不久,领导就开始了。那时不复制文件!我必须等待另外5秒,然后一切都很好。为什么?该脚本应该是序列化的,还是我错了?
这里是剧本:
#!/bin/bash
# Enable LED
/home/pi/project/src/led.sh 8 1
data="/home/pi/project/data/"
olddata="/home/pi/project/data/olddata/"
backup="/media/usbstick/backup/"
dateFolder=""
# Get current time
t=$(date +"%y-%m-%d_%H-%M")
# Check backup directory
if [ ! -d "$backup" ]; then
mkdir $backup
fi
dateFolder="${backup}backup_${t}/"
# Check dataFolder directory
if [ ! -d "$dateFolder" ]; then
mkdir $dateFolder
fi
# Check olddatq directory
if [ ! -d "$olddata" ]; then
mkdir $olddata
fi
# First copy all data to usb stick
find "${data}" -maxdepth 1 -mindepth 1 ! -iname "olddata" \
| xargs -I {} cp -r {} $dateFolder
# Then move the data to olddata
find "${data}" -maxdepth 1 -mindepth 1 ! -iname "olddata" \
| xargs -I {} mv {} $olddata
# Disable red LED, enable green
/home/pi/project/src/led.sh 8 0
/home/pi/project/src/led.sh 7 1
答案 0 :(得分:1)
在最后添加同步解决了它。感谢Jite
# Enable LED
/home/pi/project/src/led.sh 8 1
data="/home/pi/project/data/"
olddata="/home/pi/project/data/olddata/"
backup="/media/usbstick/backup/"
dateFolder=""
# Get current time
t=$(date +"%y-%m-%d_%H-%M")
# Check backup directory
if [ ! -d "$backup" ]; then
mkdir $backup
fi
dateFolder="${backup}backup_${t}/"
# Check dataFolder directory
if [ ! -d "$dateFolder" ]; then
mkdir $dateFolder
fi
# Check olddatq directory
if [ ! -d "$olddata" ]; then
mkdir $olddata
fi
# First copy all data to usb stick
find "${data}" -maxdepth 1 -mindepth 1 ! -iname "olddata" \
| xargs -I {} cp -r {} $dateFolder
# Then move the data to olddata
find "${data}" -maxdepth 1 -mindepth 1 ! -iname "olddata" \
| xargs -I {} mv {} $olddata
sync
# Disable red LED, enable green
/home/pi/project/src/led.sh 8 0
/home/pi/project/src/led.sh 7 1