I studied that
ls -dtr1 * | tail -5 | awk 'NR==2'
lists the second last folder created among the last 5 folders created.
I wanted to use it inside a for loop:
So i created a shell script forLoopExample.sh
for ii in $(seq 1 $1)
do
f_name=`ls -dtr1 * | tail -$1 | awk 'NR==$ii'`
# mkdir $f_name in some other location
# and some other operations
done
I expected if i do sh forLoopExample.sh 3
the loop runs three times and does the expected operation for all the three folders.
but mkdir
is throwing error.
Kindly help me in solving the error. Thanks in advance.
$ii
in theawk
command isn't doing what you want. You could use double-quotes instead, but it's really better to use awk's-v
option to copy the shell variable into an awk variable (see this Q/A). BTW, I also recommend using shellcheck.net to check your script for other common mistakes. – Gordon Davisson Dec 29 '20 at 10:07