LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Bash: Assign output of a command to a variable (https://www.linuxquestions.org/questions/linux-software-2/bash-assign-output-of-a-command-to-a-variable-4175695500/)

GPGAgent 05-23-2021 07:42 AM

Bash: Assign output of a command to a variable
 
I have a nice bit of code to convert hh:mm:ss into seconds like this:
Code:

jonke@charlie:~$ echo "1:01:0" | sed -E 's/(.*):(.+):(.+)/\1*3600+\2*60+\3/;s/(.+):(.+)/\1*60+\2/' | bc
3660
jonke@charlie:~$

Converts one hour and one minute into seconds.


How can I assign this to a variable?


I've tried:
Code:

jonke@charlie:~$ ST="1:01:0" | sed -E 's/(.*):(.+):(.+)/\1*3600+\2*60+\3/;s/(.+):(.+)/\1*60+\2/' | bc
jonke@charlie:~$ echo $ST
10:01
jonke@charlie:~$ ST=$("1:01:0" | sed -E 's/(.*):(.+):(.+)/\1*3600+\2*60+\3/;s/(.+):(.+)/\1*60+\2/' | bc)
bash: 1:01:0: command not found
jonke@charlie:~$

and I've tried other variations of this with no luck, it must be very simple, but I'm missing the point!

hazel 05-23-2021 08:10 AM

I think you still need the echo command to be there. Something like:
ST= $(echo.........|bc)

GPGAgent 05-23-2021 08:10 AM

Panic over guys and gals - I have real trouble with bash, don't use it ofetn enough but I finally figured out this, use a function

Code:

seconds(){
# calculates no of second from time in any format
# usage Example: echo $(seconds 1:01) will return 61
echo $1 | sed -E 's/(.*):(.+):(.+)/\1*3600+\2*60+\3/;s/(.+):(.+)/\1*60+\2/' | bc
}

Unless of course there's a better way

pan64 05-23-2021 08:36 AM

I would use awk, something like this:
Code:

echo $1 | awk -F: ' NF==3 { print ($1*3600 + $2*60 + $3) } NF==2 { print ($1*60 + $2) }'
From the other hand you do not need that echo $( ), seconds 1:01:0 will work without that

boughtonp 05-23-2021 08:38 AM


 
No need to use sed/bc when GNU coreutils can already parse and convert dates...

Code:

HMS="1:01:00"
seconds=$(date --date="1970-01-01 $HMS +0000" '+%s')
echo "$seconds"


pan64 05-23-2021 08:44 AM

Quote:

Originally Posted by boughtonp (Post 6253202)
No need to use sed/bc when GNU coreutils can already parse and convert dates...

Code:

HMS="1:01:00"
seconds=$(date --date="1970-01-01 $HMS +0000" '+%s')
echo "$seconds"


And also there is no need to use any external command, bash can do it:
Code:

VAR="1:01"
A=(${VAR//:/ })
[[ ${#A[@]} -eq 3 ]] && sec=$((${A[0]}*3600 + ${A[1]}*60 + ${A[2]}))
[[ ${#A[@]} -eq 2 ]] && sec=$((${A[0]}*60 + ${A[1]}))
echo $sec # if you wish
# but you can use the variable sec in your script without this echo


GPGAgent 05-23-2021 08:48 AM

Quote:

Originally Posted by pan64 (Post 6253203)
And also there is no need to use any external command, bash can do it:
Code:

VAR="1:01"
A=(${VAR//:/ })
[[ ${#A[@]} -eq 3 ]] && sec=$((${A[0]}*3600 + ${A[1]}*60 + ${A[2]}))
[[ ${#A[@]} -eq 2 ]] && sec=$((${A[0]}*60 + ${A[1]}))
echo $sec # if you wish
# but you can use the variable sec in your script without this echo


Neat, cheers!

BTW I've used your first example!

But I've changed back to sed, I don't want to put in the hours every time.

MadeInGermany 05-23-2021 09:58 AM

A solid alternative to
Code:

A=(${VAR//:/ })
is
Code:

IFS=":" read -a A <<< $VAR
Especially bash stumbles over its misfeature to interpret numbers with leading zero as octal; 08 or 09 gives an error in arithmetic context.
A work-around is to prefix it with 10#
Code:

sec=$(( 10#${A[-1]} + 60 * 10#${A[-2]} + 3600 * 10#${A[-3]} ))
The negative indices require bash 4.2 or higher.


All times are GMT -5. The time now is 07:21 PM.