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;








2
















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.










share|improve this 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 by SIGKILL. Then the next instance will fail with EADDRINUSE when trying to bind to it.

    – mosvy
    Aug 11 at 20:58

















2
















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.










share|improve this 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 by SIGKILL. Then the next instance will fail with EADDRINUSE when trying to bind to it.

    – mosvy
    Aug 11 at 20:58













2












2








2









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.










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















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










1 Answer
1






active

oldest

votes


















4















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





share|improve this answer






















  • 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







  • 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 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



















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4















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





share|improve this answer






















  • 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







  • 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 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















4















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





share|improve this answer






















  • 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







  • 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 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













4














4










4









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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 12 at 7:16

























answered Aug 11 at 19:55









KusalanandaKusalananda

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 then trap 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 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












  • 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







  • 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 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







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



Popular posts from this blog

Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림