0

What does the 0 2>&1 >/dev/null below mean?

rawout=$(HandBrakeCLI -i /dev/dvd -t 0 2>&1 >/dev/null)

also below,

count=$(echo $rawout | grep -Eao "\\+ title [0-9]+:" | wc -l)

what does the -Eao "\\+ title [0-9]+:" | wc -l) imply?

1 Answer 1

2

According to HandBreak cli documentation:

The -t 0 option means "scan all titles only".

Additionally, 2>&1 >/dev/null is a linux terminal output redirection of your errors (error stream) to your outpus stream (which displays general command and output messages to the terminal), and both of them redirected to /dev/null special device file wich discards whatever is sent to it.
It is a method of making a command silent and not show you any messages.

The expression grep -Eao "\\+ title [0-9]+:" | wc -l is a combination of the output of grep -Eao "\\+ title [0-9]+:" sent to the wc -l command, which counts the number of lines in a file or a command output.

In other words: count how many lines the command grep -Eao "\\+ title [0-9]+:" outputs.
More about wc in the wc manpage.

The grep util performs a search of a given expression pattern inside a file or a command output:
The -a option means "handle this data given as text data".
The option -E is a deprecated option of grep which sometimes used to treat patterns as extended regex.
lastly, -o means "show only matching results of a whole matching line".

Overall the use of grep here seems to be a searching mechanism for an series title and episode, and display their name.
More about grep in the grep manpage.

6
  • Thank You Pizza. Much appreciated.
    – Babek
    Mar 15, 2021 at 7:03
  • However, I think "2>&1 >/dev/null" is redirecting the errors to the terminal and the output to the null!! At least that what I understood when I did a bit of research. What puzzles me is WHY? rawout=$(HandBrakeCLI -i /dev/dvd -t 0 2>&1 >/dev/null) ; count=$(echo $rawout | grep -Eao "\\+ title [0-9]+:" | wc -l); This script is trying to enumerate the number of episodes in a disc. FYI
    – Babek
    Mar 15, 2021 at 7:13
  • @Babek Yes, regarding /dev/null that's exactly what I wrote. As I also mentioned, it is a common way of silencing a program so it won't interfere any built in outputs or confuse the user when an OS command error occurs, while the author usually handles it themselves within the script.
    – Pizza
    Mar 15, 2021 at 8:27
  • 1
    If my answer helped you, feel free to mark it as accepted (The V button) so it won't appear as "Unanswered", and you are more than welcome to upvote it if it was clear enough to understand.
    – Pizza
    Mar 15, 2021 at 8:29
  • Thanks. How do I mark it as accepted??? sorry no idea
    – Babek
    Mar 17, 2021 at 11:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .