Script Shell: Diplay date of two minutes (period)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script Shell: Diplay date of two minutes (period)
# 1  
Old 09-08-2015
Script Shell: Diplay date of two minutes (period)

Hi all,

Here is my script:
Code:
  #!/bin/sh 
     while :; 
     do    
        sleep 1    
        date
    done

Please, how can i change this script, i'd like to display the time only of two minutes (period) and exit ? Is that possible ?


Thank you so much for help.
Bests regards.
# 2  
Old 09-08-2015
You'd like your script to run for 2 minutes during which you display date and time each second? One way to solve this is starting a background process that sleeps for 2 minutes and check for its existance:
Code:
#!/bin/sh
sleep 120 &
SLEEPER_PID=$!
while ps -p $SLEEPER_PID >/dev/null; do
   sleep 1
   date
done

Edit: on a very busy system there may be a problem that during the 1 second of sleep another process with the same PID as your sleeper was started after the sleeper finished, so use this method with caution. A saver way would be to create a file and delete it after 2 minutes.
Code:
#!/bin/sh
F=/tmp/$0.$$
touch $F
( sleep 120 ; rm $F ) &
while [ -f $F ]; do
   sleep 1
   date
done


Last edited by cero; 09-08-2015 at 11:36 AM..
# 3  
Old 09-08-2015
If i understand that you want:

Code:
 i=0;while : ;do sleep 1;i=$((i+1));date;if [ $i == 120 ];then break;fi;done

# 4  
Old 09-08-2015
Why not
Code:
i=120; while ((i--)); do sleep 1; date; done

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting week start date and end date based on custom period start dates

Below are my custom period start and end dates based on a calender, these dates are placed in a file, for each period i need to split into three weeks for each period row, example is given below. Could you please help out to achieve solution through shell script.. File content: ... (2 Replies)
Discussion started by: nani2019
2 Replies

2. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

3. Shell Programming and Scripting

Shell script to compare two files of todays date and yesterday's date

hi all, How to compare two files whether they are same are not...? like i had my input files as 20141201_file.txt and 20141130_file2.txt how to compare the above files based on date .. like todays file and yesterdays file...? (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

4. UNIX for Dummies Questions & Answers

Adding hours and minutes to current date (Only to date not to time)

Hi, I want to add some hours and minutes to the current date. For example, if the current date is "July 16, 2012 15:20", i want to add 5 hours 30 minutes to "July 16, 2012 00:00" not to "July 16, 2012 15:20". Please help. Thanks! (4 Replies)
Discussion started by: manojgarg
4 Replies

5. Shell Programming and Scripting

Need help in Shell Script comparing todays date with Yesterday date from Sysdate

Hi, I want to compare today's date(DDMMYYYY) with yesterday(DDMMYYYY) from system date,if (today month = yesterday month) then execute alter query else do nothing. The above requirement i want in Shell script(KSH)... Can any one please help me? Double post, continued here. (0 Replies)
Discussion started by: kumarmsk1331
0 Replies

6. UNIX for Dummies Questions & Answers

Run the shell script for every 15 minutes?

I want to run my shell script for every 15 minutes starting from 12:20AM. I am passing parameter GA to shell script. Does this work? Any one please comment on this? 20 0-23/15 * * * xyz.sh 'GA' > xyz.log 2>&1 (9 Replies)
Discussion started by: govindts
9 Replies

7. Shell Programming and Scripting

How to increment a user defined date value in the DATE format itself using shell script?

I need to increment a date value through shell script. Input value consist of start date and end date in DATE format of unix. For eg. I need increment a date value of 1/1/09 to 31/12/09 i.e for a whole yr. The output must look like 1/1/09 2/2/09 . . . 31/1/09 . . 1/2/09 . 28/2/09... (1 Reply)
Discussion started by: sunil087
1 Replies

8. Shell Programming and Scripting

To run a shell script for a definite period

Hi, I would like to invoke a shell script with period (in seconds) as a parameter and want it to run olny for that period. The script should come out after that period even some work is going inside the script. Regards (2 Replies)
Discussion started by: sanjay1979
2 Replies

9. Shell Programming and Scripting

UNIX Shell script to chec timeout period when collecting files in directory - HELP

Hi, I have a shell script which is to perform a check if all 4 particular type of files exists in a directory. If ALL 4 files are present within a specific Timeframe, then tar these files and zip it. If not all 4 files are present in the directory after the specific timeframe, then tar... (1 Reply)
Discussion started by: Danny Fang
1 Replies

10. Shell Programming and Scripting

C Shell Script to convert a number into minutes

Could anyone tell me how to write a C shell script according to the following requirement. Write a C shell script convertmin which will read in a number, thought of as representing minutes, and print out the number of hours/minutes it represents so: Note: you are required to check exception... (1 Reply)
Discussion started by: Ringo
1 Replies
Login or Register to Ask a Question