bash 获取末尾有 exit 命令的文件

2024-03-03

cat >file1.sh <<'EOF_FILE1'
  echo 'before source'
  source 'file2.sh'
  echo 'after source'
  func1
EOF_FILE1

cat >file2.sh <<'EOF_FILE2'
  echo 'test script'
  func1() {
    echo 'func1 starts'
    exit
  }
  exit
EOF_FILE2

bash file1.sh

预期输出是:

before source
test script
after source
func1 starts

实际输出是:

before source
test script

由于以下原因,“后源”丢失exit命令。有没有办法解决这个问题,因为我无法删除exit从代码中?


鉴于best方法是编写旨在获取而不是执行时考虑到该用例的脚本,如果您出于某种原因无法这样做,您可能会考虑aliasing exit to return之前source命令,如下:

shopt -s expand_aliases  # enable alias expansion (off by default in noninteractive shells)
alias exit=return        # ...and alias 'exit' to 'return'

source 'file2.sh'        # source in your file which incorrectly uses 'exit' at top-level
unalias exit             # disable the alias...
echo 'after source'
func1

如果你想要exit为了在调用该函数时仍然生效,事情可以变得更复杂一些:

maybe_exit() {
  local last_retval=$?                 # preserve exit's behavior of defaulting to $?
  [[ $do_not_really_exit ]] && return  # abort if flag is set
  (( $# )) && exit "$@"                # if arguments are given, pass them through
  exit "$last_retval"                  # otherwise, use the $? we captured above
}

shopt -s expand_aliases  # enable alias expansion (off by default in noninteractive shells)
alias exit=maybe_exit    # ...and alias 'exit' to 'maybe_exit'

do_not_really_exit=1     # set a flag telling maybe_exit not to really exit
source 'file2.sh'        # source in your file which incorrectly uses 'exit' at top-level
unset do_not_really_exit # clear that flag...
unalias exit             # disable the alias...
echo 'after source'
func1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

bash 获取末尾有 exit 命令的文件 的相关文章

  • 如何使用 echo 写入非 ASCII 字符?

    如何写非ASCII http en wikipedia org wiki ASCII使用 echo 的字符 是否有转义序列 例如 012或类似的东西 我想使用以下方法将 ASCII 字符附加到文件中 echo gt gt file 如果您关
  • 通过 bash 从文件中检索电子邮件主题

    我有一个 shell 脚本 它将文件从服务器邮件文件夹下载到 NAS 设备 以便客户端拥有本地备份的副本 文件保存为11469448248 H15587P19346 smtp x14 eu 2 S文件 我已将扩展名更改为标准 eml 格式
  • 如何在 C# 中启动文件

    编辑 我觉得自己像个白痴 我有一种感觉 像下面的答案会起作用 但没有看到任何与下面的答案类似的谷歌结果 所以当我看到这段复杂的代码时 我想它一定是这样的 我搜索并找到了这个Windows 列出并启动与扩展关联的应用程序 https stac
  • 如何从 mysqldump 中删除表

    如何删除 mysqldump 中包含大量表的一个大表的输出 我有一个 6 GB 大的数据库转储 但其中 90 只是一个日志记录表 cache entries 我的备份中不再需要它 如何轻松删除转储中描述大型日志记录表的部分 我找到了这个 h
  • 用于编辑 /etc/sudoers 文件的正则表达式模式

    我想删除 etc sudoers 文件中的 uncommnet 轮组 那么我应该使用什么正则表达式模式 cat etc sudoers Allows members of the sys group to run networking so
  • shell中如何求数组的长度?

    shell中如何求数组的长度 例如 arr 1 2 3 4 5 我想得到它的长度 在本例中是 5 a 1 2 3 4 echo a 4
  • 检查 Bash 数组中是否存在元素[重复]

    这个问题在这里已经有答案了 我想知道是否有一种有效的方法来检查 Bash 数组中是否存在元素 我正在寻找类似于我可以在Python中做的事情 例如 arr a b c d if d in arr do your thing else do
  • adb shell:无法使用 ESCAPE 键

    I want to use vi when running adb shell Starting vi is easy However I found that the ESC key doesn t seem to get through
  • 在 BASH 脚本中使用字符串作为变量名

    我有以下内容 bin sh n fred bob f n echo f 我需要在替换后执行底线 echo n 有办法做到这一点吗 我刚刚得到 test sh line 8 f bad substitution 在我这边 您可以像这样使用数组
  • 如何退出bash中的所有调用脚本?

    假设我有以下脚本 a sh echo in a if test 1 ne 2 then echo oops exit 1 fi b sh echo in b a sh echo in b 2 运行 b sh 时 如果 a sh 退出 我希望
  • 为什么 shell=True 的 subprocess.Popen() 在 Linux 和 Windows 上的工作方式不同?

    使用时subprocess Popen args shell True 跑步 gcc version 仅作为示例 在 Windows 上我们得到 gt gt gt from subprocess import Popen gt gt gt
  • 在后台使用 HERE_DOC 方法运行脚本

    我有一个应该在后台运行的脚本 我必须在运行 bash 后立即回答一个问题 我该怎么做 nohup python script py lst lt
  • awk 返回两个变量

    现在这就是我正在做的事情 ret ls la awk print 3 9 usr echo ret awk print 1 fil echo ret awk print 2 问题是我没有运行ls我正在运行一个需要时间的命令 因此您可以理解其
  • exec()、shell_exec()、curl_exec() 的安全漏洞

    有时 我会使用 exec shell exec 和curl exec 以下是典型用途 假设其中有 PHP 变量 即第一个变量中的 html 用户有可能修改其内容 从安全漏洞的角度来看 我应该关注什么 escapeshellcmd 和 esc
  • “检测到堆栈粉碎”消息打印到哪个流?

    考虑以下非常基本的程序 它在此处以多种形式出现在其他问题上 include
  • 在 C# 中编写批处理脚本的好方法是什么?

    我想用 C 编写简单的脚本 我通常会使用 bat 或 4NT btm 文件 复制文件 解析文本 询问用户输入等等 相当简单 但在批处理文件中正确执行这些操作确实很困难 例如没有例外 我熟悉像 AxScript 这样的命令行 脚本 包装器 这
  • 用于获取特定用户 ID 和进程数的 Bash 脚本

    我需要 bash 脚本来计算特定用户或所有用户的进程 我们可以输入 0 1 或更多参数 例如 myScript sh root deamon 应该像这样执行 root 92 deamon 8 2 users has total proces
  • 查找并删除超过 x 天的文件或文件夹

    我想删除超过 7 天的文件和文件夹 所以我尝试了 17 07 14 email protected cdn cgi l email protection find tmp mindepth 1 maxdepth 1 ctime 7 exec
  • 在cmake中集成bash测试脚本

    我有一个 C cmake 项目 它使用 Boost Test 进行单元测试 现在我想向 cmake 公开一系列 bash 脚本 用于集成测试 假设每个脚本在以下情况下返回 0PASS或某些情况下 0FAILURE 我希望每次运行时都执行每个
  • 独特的 Linux 文件名,可按时间排序

    以前我用的是uuidgen http man7 org linux man pages man1 uuidgen 1 html创建唯一的文件名 然后我需要通过 bash 脚本按日期 时间迭代该文件名 从那时起 我发现简单地通过 ls l 循

随机推荐