5
\$\begingroup\$

I created my first shell script. It's an audio logger that runs on the hour for an hour and restarts. Although if it is started within the hour it will only run until the end of the hour.

I've added it to Startup Applications with the command:

gnome-terminal -e /home/admin/Desktop/audiolog.sh

However when it starts up the text in terminal flickers and is resolved by maximizing the window but works fine. Any suggestions for improvements on my script or anything that is wrong? Thanks

#!/bin/sh

DAY=$(date +%d)
MO=$(date +%m)
YR=$(date +%y)
SE=$(date +%-S)
H=$(date +%H)
M=$(date +%M)

NOW=$(date)

DURATION=$(((60 - M) * 60-SE))

PART=$(find ./Documents/ -type f -name "LoggerAudio 
${DAY}-${MO}-${YR}\ ${H}*" | wc -l)

reset

echo "Recording for $DAY $MO $YR - $H 00"
echo "Started at $NOW"
echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- - - - - - - - - -"

if [ "$PART" -eq 0 ]
then
arecord -f cd -c 1 -d$DURATION -t raw -v -v | lame -b 128 -q2 -mm 
-r - ./Documents/"LoggerAudio "$DAY-$MO-$YR\ $H"00".mp3
else
arecord -f cd -c 1 -d$DURATION -t raw -v -v | lame -b 128 -q2 -mm 
-r - ./Documents/"LoggerAudio "$DAY-$MO-$YR\ $H"00 - Part - "$PART.mp3
fi

sleep 1s

./Desktop/audiolog.sh;
\$\endgroup\$
1
\$\begingroup\$

This looks quite fine, except this part:

if [ "$PART" -eq 0 ]
then
arecord -f cd -c 1 -d$DURATION -t raw -v -v | lame -b 128 -q2 -mm 
-r - ./Documents/"LoggerAudio "$DAY-$MO-$YR\ $H"00".mp3
else
arecord -f cd -c 1 -d$DURATION -t raw -v -v | lame -b 128 -q2 -mm 
-r - ./Documents/"LoggerAudio "$DAY-$MO-$YR\ $H"00 - Part - "$PART.mp3
fi

One problem here is indentation: it's a lot easier to read when the branches of conditionals are indented.

A bigger problem is that a long and complicated pipeline is duplicated (looks copy-pasted), which is a bad practice, because it's prone to errors. Since only the filename is different, you can set that in the conditional branches, and then write the complicated pipeline only once.

if [ "$PART" -eq 0 ]
then
    out="LoggerAudio $DAY-$MO-$YR ${H}00.mp3"
else
    out="LoggerAudio $DAY-$MO-$YR ${H}00 - Part - $PART.mp3"
fi
arecord -f cd -c 1 -d$DURATION -t raw -v -v | lame -b 128 -q2 -mm -r - ./Documents/"$out"

Notice that I also slightly simplified the writing style of the filename.

\$\endgroup\$

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.