I'm trying to use a url piped from tshark into a while loop.

while read line ; do 
echo "$line"
ffmpeg -i "$line" -c copy "filename"
done < <(tshark -i tun0 -B 50 -P -V -q -l -Y 'http matches "(?<=\[Full request URI: )(http://mywebsite.com/file.*)(?=\])"' 2>&1 | grep --line-buffered -Po "(?<=\[Full request URI: )(http://mywebsite.com/file.*)(?=\])" | unbuffer -p uniq)

Inside the loop, I'm able to echo $line just fine; and it looks like this:


But for some reason, I'm unable to use this same "line" variable to feed ffmpeg, inside the same while loop. Doing

ffmpeg -i "$line" -c copy "filename"

results in (all quotes are accurately copy pasted)

[http @ 0x1ab5ec0] HTTP error 400 Bad Request:
Server returned 400 Bad Request5345932&fmt=5&app_id=214748364&range=20-30&etsp=1456779359&hmac=35B2lA6D0zfR2DmfdPS4ZcilYxg

On the other hand, copying the url (from the echo output), double quoting it and using the same ffmpeg command in a terminal works perfectly.

Also, for some reason, the command is truncated when running the script with -xv, in such way that it does not show the full "+ ffmpeg -i 'http://...." line as it should.

  • You want to make sure you escape the ampersands, but your code is a pain to read all on one line as it is, so I am not going to try. It will involve nested quoting most likely, or some well placed backslashes. Please edit your doe to be more readable. – Gregg Leventhal Feb 29 '16 at 20:17
  • Sorry about the bad readability, just edited. Do you mean I should run the variable value through sed to escape each ampersand ? – dédé Feb 29 '16 at 20:29

Edit: It appears the grep is capturing a newline or carriage return which is fine when you submit it as a one off command but not fine in the loop. Add tr -d '\r'

  • That is exactly what OP complains doesn't work... – vonbrand Feb 29 '16 at 21:17
  • @vonbrand, he edited his code after I submitted that, it didn't look like that at the time. – Gregg Leventhal Feb 29 '16 at 21:27
  • Let's see the full output please. – Gregg Leventhal Feb 29 '16 at 22:05
  • Update the question with this info, don't use a comment, its impossible to read. Are you are using the same URL for the -c copy argument? If so, you need to quote properly there as well, this URL contains some special characters which will affect the shell. – Gregg Leventhal Feb 29 '16 at 22:13
  • What are all those extra single quotes and backslashes? Are you sure your grep command is extracting a single url with no additional cruft? Did you try removing the line buffering stuff? – Gregg Leventhal Feb 29 '16 at 23:31

It turns out that grep (or tshark ?) is capturing a carriage return into the $line variable, as pointed out by Gregg Leventhal. This carriage return needs to be stripped off.

Replacing $line inside the loop with$(echo "$line" | tr -d '\r') did the trick for me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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