The input file of a script is passed as an argument from command line. I want the log file from tee to be saved in the same directory than the input file:
Lets say that input file is at "data/temp/inputfile.txt"
This is the part that is giving me problems:
{
INPUT1=$1
#Save each subdirectory as an element of the array
bkpIFS="$IFS"
IFS='/' read -r -a inputstring <<< $INPUT1
IFS="$bkpIFS"
#Get number of levels
PATHLENGTH=${#inputstring[@]}
if [ PATHLENGTH > 1 ]
then
#Delete the input file from path
FILEONLY=${inputstring[-1]}
INPUTFOLDER=${INPUT1/%$FILEONLY/}
#Name the log file
LOGFILE="arrays.log"
LOGPATH="$INPUTFOLDER$LOGFILE"
else
#If input file is at working directory
LOGPATH="arrays.log"
fi
# Here goes the main script...
printf "\nDone!\n"
} | tee $LOGPATH
I'm just getting an empty file named "1" at the working directory. Of course if I write in the last line:
} | tee arrays.log
everything goes well, but I need each log file in the same place than the input file.
Looks overly complicated.
should do the same thing, even in the case where there is no directory in $1 (because
$dirname foobar
returns.
).