Page 1 of 1 [ 7 posts ] 

StuartN
Veteran
Veteran

User avatar

Joined: 20 Jan 2010
Age: 61
Gender: Male
Posts: 1,569

08 Jul 2011, 11:32 am

Sorry that this is such a specific question, but maybe it will interest someone.

The line result=$(mplayer -vo null -ao null Title.vob) in Ubuntu places the multi-line standard output of the command (mplayer) into the variable (result) with the newlines intact, just as the Bash manpage says: "the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting."

The exact same script in Fedora places all the command output into a single line, i.e. it strips out all the newlines, which causes the script to fail (because it is expecting data on separate lines).

Is there any way to force the $(command) output to retain the newlines?



JohnOldman
Velociraptor
Velociraptor

User avatar

Joined: 30 Mar 2011
Age: 39
Gender: Male
Posts: 448
Location: Midwest USA (Switzerland is Where the Heart Is)

08 Jul 2011, 1:20 pm

If you're running a script with the #!/bin/sh shebang, it could have to do with the fact that Ubuntu uses the dash shell as it's /bin/sh, whereas fedora (I believe) uses bash. So different shells are behaving differently.

I'm not a fedora user, so I can't say for sure.



JohnOldman
Velociraptor
Velociraptor

User avatar

Joined: 30 Mar 2011
Age: 39
Gender: Male
Posts: 448
Location: Midwest USA (Switzerland is Where the Heart Is)

08 Jul 2011, 1:21 pm

You could try using the alternate backtick syntax: `command`



JohnOldman
Velociraptor
Velociraptor

User avatar

Joined: 30 Mar 2011
Age: 39
Gender: Male
Posts: 448
Location: Midwest USA (Switzerland is Where the Heart Is)

08 Jul 2011, 2:05 pm

While fooling around I realized that it matters how you are using the variable:
echo $result leads to stripping of newlines while echo "$result" keeps the newlines.



JohnOldman
Velociraptor
Velociraptor

User avatar

Joined: 30 Mar 2011
Age: 39
Gender: Male
Posts: 448
Location: Midwest USA (Switzerland is Where the Heart Is)

08 Jul 2011, 2:07 pm

...because without quotes the shell does word-splitting as the manpage you quoted mentions



JohnOldman
Velociraptor
Velociraptor

User avatar

Joined: 30 Mar 2011
Age: 39
Gender: Male
Posts: 448
Location: Midwest USA (Switzerland is Where the Heart Is)

08 Jul 2011, 2:23 pm

By the way, I really enjoy this stuff, so when you have a shell scripting or programming question (C, Java or Go) post it and I'll jump on it.



StuartN
Veteran
Veteran

User avatar

Joined: 20 Jan 2010
Age: 61
Gender: Male
Posts: 1,569

08 Jul 2011, 3:40 pm

JohnOldman wrote:
While fooling around I realized that it matters how you are using the variable:
echo $result leads to stripping of newlines while echo "$result" keeps the newlines.


You are exactly right here, although the details confused me. Bash is word splitting the output - that is to say any groups of characters in $IFS (usually space, tab and newline) are replaced by a single space. echo $result word splits, while echo "$result" preserves all the newlines (and tabs, if there are any).

The confusing detail was that $IFS had been modified to something else (i.e. did not include newline) in the Ubuntu installation. The quoted $result works fine in both cases. My script shows the video and audio streams in a DVD ripped VOB file, along with the aspect ratio and framerate, to pick settings for an ffmpeg encode:

#!/bin/bash

# Identify the characteristics of a video file using mplayer

for file in "$@" ; do
echo $file
result=$(mplayer -identify -frames 0 -v "$file" 2> /dev/null)
echo "$result" | grep -e "^VIDEO" -e "^AUDIO" -e "==>"

framerate=$(echo $result | grep -o "[0-9]*\.[0-9]* fps")
echo "framerate $framerate"

aspect=$(echo $result | grep -o "aspect [0-9]")
if [[ "$aspect" == "aspect 2" ]] ; then echo -n "Fullscreen (standard) " ; fi
if [[ "$aspect" == "aspect 3" ]] ; then echo -n "Widescreen " ; fi
echo $aspect

echo
done


JohnOldman wrote:
By the way, I really enjoy this stuff, so when you have a shell scripting or programming question (C, Java or Go) post it and I'll jump on it.


Thanks - this forum has never failed to provide a useful answer to my queries.