1

I have a directory name tmp on that directory on basis of FileName Category I wanted to create 'n' number of files dynamically.

   if [ ! -d tmp ]
then
    mkdir tmp
fi

TEMPDIR=$HOME/tmp

cd $TEMPDIR

array_FileName_category[0]="File1"
array_FileName_category[1]="File2"
array_FileName_category[2]="File3"
array_FileName_category[3]="File4"
a=0
while [ $a -le 9 ]
do
### Creating Source Files
${array_FileName_category[$a]}_file=$TEMPDIR/${array_FileName_category[$a]}_file_$$
a=`expr $a + 1`
done

but this is not working and giving the following errors:

File Not found with array prefix.
and the other error is for zero index of array ${array_FileName_Category[$a]} is not working

My Desired Output is: If there is FileCategory 'Sports' then at tmp directory using the while loop it will create a file with prefix Sports

6
  • cd -- "$TMPDIR"; mkdir -p -- "${array_FileName_category[@]}"
    – mikeserv
    Apr 6, 2015 at 6:09
  • @mikeserv mkdir -p will create file or a directory
    – Aman
    Apr 6, 2015 at 6:16
  • Sorry, obviously, directiories. I could swear it said it said directories. Ok. Well, you can use ksh directly for the files, I think - is this ksh93 or ATT ksh? tee or touch would create the files, too, by the way. touch would be much safer.
    – mikeserv
    Apr 6, 2015 at 6:19
  • @mikeserv yes it is ksh93
    – Aman
    Apr 6, 2015 at 6:21
  • Try something like: 'b=$array_FileName_category[$a]; c=${b}_file' and so on Apr 6, 2015 at 9:29

1 Answer 1

2

Because arrays management in korn shell is far away from other programming languages you can try something like: Instead of this:

while [ $a -le 9 ]
do
${array_FileName_category[$a]}_file=$TEMPDIR/${array_FileName_category[$a]}_file_$$
a=`expr $a + 1`
done

try this:

while [ $a -le 9 ]
do
b=$array_FileName_category[$a]
touch $TEMPDIR/${b}_file_$$
a=`expr $b + 1`
done

P.S. And IMHO for loop will be better for this purpose

6
  • after modifying your answer in this way if [ ! -d tmp ] then mkdir tmp fi TEMPDIR=$HOME/tmp cd $TEMPDIR a=0 array_FileName_category[0]="File1" array_FileName_category[1]="File2" array_FileName_category[2]="File3" array_FileName_category[3]="File4" echo ${array_FileName_category[@]} while [ $a -le 4 ] do b=${array_FileName_category[$a]} ${b}_file=$TEMPDIR/${b}_file_$$ echo ${b} a=expr $a + 1 done it is still not working
    – Aman
    Apr 6, 2015 at 10:19
  • It is giving error File1_file=../tmp/File1_file_33555804: not found. which means it is not taking as creation of file rather it is taking assignment as a file.
    – Aman
    Apr 6, 2015 at 10:21
  • If you want to create a file see my changes in the script Apr 6, 2015 at 10:24
  • this time there is not any error message but on ls -ltr the files are not getting listed , means files count is displaying 8 but there is not any file.
    – Aman
    Apr 6, 2015 at 11:06
  • 1
    It is working as my requirement Thanks dude for help :)
    – Aman
    Apr 6, 2015 at 11:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .