Skip to content
Advertisement

Bash-script to toggle espeak by keyboard shortcut

To read a selected text in GUI out loud I use the command xsel | espeak-ng -v mb-us3 -p3 -s135. To stop it I use killall -s STOP espeak-ng. To start it again killall -s CONT espeak-ng.

All 3 commands work, but the noob I am won’t let me archive correct results. The problem is in the if- and case-statement I think. I assigned them to a keyboard shortcut.

I want to manage this shellscript to toggle between read aloud and stopping the process of reading aloud if espeak-ng is already running. If it’s not already running the bash should start it.

0 #!/bin/bash
1 # Name: espeak-en.sh
2 # Author: Karsten Zarth
3 # Created: 2020-08-23
4 # Description: Textselection in GUI is read aloud with english
5 #              male voice with espeak-ng and mbrula
6 #              if already reading: stop
7 #              if stopped: continue
8 # Modified: 2020-08-23
9
10 status=`ps -ef| grep espeak-ng| grep -v grep| awk '{print $5}"`
11
12 if [[ $(pgrep -x espeak-ng) ]]
13         then
14                 case $status in
15                         Tl) killall -s CONT espeak-ng;;
16                         Sl) killall -s STOP espeak-ng;;
17                 esac
18         else
19                 xsel | espeak-ng -v mb-us3 -p3 -s135
20 fi

I don’t even know how to test the output of: if [[ $(pgrep -x espeak-ng) ]]

Can somebody help me to figure out what’s wrong and give me some hints? Many thanks in advance.

Advertisement

Answer

after struggling a bit by myself I found out that there is a difference between ps -ef and ps ax in the order of printing out some values…

Also I have to admit that the end of line 10 looks awful. It has to be ...print $5}'` . Because of the difference between ps -ef and ps ax of course it has to be ...print $3}'` .

By the way. The dependencies are xsel and espeak-ng and at least one mbrola-voice.

Now it works. Here is the full code:

0 #!/bin/bash
1 # Name: espeak-en.sh
2 # Author: Karsten Zarth
3 # Created: 2020-08-23
4 # Description: Textselection in GUI is read aloud with english
5 #              male voice with espeak-ng and mbrula
6 #              if already reading: stop
7 #              if stopped: continue
8 # Modified: 2020-08-24
9
10 status=`ps ax| grep espeak-ng| grep -v grep| awk '{print $3}'`
11
12 if [[ $(pgrep -x espeak-ng) ]]
13         then
14                 case $status in
15                         Tl) killall -s CONT espeak-ng;;
16                         Sl) killall -s STOP espeak-ng;;
17                 esac
18         else
19                 xsel | espeak-ng -v mb-us3 -p3 -s135
20 fi
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement