Operating System - HP-UX
1830899 Members
3380 Online
110017 Solutions
New Discussion

Script to create 200 test files in my directory with a timestamp different by 1 second or so...

 
SOLVED
Go to solution
Shaun Aldrich
Frequent Advisor

Script to create 200 test files in my directory with a timestamp different by 1 second or so...

Hello everyone,

Does anyone know how to create a Script which will create 200 test files in my directory with a timestamp different by 1 second or so... I would greatly appreciate any help...

I believe this would be done with a For loop..

Shaun Aldrich
SAldrich@chaptersinc.com
14 REPLIES 14
Scott D. Allen
Regular Advisor

Re: Script to create 200 test files in my directory with a timestamp different by 1 second or so...

shaun,

you can try something like this....

#!/bin/sh
i=0
while [ $i -lt 200 ]
do
touch file${i}
sleep 1
i=`expr $i + 1`
done

--Scott
"Sometimes the devil you know is better than the devil you don't know."
Steve Sauve
Frequent Advisor

Re: Script to create 200 test files in my directory with a timestamp different by 1 second or so...

I would do something like this:

count=0
while [ $count != 1001 ]
do
touch ./file.$count
sleep 1
count=`expr ${count} + 1`
done

Then just change the sleep line based on the time delay you want.

Hope this helps,
Steve
Tom Danzig
Honored Contributor

Re: Script to create 200 test files in my directory with a timestamp different by 1 second or so...

How about:

#!/usr/bin/csh

@ count = 0

while ($count < 60)
if($count < 10) set count = "0${count}"
touch -t 082012${count} xxx${count}.tmp
touch -t 082112${count} yyy${count}.tmp
@ count ++
end
Shaun Aldrich
Frequent Advisor

Re: Script to create 200 test files in my directory with a timestamp different by 1 second or so...

Thanks a lot for your response Scott... What does the following line do?

i=`expr $i + 1`

What is the expr? Do I need to replace that with anything? Sorry about all the questions but I am really rusty on scripting...

Do I need to do anything else to it?

Thanks for you help...

Shaun Aldrich
SAldrich@chaptersinc.com
John Palmer
Honored Contributor
Solution

Re: Script to create 200 test files in my directory with a timestamp different by 1 second or so...

Or using shell integer arithmetic:-

#!/usr/bin/sh

let I=1

while (( I < 201 ));
do
touch file${I}
let I=I+1
sleep 1
done
Steve Sauve
Frequent Advisor

Re: Script to create 200 test files in my directory with a timestamp different by 1 second or so...

The expr just lets you up the variable by one (which is used to name the next file).

Hope this helps,
Steve
Scott D. Allen
Regular Advisor

Re: Script to create 200 test files in my directory with a timestamp different by 1 second or so...

Shaun,
Steve's right. It just increments the counter. It could be more sexy if you use csh or ksh.
--Scott
"Sometimes the devil you know is better than the devil you don't know."
James R. Ferguson
Acclaimed Contributor

Re: Script to create 200 test files in my directory with a timestamp different by 1 second or so...

Shaun:

All of the responses given to your question do the job as you requested. At the risk of nit-picking, I thought that I might point out that the timestamps that you will see with a 'ls' command only show precision of one-minute.

If you aren't impatient, you could change the sleep interval to 60+ seconds. Otherwise, you could use the 'touch' command with its '-t' option to create files that you can more readily distinguish.

...JRF...
Shaun Aldrich
Frequent Advisor

Re: Script to create 200 test files in my directory with a timestamp different by 1 second or so...

Thanks for your help everyone...

I just created the following script and called it test1.

#!/usr/bin/ksh
i=0
while [ $i -lt 200 ]
do
touch file${i}
sleep 1
i=`expr $i + 1`
done

when I execute it I get the following error message:

./test1[4]: $i: unknown test operator

Does anyone have any idea why?

When I do an ls on the directory where I execute the script I now have two listings. Both the script test1 and a blank file called file0.

Any ideas are greatly appreciated.

Shaun Aldrich

Shaun Aldrich
Frequent Advisor

Re: Script to create 200 test files in my directory with a timestamp different by 1 second or so...

Thanks for your help everyone...

I just created the following script and called it test1.

#!/usr/bin/ksh
i=0
while [ $i -lt 200 ]
do
touch file${i}
sleep 1
i=`expr $i + 1`
done

when I execute it I get the following error message:

./test1[4]: $i: unknown test operator

Does anyone have any idea why?

When I do an ls on the directory where I execute the script I now have two listings. Both the script test1 and a blank file called file0.

Any ideas are greatly appreciated.

Shaun Aldrich

Rick Garland
Honored Contributor

Re: Script to create 200 test files in my directory with a timestamp different by 1 second or so...

Put some double quotes around the $i and the 200.

while [ "$i" -lt "200" ]
Anthony deRito
Respected Contributor

Re: Script to create 200 test files in my directory with a timestamp different by 1 second or so...

Shaun, [ $i -lt 200 ] is an arithmetic conditional. Use (( )) to evaluate relational operators in an arithmetic conditional. The result is NOT a textual result, it just sets an exit status according to the truth of the expression. 0 for true or 1 for false. So for example,

((${i} < 200))
if [ $? = 0 ];then
echo "the exp is true"
[do whatever]
else
echo "the exp is false"
[do whatever]
fi

Tony

John Palmer
Honored Contributor

Re: Script to create 200 test files in my directory with a timestamp different by 1 second or so...

Shell integer arithmetic and the let construct are easier to understand and are more efficient than string manipulation. See my example above.
Steve Sauve
Frequent Advisor

Re: Script to create 200 test files in my directory with a timestamp different by 1 second or so...

Make sure it's i=0 (as in zero)...

Silly, but it could be the problem.
Steve