Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Okay everyone. I am working on a shell script but I want to use command line arguments instead.

For instance if I enter time 2 hours, the output should be 7200 seconds. This is what I have so far but it is using the read instead of argument. How can I rewrite this as command line argument?

seconds=0

echo -n "Enter number of seconds : "
read seconds

hours=$((seconds / 3600))
seconds=$((seconds % 3600))
minutes=$((seconds / 60))
seconds=$((seconds % 60))

echo "$hours hour(s) $minutes minute(s) $seconds second(s)"
share|improve this question

closed as off-topic by Vojtech Trefny, darent, bodhi.zazen, Rinzwind, Mitch May 2 at 12:12

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – Vojtech Trefny, darent, bodhi.zazen, Rinzwind, Mitch
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

up vote 3 down vote accepted

Just replace seconds with $1, which will match the first argument passed to the script.

If you want to specify the units of the input, then you can reference $2 for this. Note: I am not able to test the if statements right now, syntax may be off:

if [ $2 == "minutes" ]; then
    seconds=$(($1*60))
elif [ $2 == "hours" ]; then
    seconds=$(($1*3600))
else
    seconds=$1
fi
hours=$((seconds / 3600))
seconds=$((seconds % 3600))
minutes=$((seconds / 60))
echo "$hours hour(s) $minutes minute(s) $seconds second(s)"

Usage:

$ ./time.sh 7200
2 hour(s) 0 minute(s) 0 second(s)
$ ./time.sh 2 hours
2 hour(s) 0 minute(s) 0 second(s)
share|improve this answer
    
this works perfect: this is the results but how do I get rid of the $ on the minutes (see where it has 25 then $) show_seconds_for 102354 28 hour(s) 25$ minute(s) 1554 second(s) –  Anu Mathuria May 1 at 21:47
    
this works perfect: this is the results but how do I get rid of the $ on the minutes (see where it has 25 then $) show_seconds_for 102354 28 hour(s) 25$ minute(s) 1554 second(s) On a side note - I know this is crazy but what if I want to specify for instance show_seconds_for 14 minutes or 2 hours. I know I need some work on this one –  Anu Mathuria May 1 at 21:58
1  
I accidentally had an extra $ on the end, removed it now. Could you explain more what you mean about showing seconds? –  Nattgew May 1 at 22:12
    
For instance if I execute the script called show_seconds_for (then followed by just numbers, it works) for instance: show_seconds_for 6544789 but if I execute show_seconds_for 5 hours, it just populates in the seconds field. I want to execute both rather I just put in numbers or if I put in 5 hours, it will convert to hours many seconds or populate in the hour field. hope this make since. right now if I put 5 hours, it will only populate in the second field and not the hour field –  Anu Mathuria May 1 at 23:32
    
I added a section to test for units, may need some adjustments to work. –  Nattgew May 2 at 19:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.