尝试/最后使用 bash shell

Ale*_*lls 3 shell bash exit-status

我有这三行:

  export bunion_uds_file="$bunion_socks/$(uuidgen).sock";
  "$cmd" "$@" | bunion
  rm -f "$bunion_uds_file"
Run Code Online (Sandbox Code Playgroud)

我需要确保最后一行总是执行..我可以这样做:

  export bunion_uds_file="$bunion_socks/$(uuidgen).sock";
  (
    set +e
    "$cmd" "$@" | bunion
    rm -f "$bunion_uds_file"
  )
Run Code Online (Sandbox Code Playgroud)

或者像这样:

  export bunion_uds_file="$bunion_socks/$(uuidgen).sock";
  "$cmd" "$@" | bunion && rm -f "$bunion_uds_file" || rm -f "$bunion_uds_file"
Run Code Online (Sandbox Code Playgroud)

我假设创建子外壳并使用 set +e 的性能稍低等。

Kus*_*nda 5

你可以设置一个陷阱:

#!/bin/bash

export bunion_uds_file="$bunion_socks/$(uuidgen).sock"
trap 'rm -f "$bunion_uds_file"' EXIT

"$cmd" "$@" | bunion
Run Code Online (Sandbox Code Playgroud)

这将使rm -f命令在 shell 会话终止时运行,除非由KILL信号终止。

正如 mosvy 在评论中指出的那样,如果这是一个需要在使用前清理的套接字,那么在重新创建和使用它之前将其删除会更容易:

#!/bin/bash

export bunion_uds_file="$bunion_socks/$(uuidgen).sock"
rm -f "$bunion_uds_file" || exit 1

"$cmd" "$@" | bunion
Run Code Online (Sandbox Code Playgroud)