LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-23-2021, 07:42 AM   #1
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
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!
 
Old 05-23-2021, 08:10 AM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,572
Blog Entries: 19

Rep: Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451
I think you still need the echo command to be there. Something like:
ST= $(echo.........|bc)
 
1 members found this post helpful.
Old 05-23-2021, 08:10 AM   #3
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
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

Last edited by GPGAgent; 05-23-2021 at 08:11 AM.
 
Old 05-23-2021, 08:36 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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
 
Old 05-23-2021, 08:38 AM   #5
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

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"
 
Old 05-23-2021, 08:44 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by boughtonp View Post
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
 
1 members found this post helpful.
Old 05-23-2021, 08:48 AM   #7
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs up

Quote:
Originally Posted by pan64 View Post
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.

Last edited by GPGAgent; 05-23-2021 at 09:20 AM.
 
Old 05-23-2021, 09:58 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash script: How to assign variable to an sqlite3 command with variable embedded? ninja6o4 Linux - Software 10 02-15-2015 04:43 PM
[BASH] Assign output to variable(s) useretail Linux - Newbie 4 12-06-2014 06:08 AM
[SOLVED] BASH: Assign a variable to a variable name... c_henry Programming 9 06-04-2012 11:33 AM
bash script: how to assign command ouput to a variable without executing it? bostonantifan Programming 1 02-12-2011 11:55 PM
bash script: how to assign an output (not the result) to a variable? Singing Banzo Programming 8 10-01-2006 06:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration