啟動 bash 檔案在第一個命令後退出並顯示「終止」訊息,同時它在終端內運作良好

啟動 bash 檔案在第一個命令後退出並顯示「終止」訊息,同時它在終端內運作良好

我在 Ubuntu 22.04 平台上。我在 c 中製作了一個簡單的按鈕 GUI t2s,並將其放置在~/.local/bin已新增至PATH環境變數的路徑中。當我按下按鈕時,它正在將麥克風中的語音錄製到臨時檔案中。當我釋放按鈕時 GUI 退出。我運行以下行,該行在終端機中運行良好:

 t2s && notify-send -u normal  -t 10000 "$( whispercpp -m /home/****/Desktop/2022-10/whisper.cpp/models/ggml-small.bin -nt -l tr -f /dev/shm/mic.wav )"

語音被發送whispercpp到文字引擎並轉錄為語音。結果顯示在螢幕上的通知中。

但是當我將該行放入文件中並啟動它時,例如:

 #!/bin/bash

 t2s && notify-send -u normal  -t 10000 "$( whispercpp -m /home/****/Desktop/2022-10/whisper.cpp/models/ggml-small.bin -nt -l tr -f /dev/shm/mic.wav )"

 exit 0

它只執行GUI按鈕,當釋放按鈕後GUI退出時,它不執行

 notify-send -u normal  -t 10000 "$( whispercpp -m /home/****/Desktop/2022-10/whisper.cpp/models/ggml-small.bin -nt -l tr -f /dev/shm/mic.wav )"

部分

我究竟做錯了什麼?

編輯:

我也這樣嘗試過:

#!/bin/bash

t2s
TEXT=$( whispercpp -m /home/****/Desktop/2022-10/whisper.cpp/models/ggml-small.bin -nt -l tr -f /dev/shm/mic.wav )"
notify-send -u normal  -t 10000 $TEXT

什麼都沒有改變。

編輯:

我注意到它與外殼內部有關。

我仍然不知道如何克服它。

答案1

閱讀以下連結後:

我了解到,終止ffmpeg執行的行會導致 bash shell 在GUIGUI button之後終止。t2s我透過在區塊內進行通訊SIGINTSIGTERM訊號來解決這個問題trap,然後將其餘命令放入其中t2s

#!/bin/bash

trap_with_arg() { # from https://stackoverflow.com/a/2183063/804678
  local func="$1"; shift
  for sig in "$@"; do
    trap "$func $sig" "$sig"
  done
}

stop() {
  trap - SIGINT EXIT
  printf '\n%s\n' "received $1, killing child processes"
  notify-send -u normal  -t 10000 "$(whispercpp -m /home/**/Desktop/2022-10/whisper.cpp/models/ggml-small.bin -nt -l tr -f /dev/shm/mic.wav )"
  kill -s SIGINT 0
}

trap_with_arg 'stop' EXIT SIGINT SIGTERM SIGHUP

t2s

exit 0

相關內容