Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
echo "enter e-mail address"
read fname1

echo "enter number of hours"
read fname2

START = $(date +"%T")
END = $(date +"%T")
Diff = $ (($END - $ START))

if [$Diff -eq $fname2]

some lines to execute

In my above code segment I need to get End date from user input. Which means if user press 2 END should be now time + 2.

Anyone who can help really appreciate.


Regards,
Do B.
Posted

1 solution

If I am understanding your question correctly you want to add an amount of hours to the end-time.

You can do that with the following command:
futureDate=$(date -d "+2 hours")

The -d option is quite versitile and powerful. It allows you to enter a very human-readable command, like "-5 days", "+3 months", "9 days ago".

To get user input, use the read command:
read startDate duration

By default the read command will read a single line from the standard input (usually the keyboard). The first word the user enters will be assigned to the first variable name, the second word to the second variable name, and so forth. If the user entered more words than the amount of variables you supplied, all the remaining words will be assigned to the last variable.

An example:
read var1 var2 var3 var4

User-input:
The quick brown fox jumps over the lazy dog.

Result:
var1 = The
var2 = quick
var3 = brown
var4 = fox jumps over the lazy dog.

You can add an instruction-message to your read command to inform the user what you expect by using the -p option:
read -p 'Enter the start date and the duration: ' startDate duration
 
Share this answer
 
v2
Comments
Strider87 13-Sep-13 17:10pm    
Instead of "+2 hours". I need to take that value from user.
Member 9548724 14-Sep-13 4:48am    
Solution updated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900