I’m trying to write a simple script that will move files in a list of sequential files in a directory if one is missing. For instance, if I have a directory with files named “1”, “2”, “3”, “5”, etc, I want to move “3” to “4”, “3” to “2”, etc. I have the following code below. Upper and Lower correspond to the values that I want to shift through. Using the above example, LOWER would be 1 and UPPER would be 5.
for z in $( eval echo {$UPPER..$LOWER}) ; do
checkfile $DIRNAME $z #Returns -1 on file missing
if [[ $? -ne 0 ]]; then
echo "Found bad match"
for y in $( eval echo {$z..$LOWER}) ; do
n=$(($y - 1))
echo "$y $n"
mv "$n" "$y"
done
exit 0
fi
done
I receive the following output
Found bad match
18 17
mv: cannot stat `17': No such file or directory
17 16
mv: cannot stat `16': No such file or directory
16 15
mv: cannot stat `15': No such file or directory
15 14
mv: cannot stat `14': No such file or directory
I’m not quite sure why mv is complaining. Any ideas would be much appreciated!
Your
mvcommand is trying to move files in your local directory, but the files you’re checking are elsewhere (in$DIRNAME). Perhaps trymv "$DIRNAME/$n" "$DIRNAME/$y"?