Questions tagged [bash-trap]

Use this tag for questions about the `trap` Bash built-in.

171 questions
3
votes
1 answer

bash trap EXIT $LINENO always 1

test.sh #!/bin/bash set -e trap 'echo $LINENO' EXIT /bin/false run $ ./test.sh 1 How may I get the actual line number of "/bin/false" instead of always '1'?
Darren Ng
  • 373
  • 5
  • 12
3
votes
1 answer

Trap signals both in script and subshells

I know there's a lot of questions similar to mine, but I didn't found any about trapping signals sent when a subshell is taking place on the terminal. Let me explain: #!/bin/sh trap 'echo "exiting.."; exit 0;' INT var1=$(echo "ab\nab" |…
Teodoro
  • 1,194
  • 8
  • 22
3
votes
2 answers

SIGPIPE due to file descriptors and process substitution

I am trying to save some logs from bash functions which execute tools (some of them run in subshells). In addition I would like to print all errors to the terminal. My code leads to a sigpipe and exit code 141 upon hitting ctr-c plus a strange log…
pyr0
  • 377
  • 1
  • 14
3
votes
2 answers

Trap with external call in command substitution breaks the parent Bash shell

I have a text-based user interface script that allows me to browse directories and select a file. The graphics are output to stderr, the chosen file's path is sent to stdout. This allows to get the chosen file this way: file="$(./script)" This is…
Informancien
  • 255
  • 3
  • 13
3
votes
1 answer

SIGCHLD not delivered when a process exits

I need to detect when one of my background processes exits. Hence, I installed a trap. run_gui and run_ai1 are simple exec functions. run_gui & gui_pid=$! run_ai1 & ai1_pid=$! trap 'echo foo' SIGCHLD while true; do echo "Started the loop" …
marmistrz
  • 5,974
  • 10
  • 42
  • 94
3
votes
1 answer

Clean exit from bashscript which uses read and trap

Run the below script Press Ctrl+C Observe the current terminal behaviour. Press enter few times and try to execute some commands. #!/bin/bash LOCK_FILE=/tmp/lockfile clean_up(){ # Perform program exit housekeeping echo -e "Signal Trapped,…
Dipankar
  • 141
  • 2
  • 9
3
votes
3 answers

Why does my bash script take so long to respond to kill when it runs in the background?

(Question revised, now that I understand more about what's actually happening): I have a script that runs in the background, periodically doing some work and then sleeping for 30 seconds: echo "background script PID: $$" trap 'echo "Exiting..."' INT…
RashaMatt
  • 247
  • 1
  • 3
  • 11
3
votes
3 answers

Why trap didn't work in a pipeline

In this script We have a script trap.sh #!/bin/bash trap "echo trapped" EXIT exit 0 and test.sh. If test.sh is like #!/bin/bash . trap.sh or #!/bin/bash ./trap.sh | : trap works But if test.sh is like #!/bin/bash . trap.sh | : the trap didn't…
dspjm
  • 5,473
  • 6
  • 41
  • 62
3
votes
1 answer

bash shell: Can a control-c cause shell to write an empty file?

I have a bash shell script. It writes out to a text file. Most of the it works find if I stop the script with a control-c at the command level. Sometimes the file that's been written to such as echo "hello world" >myfile.txt will end up being…
Edward
  • 9,430
  • 19
  • 48
  • 71
3
votes
3 answers

How to keep restarting a Windows cmd prompt

Cygwin's bash is often preferable to Windows' cmd command shell, so we use it to set up our environments before spawning a Windows shell. However, halting execution of a running process in this spawned shell with Ctrl-C kills boots the user back to…
tcdaniel
  • 203
  • 1
  • 2
  • 11
3
votes
1 answer

bash send errors by email

I wrote some bash script that does some backup job. I run the script with errexit and pipefail so I won't miss any error. The thing I want now is the script sending me an email in case an error occures. I got this working. But I want the scripts…
Mike Dynamite
  • 65
  • 2
  • 8
3
votes
2 answers

how to trap errors inside the if statement

Running the following code: #!/bin/bash set -o pipefail set -o errtrace set -o nounset set -o errexit function err_handler () { local error_code="$?" echo "TRAP!" echo "error code: $error_code" exit } trap err_handler ERR echo…
Luca Borrione
  • 16,324
  • 8
  • 52
  • 66
2
votes
1 answer

Perl trapping Ctrl-C (sigint) in bash

I'm reading How do we capture CTRL ^ C - Perl Monks, but I cannot seem to get the right info to help with my problem. The thing is - I have an infinite loop, and 'multiline' printout to terminal (I'm aware I'll be told to use ncurses instead - but…
sdaau
  • 36,975
  • 46
  • 198
  • 278
2
votes
0 answers

redirecting stderr to err function

I want to trap errors with my err function, which writes to the stderr. But in this example I am getting two error lines. How can I remove the first error msg not generated by my err function? I want to prevent something like _get_error_msg 2>…
mikee
  • 29
  • 3
2
votes
1 answer

Trap Command not detecting terminal resize (SIGWINCH)

Im trying to detect when my menu is resized using trap "echo resized" SIGWINCH but it doesnt seem to be detecting it. Im currently using ubuntu 20.04 and i am using a bash script to do this. The trap command is at the top of my script. Why is it not…
Quantum
  • 59
  • 5