try/finally with bash shell [duplicate]Getting “ensure” / “finally” functionality in a shell command (not script)?How do I set permissions recursively on a dir (with ACL enabled)?How can avoid these spurious characters in my bash prompt?Editing lines in a file using a bash script?Parse XML file to copy the content into another file at a particular spot in the file?How to make bash built-in “read” ignore commented or empty lines?Getting “ensure” / “finally” functionality in a shell command (not script)?How to create a loop with five (5) input variables?sed - if condition met, use next pattern
Handling Disruptive Student on the Autistic Spectrum
Efficiently pathfinding many flocking enemies around obstacles
How to prevent clipped screen edges on my TV, HDMI-connected?
Is "imperatives have invisible subjects" a universal?
Is “I am getting married with my sister” ambiguous?
French abbreviation for comparing two items ("vs")
Architectural feasibility of a tiered circular stone keep
Is there any practical application for performing a double Fourier transform? ...or an inverse Fourier transform on a time-domain input?
Are there any elected officials in the U.S. who are not legislators, judges, or constitutional officers?
What is the difference between Major and Minor Bug?
Does travel insurance for short flight delays exist?
Why did this happen to Thanos's ships at the end of "Avengers: Endgame"?
Why in most German places is the church the tallest building?
Slitherlink Fillomino hybrid
Would the Republic of Ireland and Northern Ireland be interested in joining together?
Why do banks “park” their money at the European Central Bank?
Can roasted coffee as soil burn soft leaves?
Can more than one wizard copy a spell from a spellbook?
Round towards zero
Are the players on the same team as the DM?
Irish Snap: Variant Rules
How should I face my manager if I make a mistake because a senior coworker explained something incorrectly to me?
Is there any method of inflicting the incapacitated condition and no other condition?
Why did they avoid parodying Martian Manhunter?
try/finally with bash shell [duplicate]
Getting “ensure” / “finally” functionality in a shell command (not script)?How do I set permissions recursively on a dir (with ACL enabled)?How can avoid these spurious characters in my bash prompt?Editing lines in a file using a bash script?Parse XML file to copy the content into another file at a particular spot in the file?How to make bash built-in “read” ignore commented or empty lines?Getting “ensure” / “finally” functionality in a shell command (not script)?How to create a loop with five (5) input variables?sed - if condition met, use next pattern
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
This question already has an answer here:
Getting “ensure” / “finally” functionality in a shell command (not script)?
5 answers
I have these three lines:
export bunion_uds_file="$bunion_socks/$(uuidgen).sock";
"$cmd" "$@" | bunion
rm -f "$bunion_uds_file"
I need to make sure the last line always executes..I could do this:
export bunion_uds_file="$bunion_socks/$(uuidgen).sock";
(
set +e
"$cmd" "$@" | bunion
rm -f "$bunion_uds_file"
)
or maybe like this:
export bunion_uds_file="$bunion_socks/$(uuidgen).sock";
"$cmd" "$@" | bunion && rm -f "$bunion_uds_file" || rm -f "$bunion_uds_file"
I assume creating the subshell and using set +e is slightly less performant etc.
bash shell exit-status
marked as duplicate by muru, vonbrand, roaima, Philippos, DarkHeart Aug 14 at 5:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Getting “ensure” / “finally” functionality in a shell command (not script)?
5 answers
I have these three lines:
export bunion_uds_file="$bunion_socks/$(uuidgen).sock";
"$cmd" "$@" | bunion
rm -f "$bunion_uds_file"
I need to make sure the last line always executes..I could do this:
export bunion_uds_file="$bunion_socks/$(uuidgen).sock";
(
set +e
"$cmd" "$@" | bunion
rm -f "$bunion_uds_file"
)
or maybe like this:
export bunion_uds_file="$bunion_socks/$(uuidgen).sock";
"$cmd" "$@" | bunion && rm -f "$bunion_uds_file" || rm -f "$bunion_uds_file"
I assume creating the subshell and using set +e is slightly less performant etc.
bash shell exit-status
marked as duplicate by muru, vonbrand, roaima, Philippos, DarkHeart Aug 14 at 5:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
The common practice is to remove a unix domain socket before binding to it. No try/finally, traps, etc could guard against your script just crashing or being killed bySIGKILL. Then the next instance will fail withEADDRINUSEwhen trying to bind to it.
– mosvy
Aug 11 at 20:58
add a comment |
This question already has an answer here:
Getting “ensure” / “finally” functionality in a shell command (not script)?
5 answers
I have these three lines:
export bunion_uds_file="$bunion_socks/$(uuidgen).sock";
"$cmd" "$@" | bunion
rm -f "$bunion_uds_file"
I need to make sure the last line always executes..I could do this:
export bunion_uds_file="$bunion_socks/$(uuidgen).sock";
(
set +e
"$cmd" "$@" | bunion
rm -f "$bunion_uds_file"
)
or maybe like this:
export bunion_uds_file="$bunion_socks/$(uuidgen).sock";
"$cmd" "$@" | bunion && rm -f "$bunion_uds_file" || rm -f "$bunion_uds_file"
I assume creating the subshell and using set +e is slightly less performant etc.
bash shell exit-status
This question already has an answer here:
Getting “ensure” / “finally” functionality in a shell command (not script)?
5 answers
I have these three lines:
export bunion_uds_file="$bunion_socks/$(uuidgen).sock";
"$cmd" "$@" | bunion
rm -f "$bunion_uds_file"
I need to make sure the last line always executes..I could do this:
export bunion_uds_file="$bunion_socks/$(uuidgen).sock";
(
set +e
"$cmd" "$@" | bunion
rm -f "$bunion_uds_file"
)
or maybe like this:
export bunion_uds_file="$bunion_socks/$(uuidgen).sock";
"$cmd" "$@" | bunion && rm -f "$bunion_uds_file" || rm -f "$bunion_uds_file"
I assume creating the subshell and using set +e is slightly less performant etc.
This question already has an answer here:
Getting “ensure” / “finally” functionality in a shell command (not script)?
5 answers
bash shell exit-status
bash shell exit-status
edited Aug 11 at 20:07
Kusalananda♦
161k18 gold badges318 silver badges504 bronze badges
161k18 gold badges318 silver badges504 bronze badges
asked Aug 11 at 19:37
Alexander MillsAlexander Mills
2,4882 gold badges22 silver badges70 bronze badges
2,4882 gold badges22 silver badges70 bronze badges
marked as duplicate by muru, vonbrand, roaima, Philippos, DarkHeart Aug 14 at 5:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by muru, vonbrand, roaima, Philippos, DarkHeart Aug 14 at 5:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by muru, vonbrand, roaima, Philippos, DarkHeart Aug 14 at 5:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
The common practice is to remove a unix domain socket before binding to it. No try/finally, traps, etc could guard against your script just crashing or being killed bySIGKILL. Then the next instance will fail withEADDRINUSEwhen trying to bind to it.
– mosvy
Aug 11 at 20:58
add a comment |
The common practice is to remove a unix domain socket before binding to it. No try/finally, traps, etc could guard against your script just crashing or being killed bySIGKILL. Then the next instance will fail withEADDRINUSEwhen trying to bind to it.
– mosvy
Aug 11 at 20:58
The common practice is to remove a unix domain socket before binding to it. No try/finally, traps, etc could guard against your script just crashing or being killed by
SIGKILL. Then the next instance will fail with EADDRINUSE when trying to bind to it.– mosvy
Aug 11 at 20:58
The common practice is to remove a unix domain socket before binding to it. No try/finally, traps, etc could guard against your script just crashing or being killed by
SIGKILL. Then the next instance will fail with EADDRINUSE when trying to bind to it.– mosvy
Aug 11 at 20:58
add a comment |
1 Answer
1
active
oldest
votes
You could set a trap:
#!/bin/bash
export bunion_uds_file="$bunion_socks/$(uuidgen).sock"
trap 'rm -f "$bunion_uds_file"' EXIT
"$cmd" "$@" | bunion
This would make the rm -f command run whenever the shell session terminates, except for when terminating by the KILL signal.
As mosvy points out in comments, if this is a socket that needs to be cleaned up before use, it would be easier to remove it before recreating and using it:
#!/bin/bash
export bunion_uds_file="$bunion_socks/$(uuidgen).sock"
rm -f "$bunion_uds_file" || exit 1
"$cmd" "$@" | bunion
1
I like to put the trap code in a function:cleanup() rm -f $thefile;and thentrap cleanup EXIT-- I think it's cleaner, and it solves any quoting problems.
– glenn jackman
Aug 11 at 20:44
1
@glennjackman You've just introduced a quoting problem in that comment's code, but I get what you're saying. For just deleting a single file in a three line script, I personally think not using a function is ok.
– Kusalananda♦
Aug 11 at 20:57
Yes, bash (but not dash or zsh) will also call theEXITtrap when being killed by aSIGINTorSIGTERM, but I don't know how wise it is to rely on that.
– mosvy
Aug 11 at 21:03
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could set a trap:
#!/bin/bash
export bunion_uds_file="$bunion_socks/$(uuidgen).sock"
trap 'rm -f "$bunion_uds_file"' EXIT
"$cmd" "$@" | bunion
This would make the rm -f command run whenever the shell session terminates, except for when terminating by the KILL signal.
As mosvy points out in comments, if this is a socket that needs to be cleaned up before use, it would be easier to remove it before recreating and using it:
#!/bin/bash
export bunion_uds_file="$bunion_socks/$(uuidgen).sock"
rm -f "$bunion_uds_file" || exit 1
"$cmd" "$@" | bunion
1
I like to put the trap code in a function:cleanup() rm -f $thefile;and thentrap cleanup EXIT-- I think it's cleaner, and it solves any quoting problems.
– glenn jackman
Aug 11 at 20:44
1
@glennjackman You've just introduced a quoting problem in that comment's code, but I get what you're saying. For just deleting a single file in a three line script, I personally think not using a function is ok.
– Kusalananda♦
Aug 11 at 20:57
Yes, bash (but not dash or zsh) will also call theEXITtrap when being killed by aSIGINTorSIGTERM, but I don't know how wise it is to rely on that.
– mosvy
Aug 11 at 21:03
add a comment |
You could set a trap:
#!/bin/bash
export bunion_uds_file="$bunion_socks/$(uuidgen).sock"
trap 'rm -f "$bunion_uds_file"' EXIT
"$cmd" "$@" | bunion
This would make the rm -f command run whenever the shell session terminates, except for when terminating by the KILL signal.
As mosvy points out in comments, if this is a socket that needs to be cleaned up before use, it would be easier to remove it before recreating and using it:
#!/bin/bash
export bunion_uds_file="$bunion_socks/$(uuidgen).sock"
rm -f "$bunion_uds_file" || exit 1
"$cmd" "$@" | bunion
1
I like to put the trap code in a function:cleanup() rm -f $thefile;and thentrap cleanup EXIT-- I think it's cleaner, and it solves any quoting problems.
– glenn jackman
Aug 11 at 20:44
1
@glennjackman You've just introduced a quoting problem in that comment's code, but I get what you're saying. For just deleting a single file in a three line script, I personally think not using a function is ok.
– Kusalananda♦
Aug 11 at 20:57
Yes, bash (but not dash or zsh) will also call theEXITtrap when being killed by aSIGINTorSIGTERM, but I don't know how wise it is to rely on that.
– mosvy
Aug 11 at 21:03
add a comment |
You could set a trap:
#!/bin/bash
export bunion_uds_file="$bunion_socks/$(uuidgen).sock"
trap 'rm -f "$bunion_uds_file"' EXIT
"$cmd" "$@" | bunion
This would make the rm -f command run whenever the shell session terminates, except for when terminating by the KILL signal.
As mosvy points out in comments, if this is a socket that needs to be cleaned up before use, it would be easier to remove it before recreating and using it:
#!/bin/bash
export bunion_uds_file="$bunion_socks/$(uuidgen).sock"
rm -f "$bunion_uds_file" || exit 1
"$cmd" "$@" | bunion
You could set a trap:
#!/bin/bash
export bunion_uds_file="$bunion_socks/$(uuidgen).sock"
trap 'rm -f "$bunion_uds_file"' EXIT
"$cmd" "$@" | bunion
This would make the rm -f command run whenever the shell session terminates, except for when terminating by the KILL signal.
As mosvy points out in comments, if this is a socket that needs to be cleaned up before use, it would be easier to remove it before recreating and using it:
#!/bin/bash
export bunion_uds_file="$bunion_socks/$(uuidgen).sock"
rm -f "$bunion_uds_file" || exit 1
"$cmd" "$@" | bunion
edited Aug 12 at 7:16
answered Aug 11 at 19:55
Kusalananda♦Kusalananda
161k18 gold badges318 silver badges504 bronze badges
161k18 gold badges318 silver badges504 bronze badges
1
I like to put the trap code in a function:cleanup() rm -f $thefile;and thentrap cleanup EXIT-- I think it's cleaner, and it solves any quoting problems.
– glenn jackman
Aug 11 at 20:44
1
@glennjackman You've just introduced a quoting problem in that comment's code, but I get what you're saying. For just deleting a single file in a three line script, I personally think not using a function is ok.
– Kusalananda♦
Aug 11 at 20:57
Yes, bash (but not dash or zsh) will also call theEXITtrap when being killed by aSIGINTorSIGTERM, but I don't know how wise it is to rely on that.
– mosvy
Aug 11 at 21:03
add a comment |
1
I like to put the trap code in a function:cleanup() rm -f $thefile;and thentrap cleanup EXIT-- I think it's cleaner, and it solves any quoting problems.
– glenn jackman
Aug 11 at 20:44
1
@glennjackman You've just introduced a quoting problem in that comment's code, but I get what you're saying. For just deleting a single file in a three line script, I personally think not using a function is ok.
– Kusalananda♦
Aug 11 at 20:57
Yes, bash (but not dash or zsh) will also call theEXITtrap when being killed by aSIGINTorSIGTERM, but I don't know how wise it is to rely on that.
– mosvy
Aug 11 at 21:03
1
1
I like to put the trap code in a function:
cleanup() rm -f $thefile; and then trap cleanup EXIT -- I think it's cleaner, and it solves any quoting problems.– glenn jackman
Aug 11 at 20:44
I like to put the trap code in a function:
cleanup() rm -f $thefile; and then trap cleanup EXIT -- I think it's cleaner, and it solves any quoting problems.– glenn jackman
Aug 11 at 20:44
1
1
@glennjackman You've just introduced a quoting problem in that comment's code, but I get what you're saying. For just deleting a single file in a three line script, I personally think not using a function is ok.
– Kusalananda♦
Aug 11 at 20:57
@glennjackman You've just introduced a quoting problem in that comment's code, but I get what you're saying. For just deleting a single file in a three line script, I personally think not using a function is ok.
– Kusalananda♦
Aug 11 at 20:57
Yes, bash (but not dash or zsh) will also call the
EXIT trap when being killed by a SIGINT or SIGTERM, but I don't know how wise it is to rely on that.– mosvy
Aug 11 at 21:03
Yes, bash (but not dash or zsh) will also call the
EXIT trap when being killed by a SIGINT or SIGTERM, but I don't know how wise it is to rely on that.– mosvy
Aug 11 at 21:03
add a comment |
The common practice is to remove a unix domain socket before binding to it. No try/finally, traps, etc could guard against your script just crashing or being killed by
SIGKILL. Then the next instance will fail withEADDRINUSEwhen trying to bind to it.– mosvy
Aug 11 at 20:58