AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题

问题[subshell](unix)

Martin Hope
Erwann
Asked: 2024-03-13 14:10:06 +0800 CST

获取当前 shell 环境和干净 shell 环境之间“declare -F”的增量

  • 6

主要问题:如何获得declare -F当前 shell 中的值与 shell 刚刚启动时的值之间的增量(下面的前两个命令)。$(declare -F)并不能解决问题,因为子shell是shell进程的副本。子公司:为什么下面第三个命令没有输出任何内容?

$ exec env -i bash                   
$ declare -F      
declare -f ShowInstallerIsoInfo
declare -f __expand_tilde_by_ref
declare -f __get_cword_at_cursor_by_ref
declare -f __load_completion
declare -f __ltrim_colon_completions
declare -f __parse_options
declare -f __reassemble_comp_words_by_ref
declare -f _allowed_groups
declare -f _allowed_users
declare -f _available_interfaces
declare -f _bashcomp_try_faketty
declare -f _bq_completer
declare -f _cd
declare -f _cd_devices
declare -f _command
declare -f _command_offset
declare -f _complete_as_root
declare -f _completer
declare -f _completion_loader
declare -f _configured_interfaces
declare -f _count_args
declare -f _dvd_devices
declare -f _expand
declare -f _filedir
declare -f _filedir_xspec
declare -f _fstypes
declare -f _get_comp_words_by_ref
declare -f _get_cword
declare -f _get_first_arg
declare -f _get_pword
declare -f _gids
declare -f _have
declare -f _included_ssh_config_files
declare -f _init_completion
declare -f _installed_modules
declare -f _ip_addresses
declare -f _kernel_versions
declare -f _known_hosts
declare -f _known_hosts_real
declare -f _longopt
declare -f _mac_addresses
declare -f _minimal
declare -f _modules
declare -f _ncpus
declare -f _open_files_for_editing
declare -f _parse_help
declare -f _parse_usage
declare -f _pci_ids
declare -f _pgids
declare -f _pids
declare -f _pnames
declare -f _python_argcomplete
declare -f _quote_readline_by_ref
declare -f _realcommand
declare -f _rl_enabled
declare -f _root_command
declare -f _service
declare -f _services
declare -f _shells
declare -f _signals
declare -f _split_longopt
declare -f _sysvdirs
declare -f _terms
declare -f _tilde
declare -f _uids
declare -f _upvar
declare -f _upvars
declare -f _usb_ids
declare -f _user_at_host
declare -f _usergroup
declare -f _userland
declare -f _variables
declare -f _xfunc
declare -f _xinetd_services
declare -f dequote
declare -f quote
declare -f quote_readline
$ "$SHELL" -c 'declare -F'   

其他:

$ uname -a
Linux elitebook 6.7.3-arch1-2 #1 SMP PREEMPT_DYNAMIC Fri, 02 Feb 2024 17:03:55 +0000 x86_64 GNU/Linux
$ bash --version
GNU bash, version 5.2.26(1)-release (x86_64-pc-linux-gnu)
$ echo $SHELL
/bin/bash

更新:

干净状态是一个空函数列表,如输出所示bash -c 'declare -F'

根据第一个命令,我预计第二个命令会输出两个函数而不是零。

$ grep -F -f <(declare -F | cut --delimiter=' ' --fields=3) ~/.bashrc | grep -v -E '^#' |  wc -l
2
$ bash -c 'source ~/.bashrc; declare -F'

更新2

我期望第二个输出两个函数而不是零。

采购不生效的原因可能是:

$ cat ~/.bashrc
#
# ~/.bashrc
#

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

解决方法确实输出两个函数。

$ bash -c 'source <(grep -v '\''!= *i*'\'' ~/.bashrc); compgen -A function' 2>/dev/null
subshell
  • 1 个回答
  • 27 Views
Martin Hope
jokoyoporo
Asked: 2022-10-05 05:31:04 +0800 CST

为什么在busybox sh中运行异步作业需要/dev/null?

  • 0

我很好奇为什么需要这个特殊的设备来分叉命令并在最小的 Busybox shell 中异步运行它。

BusyBox v1.30.1 (Debian 1:1.30.1-4) built-in shell (ash)
Enter 'help' for a list of built-in commands.

/bin/sh: can't access tty; job control turned off
/ #
/ # echo Hello && sleep 2s && echo World &
/bin/sh: / # can't open '/dev/null': No such file or directory

/ #
/ # mknod /dev/null c 1 3 && chmod 666 /dev/null
/ # echo Hello && sleep 2s && echo World &
/ # Hello
World

/ #
busybox subshell
  • 1 个回答
  • 38 Views
Martin Hope
Saeed Neamati
Asked: 2022-01-28 08:53:56 +0800 CST

为什么在所有子shell之后不执行此脚本中的等待?

  • 7

在这个脚本中,它会拉取所有 git 存储库:

#!/bin/bash

find / -type d -name .git 2>/dev/null | 
while read gitFolder; do
    if [[ $gitFolder == *"/Temp/"* ]]; then
        continue;
    fi
    if [[ $gitFolder == *"/Trash/"* ]]; then
        continue;
    fi
    if [[ $gitFolder == *"/opt/"* ]]; then
        continue;
    fi
    parent=$(dirname $gitFolder);
    echo "";
    echo $parent;
    (git -C $parent pull && echo "Got $parent") &
done 
wait
echo "Got all"

不wait等待所有git pull子shell。

为什么会这样,我该如何解决?

subshell wait
  • 1 个回答
  • 506 Views
Martin Hope
x-yuri
Asked: 2021-11-10 17:43:44 +0800 CST

为 INT 设置陷阱在子 shell 中不起作用

  • 1
$ bash -c "trap \"echo INT\" INT; sleep 3" & pid=$!; sleep 1; kill -INT $pid; wait
[1] 27811
INT
[1]+  Done                    bash -c "trap \"echo INT\" INT; sleep 3"

$ (bash -c "trap \"echo INT\" INT; sleep 3" & pid=$!; sleep 1; kill -INT $pid; wait)

你能解释一下为什么SIGINT处理程序在第二种情况下没有被调用吗?

shell subshell
  • 1 个回答
  • 240 Views
Martin Hope
Brad Parks
Asked: 2021-10-30 11:55:05 +0800 CST

bash:使用子shell分配给变量,但如果分配失败则退出主脚本

  • 0

我想要一个 bash 脚本,它允许我调用一个函数来查看文件中是否包含某些数据,如果它没有使主脚本失败,类似于以下内容,简化为保留这一点。

这不起作用(当子 shell 失败时不退出主脚本)。

我该如何编写require_line函数,这样我就可以像这样在一个文件中说出其中的 20 多个

VALUE1=$(require_line "myKey1")
VALUE2=$(require_line "myKey2")
...

并且不需要一个 if 围绕每个?

#!/bin/bash
set -eo pipefail

VALUE=$(require_line "myKey")

require_line(){
  local KEY=$1
  local DATA=$(cat /tmp/myfile)
  local INFO=$(echo "$DATA" | grep "$KEY")

  if [ ! -z "$INFO" ]
  then
    echo "Key not found in $DATA, key: $KEY"
    exit 1;
  fi
  echo "$INFO"
}
bash subshell
  • 2 个回答
  • 284 Views
Martin Hope
Nick Bull
Asked: 2020-02-05 11:47:38 +0800 CST

外壳数学是否在子外壳中运行?

  • 1

当我在 中执行简单的数学运算时#!/bin/sh,是否会创建一个子shell?

例如,

addition=$(( 1 + 1 ))

语法会建议一个子shell,但我找不到任何东西

shell subshell
  • 2 个回答
  • 459 Views
Martin Hope
Tim
Asked: 2018-12-23 08:39:45 +0800 CST

如何在子 shell 退出之前停止子 shell 的子进程(根据 SIGSTOP)?

  • 1

在 bash 中,当运行时,当子shell 退出时( sleep 123 &),进程将继续运行。sleep 123如何sleep 123在其父子外壳退出之前停止该进程?

我正在尝试查看该sleep 123过程是否会因接收SIGHUP和而终止SIGCONT。我正在寻找SIGHUP 是否已发送到此孤立进程的示例,为什么它不终止?内核是否默认将 SIGHUP 发送到成为孤立并包含已停止进程的进程组终止所有进程?

bash subshell
  • 1 个回答
  • 465 Views
Martin Hope
leeand00
Asked: 2018-05-09 22:31:19 +0800 CST

$() 是一个子shell吗?

  • 76

我理解子shell语法是(<commands...>),$()只是一个可以从中检索变量值的子shell?

注意:这适用于 bash 4.4,基于其文档中的不同措辞。

bash subshell
  • 3 个回答
  • 22859 Views
Martin Hope
Curious Coder
Asked: 2018-02-19 18:15:06 +0800 CST

如何在与 shell 脚本不同的目录中执行 ant 脚本?

  • 1

我正在编写一个 shell 脚本来自动化一个复杂的构建过程。它包括执行ant位于不同目录中的多个脚本。

shell 脚本位于我的主目录中,而 ant 脚本位于: /opt/myApp/module1/ ant -f patch.xml和/opt/myApp2/module2 ant build.xml.

如何从不同的文件夹位置在我的 shell 脚本中调用 ant 命令?

shell-script subshell
  • 2 个回答
  • 1333 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve