KSH/bash for File Creation Dynamically











up vote
1
down vote

favorite












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










share|improve this question
























  • 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

















up vote
1
down vote

favorite












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










share|improve this question
























  • 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















up vote
1
down vote

favorite









up vote
1
down vote

favorite











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










share|improve this question















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







shell-script ksh






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 at 23:11









Rui F Ribeiro

38.3k1477127




38.3k1477127










asked Apr 6 '15 at 6:03









Aman

1611215




1611215












  • 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




















  • 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


















cd -- "$TMPDIR"; mkdir -p -- "${array_FileName_category[@]}"
– mikeserv
Apr 6 '15 at 6:09




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




@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






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




@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






Try something like: 'b=$array_FileName_category[$a]; c=${b}_file' and so on
– Romeo Ninov
Apr 6 '15 at 9:29












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










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






share|improve this answer























  • 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








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f194564%2fksh-bash-for-file-creation-dynamically%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










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






share|improve this answer























  • 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















up vote
2
down vote



accepted










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






share|improve this answer























  • 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













up vote
2
down vote



accepted







up vote
2
down vote



accepted






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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 6 '15 at 11:10

























answered Apr 6 '15 at 10:08









Romeo Ninov

5,01431627




5,01431627












  • 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


















  • 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
















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






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




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




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




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




1




It is working as my requirement Thanks dude for help :)
– Aman
Apr 6 '15 at 11:22




It is working as my requirement Thanks dude for help :)
– Aman
Apr 6 '15 at 11:22


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f194564%2fksh-bash-for-file-creation-dynamically%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Morgemoulin

Scott Moir

Souastre