0

I'm trying to understand the codes in this project: https://github.com/benoitfragit/google2ubuntu/blob/master/record.sh

#!/bin/sh
# configuration file
CONFIGURATION="$HOME/.config/google2ubuntu/google2ubuntu.conf"

# default recording time
recording=5
if [ -f "$CONFIGURATION" ]; 
then
{
    # load the configuration
    . "$CONFIGURATION"

    # small security test
    if [ "$recording" = "" ];
    then
        recording=5
    fi
}
fi

# get the pid 
PID=$1
# kill the previous instance of rec to cascade commands
killall rec 2>/dev/null

# record during fixed seconds
( rec -r 16000 -d /tmp/voix_$PID.flac ) & pid=$!
( sleep "$recording"s && kill -HUP $pid ) 2>/dev/null & watcher=$!
wait $pid 2>/dev/null && pkill -HUP -P $watcher
exit 0;

I didn't understand this: . "$CONFIGURATION" # load the configuration What kind of loading, running a .conf file? But it isn't a program?

and also I didn't understand the last five lines.

( rec -r 16000 -d /tmp/voix_$PID.flac ) & pid=$! 

OK record an audio file, save it to tmp/ directory & What? :)

( sleep "$recording"s && kill -HUP $pid ) 2>/dev/null & watcher=$!
wait $pid 2>/dev/null && pkill -HUP -P $watcher

I did not understand anything from these two lines.

exit 0;

I think it means "return 0" and it is like "return True" in programming languages. Correct?

mertyildiran
  • 6,477
  • 5
  • 32
  • 55

1 Answers1

1

The . command (also known as source in the C shell, and source has been transplanted into Bash too), reads the file named in its argument in the context of the current shell. That means that any variables set in the file are set in the current shell.

Conceptually, a login shell does:

[ -f /etc/profile ] && . /etc/profile
[ -f $HOME/.profile ] && . $HOME/.profile

etc. (the details differ depending on your shell).

This mechanism is used in the script you're showing. Note that the file being included is found via $PATH (if there are no slashes in the name), but the file does not need to be executable (readable is sufficient).


The line:

( rec -r 16000 -d /tmp/voix_$PID.flac ) & pid=$!

runs the rec command in a sub-shell and captures the PID of the sub-shell in the variable $pid.

( sleep "$recording"s && kill -HUP $pid ) 2>/dev/null & watcher=$!
wait $pid 2>/dev/null && pkill -HUP -P $watcher

The first line runs a sub-shell which sleeps for a time specified by $recording and then runs kill -HUP $pid to hangup the rec command previously run in the background. The standard error is sent to /dev/null, and the PID is captured in the variable $watcher.

The second line waits for $pid to die, redirecting errors to /dev/null, and when that returns successfully, does a pkill -HUP -P to the process identified by $watcher.

So, the first line sets up a watch-dog timer to make sure rec doesn't last too long. The second line waits for the rec process to die, and kills the watch-dog when it does.

exit 0;

The semicolon is superfluous, but exit status 0 indicates success and any non-zero value (in the range 1..255) indicates failure of some sort. See also Exit codes bigger than 255 — possible?

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278