I am trying to give the option to the users to run the script in a different directory. However when the script changes the directory, the mkdir does not work.
I change the directory like this:
case $1 in
"-d")
PWD=$2;;
*)
;;
esac
but when I run the script, it runs in the directory that it exists. it does not change the directory.
here is the complete code:
#!/bin/bash
#File sorter
set -e # Abort in case of error
START=$(date +"%T")
SAVEIFS=$IFS
case $1 in
"-d")
PWD=$2;;
*)
;;
esac
echo "trigger took place in $PWD"
for i in *; do
echo "processing \"$i\" ..."
case $i in
*.zip)
[ -d archive ] || mkdir archive
mkdir "archive/${i%.*}"
unzip -d "archive/${i%.*}" "./$i"
echo -e "\"$i\" extracted in $PWD/archive/${i%.*}" >> report.txt;;
*.*)
mkdir -p "${i##*.}"
mv -- "$i" "${i##*.}/"
echo -e "\"$i\" moved to $PWD/${i##*.}" >> report.txt;;
*)
tmp=$(TMPDIR=. mktemp -d)
mv -- "$i" "$tmp/"
mv -- "$tmp" "$i";;
esac
done
echo "Done in $SECONDS seconds!"
END=$(date +"%T")
echo "Start time: $START" >> report.txt
echo "End time: $END" >> report.txt
cdto change directory?