Script to create files
am 28.01.2008 11:04:11 von laxman
hi everybody,
i want to write a script, to create files like log1 log2
log3........logn this files automatically has to be created what is
the script for it in linux
it is very urgent please help me
Re: Script to create files
am 28.01.2008 11:31:20 von Stephane CHAZELAS
On Mon, 28 Jan 2008 02:04:11 -0800 (PST), laxman wrote:
[...]
> i want to write a script, to create files like log1 log2
> log3........logn this files automatically has to be created what is
> the script for it in linux
>
> it is very urgent please help me
touch log{1..n}
works in zsh and recent versions of bash and ksh93.
zsh also allows:
touch log{01..99}
to create files that sort numerically and alphabetically the
same way.
On GNU systems, you can also do:
seq -f log%02g 99 | xargs touch
--
Stephane
Re: Script to create files
am 28.01.2008 11:55:51 von laxman
On Jan 28, 3:31=A0pm, Stephane Chazelas
wrote:
> On Mon, 28 Jan 2008 02:04:11 -0800 (PST), laxman wrote:
>
> [...]
>
> > i want to write a script, to create =A0files like log1 log2
> > log3........logn =A0this files automatically has to be created what is
> > the script for it =A0in linux
>
> > it is very urgent please help me
>
> touch log{1..n}
>
> works in zsh and recent versions of bash and ksh93.
>
> zsh also allows:
>
> touch log{01..99}
>
> to create files that sort numerically and alphabetically the
> same way.
>
> On GNU systems, you can also do:
>
> seq -f log%02g 99 | xargs touch
>
> --
> Stephane
thanks a lot Mr.Stephane
Re: Script to create files
am 28.01.2008 12:07:26 von laxman
On Jan 28, 3:55=A0pm, laxman wrote:
> On Jan 28, 3:31=A0pm, Stephane Chazelas
> wrote:
>
>
>
>
>
> > On Mon, 28 Jan 2008 02:04:11 -0800 (PST), laxman wrote:
>
> > [...]
>
> > > i want to write a script, to create =A0files like log1 log2
> > > log3........logn =A0this files automatically has to be created what is=
> > > the script for it =A0in linux
>
> > > it is very urgent please help me
>
> > touch log{1..n}
>
> > works in zsh and recent versions of bash and ksh93.
>
> > zsh also allows:
>
> > touch log{01..99}
>
> > to create files that sort numerically and alphabetically the
> > same way.
>
> > On GNU systems, you can also do:
>
> > seq -f log%02g 99 | xargs touch
>
> > --
> > Stephane
>
> thanks a lot Mr.Stephane- Hide quoted text -
>
> - Show quoted text -
Mr.Stephane it is working fine but whenever i run the script it has to
create a file.....example when i run it 1st time it has to create
log1....2nd time log2 .......like that it has to create but it is
creating all at once will it possible
thanks in advance
Re: Script to create files
am 28.01.2008 12:18:27 von Stephane CHAZELAS
On Mon, 28 Jan 2008 03:07:26 -0800 (PST), laxman wrote:
[...]
>> > > i want to write a script, to create files like log1 log2
>> > > log3........logn this files automatically has to be created what is
>> > > the script for it in linux
[...]
> Mr.Stephane it is working fine but whenever i run the script it has to
> create a file.....example when i run it 1st time it has to create
> log1....2nd time log2 .......like that it has to create but it is
> creating all at once will it possible
[...]
Ok, I hadn't understood your question correctly it would seem.
You could do in your script (assuming it's a POSIX sh script).
log_dir=/var/log/myscript
log_prefix=log
n=1
mkdir -p -- "$log_dir" || exit
set -C
until
{
command exec 3> "$log_dir/$log_prefix$n"
} 2> /dev/null
do
n=$(($n + 1))
done
# then use:
echo log line >&3
# to add a line to the log file.
The above relies on set -C that prevents redirection to
overwrite an existing file, so it keeps trying with "n"
incremented each time until the redirection succeeds.
"command" above is to turn off the special behavior of "exec"
that would otherwise exit the script upon failure.
--
Stephane
Re: Script to create files
am 28.01.2008 13:28:58 von laxman
On Jan 28, 4:18=A0pm, Stephane Chazelas
wrote:
> On Mon, 28 Jan 2008 03:07:26 -0800 (PST), laxman wrote:
>
> [...]>> > > i want to write a script, to create =A0files like log1 log2
> >> > > log3........logn =A0this files automatically has to be created what=
is
> >> > > the script for it =A0in linux
> [...]
> > Mr.Stephane it is working fine but whenever i run the script it has to
> > create a file.....example when i run it 1st time it has to create
> > log1....2nd time log2 .......like that it has to create but it is
> > creating all at once =A0will it possible
>
> [...]
>
> Ok, I hadn't understood your question correctly it would seem.
>
> You could do in your script (assuming it's a POSIX sh script).
>
> log_dir=3D/var/log/myscript
> log_prefix=3Dlog
> n=3D1
> mkdir -p -- "$log_dir" || exit
>
> set -C
> until
> =A0 {
> =A0 =A0 command exec 3> "$log_dir/$log_prefix$n"
> =A0 } 2> /dev/null
> do
> =A0 n=3D$(($n + 1))
> done
>
> # then use:
> echo log line >&3
> # to add a line to the log file.
>
> The above relies on set -C that prevents redirection to
> overwrite an existing file, so it keeps trying with "n"
> incremented each time until the redirection succeeds.
>
> "command" above is to turn off the special behavior of "exec"
> that would otherwise exit the script upon failure.
>
> --
> Stephane
Mr.Stephane, Once again thank you for your support
now it is working fine
Re: Script to create files
am 28.01.2008 13:44:46 von Maxwell Lol
laxman writes:
> Mr.Stephane it is working fine but whenever i run the script it has to
> create a file.....example when i run it 1st time it has to create
> log1....2nd time log2 .......like that it has to create but it is
> creating all at once will it possible
That's not common.
Normally people do log files so that they write to "log" and every
period (day, week), the cron system "ages" then, so that log.5 becomes
log.6, log.4 becomes log.5, etc. and log becomes log.1 And a new log
file is created so that "log" is the most recent and log.1 is last
week, etc.
Read the manpage on logrotate
Others like to use a date in the filename, such as log.`date +%y_%m_%d`
that way it's easy to locate log files created within a certain date.
Re: Script to create files
am 29.01.2008 13:08:32 von laxman
On Jan 28, 5:44=A0pm, Maxwell Lol wrote:
> laxman writes:
> > Mr.Stephane it is working fine but whenever i run the script it has to
> > create a file.....example when i run it 1st time it has to create
> > log1....2nd time log2 .......like that it has to create but it is
> > creating all at once =A0will it possible
>
> That's not common.
>
> Normally people do log files so that they write to "log" and every
> period (day, week), the cron system "ages" then, so that log.5 becomes
> log.6, log.4 becomes log.5, etc. and log becomes log.1 And a new log
> file is created so that "log" is the most recent and log.1 is last
> week, etc.
>
> Read the manpage on logrotate
>
> Others like to use a date in the filename, such as log.`date +%y_%m_%d`
>
> that way it's easy to locate log files created within a certain date.
hi Mr.Stephane i gone through logrotate. What you have suggested me
is correct.
i changed the format to log.`date +%y_%m_%d` it is working in linux
Thanks for Suggestions
Re: Script to create files
am 30.01.2008 02:04:28 von Maxwell Lol
laxman writes:
> hi Mr.Stephane i gone through logrotate.
You are welcome, but that's not my name.