user3248174
user3248174

Reputation: 1

Shell scripting : split an output and assign it to different variables

I have an output of a command/script as below...

63-19:48:47

I want a script to read it and print it as 63days 19hours 48mins 47seconds.

If it is just 19:48:47, then I want the same script to print as 19hours 48mins 47seconds.

Is there a way to split the output and assign it to different variables to achieve the abouve results or thru some other emans?...Please help me out.

Upvotes: 0

Views: 256

Answers (1)

Igor Chubin
Igor Chubin

Reputation: 64563

echo 63-19:48:47 | tr ':-' ' ' |read a b c d

Or if number of parameters is not strict (3 or 4):

set `echo 63-19:48:47 | tr ':-' ' '`
if [ -z "$4" ]
then
  day="$1"
  shift
fi
hour="$1"
min="$2"
sec="$3"

Upvotes: 3

Related Questions