Nieuws:

Welkom, Gast. Alsjeblieft inloggen of registreren.
Heb je de activerings-mail niet ontvangen?

Auteur Topic: script in bash: Na sluiten met ctrl-c nog een opdracht uitvoeren.  (gelezen 806 keer)

Offline bart85

  • Lid
In het volgende script.
Het volgende script draait op de achtergrond. Het kan alleen  met Ctrl-C worden afgesloten. Ik wil graag dat dan de laatste regel nog word uitgevoerd voor het wissen van het clipboard.
#!/bin/bash

last=$(xclip -selection clipboard -o 2>/dev/null)

while true
do
new=$(xclip -selection clipboard -o 2>/dev/null)
if [[ ! -z $new ]]
then
[[ $last != $new ]] && yt-dlp -x -f ba --no-playlist $new
last=$new
fi
sleep 10
done
echo "" | xclip

Of is het mogelijk om de while loop af te sluiten, zodat de laatste regel nog word uitgevoerd met een keypress?
« Laatst bewerkt op: 2022/12/02, 20:34:34 door bart85 »
Relying on complex tools to manage and build your system is going to hurt the end-users. [...] "If you try to hide the complexity of the system, you'll end up with a more complex system". Layers of abstraction that serve to hide internals are never a good thing. Instead, the internals should be designed in a way such that they NEED no hiding.

— Aaron Griffin

Offline Bloom

  • Lid
Re: script in bash: Na sluiten met ctrl-c nog een opdracht uitvoeren.
« Reactie #1 Gepost op: 2022/12/02, 22:20:19 »
Dat is niet mogelijk in datzelfde script met ctrl-C.
Als je die functionaliteit wil, gebruik dan in plaats van while true een while met test op de aanwezigheid van een stopbestand.
while [[ ! -f $0.stop ]]; do  ...doneAls je wil dat het script stopt, maak je gewoon met touch een bestand aan met de naam van het script.stop en dan zal de while-lus stoppen en de opdracht(en) daarna uitvoeren.

Offline bart85

  • Lid
Re: script in bash: Na sluiten met ctrl-c nog een opdracht uitvoeren.
« Reactie #2 Gepost op: 2022/12/03, 11:22:56 »
Script wat aangepast. In plaats van op het einde wist het script het klembord bij starten script.
#!/bin/bash
xclip -sel clip < /dev/null
format=${format:-"ba"}
options=${options:-"--extract-audio --no-playlist"}
log=${log:-"true"}
while true
do
next=$(xclip -selection clipboard -o 2>/dev/null)
if [[ $prev != $next ]]
then
yt-dlp $options -f $format  $next
[[ $log == "true" ]] && echo $next >> playlist
prev=$next
echo "waiting for next link"
fi
sleep 10
done
[/done]
« Laatst bewerkt op: 2022/12/03, 14:06:41 door bart85 »
Relying on complex tools to manage and build your system is going to hurt the end-users. [...] "If you try to hide the complexity of the system, you'll end up with a more complex system". Layers of abstraction that serve to hide internals are never a good thing. Instead, the internals should be designed in a way such that they NEED no hiding.

— Aaron Griffin

Offline vanadium

  • Lid
Re: script in bash: Na sluiten met ctrl-c nog een opdracht uitvoeren.
« Reactie #3 Gepost op: 2022/12/03, 17:46:09 »
Het bash "trap" commando kan hierbij helpen.