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
shell-script ksh
|
show 1 more comment
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
shell-script ksh
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 useksh
directly for the files, I think - is thisksh93
or ATTksh
?tee
ortouch
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
|
show 1 more comment
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
shell-script ksh
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
shell-script ksh
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 useksh
directly for the files, I think - is thisksh93
or ATTksh
?tee
ortouch
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
|
show 1 more comment
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 useksh
directly for the files, I think - is thisksh93
or ATTksh
?tee
ortouch
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
|
show 1 more comment
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
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
|
show 1 more comment
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
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
|
show 1 more comment
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
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
|
show 1 more comment
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
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
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
|
show 1 more comment
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
|
show 1 more comment
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 thisksh93
or ATTksh
?tee
ortouch
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