Search code examples
shellshtcshffprobe

pipe ffprobe in sh script


I'm trying to get the audio type in an OSX sh script using ffprobe: ffprobe "$i" |& egrep -ci "vorbis|aac" it works from cli in tcsh but not from script. |& doesn't seem to work from inside script. |& redirects the output from ffprobe, otherwise ffprobe prints out to cli. Any help is appreciated.

##mkv files
for i in "$input"*.mkv ; do
    if [ -e "$i" ] ; then
         ##1st check type(vorbis|aac)
         type=$(/usr/local/bin/ffprobe "$i" |& egrep -i "vorbis|aac")
         echo "Test: $type"
         ##just get audio format
         type=$(/usr/bin/perl -e '$_=@ARGV[0];if (/(aac|vorbis)/ig) {print $1;}' "$type")
         echo "Type: $type"
         exit
    fi
done

error I get is:

command substitution: line 46: syntax error near unexpected token `&'
command substitution: line 46: `/usr/local/bin/ffprobe "$i" |& egrep -i "vorbis|aac"'

Solution

  • You are attempting to use tcsh syntax in a sh script; the fact that this does not work should be unsurprising, although you appear to be unaware that they are two different languages.

    Perhaps the most straightforward immediate solution for your script is to run it in bash instead of sh, where the former supports the tcsh syntax you tried to use, not just coincidentally but because it has loaned many good and some less good ideas from tcsh, too, while preserving full backwards compatibility with POSIX sh.

    Assuming the information you want is printed to standard output py ffprobe, you could also switch to a regular pipe, and keep the sh shebang line. Either way, explicitly invoking the script with sh is less recommended -- just mark the file as executable and invoke it directly, and the system will use whichever interpreter the shebang specifies (and conversely, the shebang will simply be ignored if you explicitly specify an interpreter).

    There is a wide consensus that tcsh should be avoided for both scripting and interactive use -- I would advise you to consider also switching to bash for interactive use, which will unify the syntax you apparently already use for scripts with the syntax for interactive use. In fact, you are currently missing out on one of the attractive features of the shell by using an incompatible interactive shell.

    Tom Christiansen's csh.whynot is the canonical reference criticism of the C shell. While tcsh fixes a good number of the bugs and inconsistencies from csh, many do still remain. Switching to one of the dominant shells will also give you a broader network of help, learning, and troubleshooting resources to support you.