LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-16-2012, 03:52 PM   #1
tmerriam
Member
 
Registered: Dec 2008
Distribution: slackware
Posts: 50

Rep: Reputation: 16
bash/awk: What is going on here?


I have a problem with a line in a bash script I am working on, but I'm not really sure what the problem is. The command works, but my script breaks when the one of the variable names has spaces.

I found this command to use ffmpeg to find the length of a movie in seconds.

Code:
MOVIE=${1}
TIME=$(ffmpeg -i ${MOVIE} 2>&1 | grep "Duration"| cut -d ' ' -f 4 | sed s/,// | awk '{ split($1, A, ":"); split(A[3], B, "."); print 3600*A[1] + 60*A[2] + B[1] }')

echo "Duration: $TIME"
When $MOVIE has spaces in it, it seems that the line TIME=... is completely ignored. I know this because when it works correctly, there is some output from ffmpeg. Instead, bash quits later on because $TIME is not set.

What the heck is going wrong?
 
Old 04-16-2012, 04:06 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You have to use quotes to prevent the positional parameter (argument) from being splitted in two or more space separated fields:
Code:
MOVIE="${1}"
TIME=$(ffmpeg -i "${MOVIE}" 2>&1 | blah blah blah
An aside note: so many piped commands are not really necessary when awk can do it all! Example:
Code:
TIME=$(ffmpeg -i "${MOVIE}" 2>&1 | TZ=UTC0 awk '/Duration/{gsub(/[,:]/," ",$2); print mktime("1970 01 01 " $2)}')

Last edited by colucix; 04-16-2012 at 04:24 PM. Reason: Added awk code
 
Old 04-16-2012, 04:38 PM   #3
tmerriam
Member
 
Registered: Dec 2008
Distribution: slackware
Posts: 50

Original Poster
Rep: Reputation: 16
That helped a little bit, but it still doesn't work. The line runs now, but ffmpeg just prints out the first word of the filename, complains that it doesn't exist, and quits. So the spaces are still tripping it up somehow.
 
Old 04-16-2012, 04:45 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Please, can you post an example of your shell session? I'll try to reproduce the error to see what's going on. A working example:
Code:
$ cat test.sh
#!/bin/bash
#
MOVIE="${1}"
TIME=$(ffmpeg -i "${MOVIE}" 2>&1 | TZ=UTC0 awk '/Duration/{gsub(/[,:]/," ",$2); print mktime("1970 01 01 " $2)}')
echo $TIME
$ ./test.sh "video name with spaces.avi"
3102
$ ffmpeg -i "video name with spaces.avi" 2>&1 | grep Duration
  Duration: 00:51:42.76, start: 0.000000, bitrate: 1353 kb/s
 
Old 04-16-2012, 05:06 PM   #5
tmerriam
Member
 
Registered: Dec 2008
Distribution: slackware
Posts: 50

Original Poster
Rep: Reputation: 16
Actually, I take that back, it does work. I was misreading a different error from further on in the script. I fixed that and it works now.

Thank you so much!
 
Old 04-16-2012, 05:18 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Nice! You can mark this thread as SOLVED if you wish.
 
Old 04-16-2012, 06:19 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
And maybe also thank the man.
Every time I read a post from colucix I learn something new ...
 
  


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
AWK/BASH: get nth line from a file by getline feed to actions in a same awk line cristalp Programming 3 11-23-2011 11:38 AM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 09:06 PM
bash script and awk phonebooth Programming 8 03-27-2010 12:51 AM
need help with bash and awk stympman Programming 3 11-01-2008 06:12 PM
Help with Bash (grep/awk/etc) piercey Programming 12 02-27-2008 10:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:22 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