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 ]
    mkdir tmp



while [ $a -le 9 ]
### Creating Source Files
a=`expr $a + 1`

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

  • cd -- "$TMPDIR"; mkdir -p -- "${array_FileName_category[@]}" – mikeserv Apr 6 '15 at 6:09
  • @mikeserv mkdir -p will create file or a directory – Aman Apr 6 '15 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 '15 at 6:19
  • @mikeserv yes it is ksh93 – Aman Apr 6 '15 at 6:21
  • Try something like: 'b=$array_FileName_category[$a]; c=${b}_file' and so on – Romeo Ninov Apr 6 '15 at 9:29

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 ]
a=`expr $a + 1`

try this:

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

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

  • 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 '15 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 '15 at 10:21
  • If you want to create a file see my changes in the script – Romeo Ninov Apr 6 '15 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 '15 at 11:06
  • 1
    It is working as my requirement Thanks dude for help :) – Aman Apr 6 '15 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.