Hi all! Im trying to get this script to check for folders in a year/month/day folder structure and if the day doesnt exist then it makes the day. It will also make sure all of the days before todays date exist as well. This script assumes that the month and year folder already exist. It works fine until it finds a day that already exists then it gets stuck in a loop.
cd ~/Desktop
stopday=1
year=`date +%Y`
month=`date +%m`
day=`date +%d`
if [ -d $year ]
then echo $year
cd $year
if [ -d $month ]
then echo $month
cd $month
while [ $day -ge $stopday ]
do if [ -d $day ]
then echo $day
cd $day
if [ -d products ]
then echo ""$month"/"$day"/"$year" directory already exists."
cd ..
else mkdir products
echo "Made "$month"/"$day"/"$year" directory!"
cd ..
fi
else mkdir $day
cd $day
if [ -d products ]
then echo ""$month"/"$day"/"$year" directory already exists."
cd ..
else mkdir products
echo "Made "$month"/"$day"/"$year" directory!"
cd ..
fi
day=`expr $day - 1`
if [ $day -lt 10 ]
then day=0$day
fi
fi
echo $day
done
fi
fi
From the layout of your code it is totally unclear which "if then else fi"'s belong together. At least it is to me.
Anyway, this is your code (leaving out some parts):
if [ -d $day ]
then
echo $day
cd $day
.
.
.
else
mkdir $day
cd $day
.
.
.
day=`expr $day - 1`
if [ $day -lt 10 ]
then
day=0$day
fi
fi
Correct code, a bit simplified:
cd ~/Desktop
stopday=1
year=`date +%Y`
month=`date +%m`
day=`date +%d`
if [ -d $year ]
then
echo $year
cd $year
if [ -d $month ]
then
echo $month
cd $month
while [ $day -ge $stopday ]
do
if [ -d $day ]
then
echo $day
else
mkdir $day
fi
cd $day
if [ -d products ]
then
echo ""$month"/"$day"/"$year" directory already exists."
else
mkdir products
echo "Made "$month"/"$day"/"$year" directory!"
fi
cd ..
day=`expr $day - 1`
if [ $day -lt 10 ]
then
day=0$day
fi
echo $day
done
fi
fi
As a side note, here's a different approach to tackle your problem that involves the use of the "seq" command. My setup is Bash on Linux, check your documentation to see if you have it.
$
$ cat -n testscr1.sh
1 #!/bin/bash
2
3 yr=$(date +%Y)
4 mon=$(date +%m)
5 dt=$(date +%d)
6
7 if [ -d $yr/$mon ]; then
8 for i in $(seq -w $dt -1 1)
9 do
10 if [ -d $yr/$mon/$i/products ]; then
11 echo $yr/$mon/$i/products was found.
12 else
13 mkdir -p $yr/$mon/$i/products
14 echo $yr/$mon/$i/products was not found. Created it.
15 fi
16 done
17 fi
18
$
$
$ find ./2009 | sort
./2009
./2009/09
./2009/09/04
./2009/09/04/products
./2009/09/13
./2009/09/15
./2009/09/15/products
./2009/09/19
$
$ # now run the script
$ ./testscr1.sh
2009/09/20/products was not found. Created it.
2009/09/19/products was not found. Created it.
2009/09/18/products was not found. Created it.
2009/09/17/products was not found. Created it.
2009/09/16/products was not found. Created it.
2009/09/15/products was found.
2009/09/14/products was not found. Created it.
2009/09/13/products was not found. Created it.
2009/09/12/products was not found. Created it.
2009/09/11/products was not found. Created it.
2009/09/10/products was not found. Created it.
2009/09/09/products was not found. Created it.
2009/09/08/products was not found. Created it.
2009/09/07/products was not found. Created it.
2009/09/06/products was not found. Created it.
2009/09/05/products was not found. Created it.
2009/09/04/products was found.
2009/09/03/products was not found. Created it.
2009/09/02/products was not found. Created it.
2009/09/01/products was not found. Created it.
$
$ # check the directories
$ find ./2009 | sort | grep products
./2009/09/01/products
./2009/09/02/products
./2009/09/03/products
./2009/09/04/products
./2009/09/05/products
./2009/09/06/products
./2009/09/07/products
./2009/09/08/products
./2009/09/09/products
./2009/09/10/products
./2009/09/11/products
./2009/09/12/products
./2009/09/13/products
./2009/09/14/products
./2009/09/15/products
./2009/09/16/products
./2009/09/17/products
./2009/09/18/products
./2009/09/19/products
./2009/09/20/products
$
$