在 bash 中,你有这个方便的变量: $BASHPID ,它总是返回当前运行的子 shell 的 PID。如何在 ksh 中获取子 shell 的 PID?例如看下面的代码:
#!/usr/bin/ksh93
echo "PID at start: $$"
function run_in_background {
echo "PID in run_in_background $$"
run_something &
echo "PID of backgrounded run_something: $!"
}
function run_something {
echo "*** PID in run_something: $$"
sleep 10;
}
run_in_background
echo "PID of run in background $!"
Run Code Online (Sandbox Code Playgroud)
这将输出以下内容:
PID at start: 5328
PID in run_in_background 5328
*** PID in run_something: 5328
PID of backgrounded run_something: 5329
PID of run in background …Run Code Online (Sandbox Code Playgroud) 我有以下一系列命令:
cd / && ls | ( cd /tmp && cat >dumpfile)
Run Code Online (Sandbox Code Playgroud)
这一系列命令执行以下操作:它创建一个名为/tmp/dumpfile. 该文件包含根目录的列表。
该cd / && ls输出被输送到一个子shell。我觉得奇怪的是,在子外壳中,不是cd /tmp吞下ls输出,而是 后者cat >dumpfile获取它并将其写入文件。这里发生了什么?
Solaris / sh
我在一个文件中定义了一些函数,这些函数通过加载
. ./some_file.sh
Run Code Online (Sandbox Code Playgroud)
当我开始一个子shell时
sh
Run Code Online (Sandbox Code Playgroud)
我所有的函数定义都丢失了,但是当我这样做时
env
Run Code Online (Sandbox Code Playgroud)
我确实看到了源代码,有没有一种简单的方法可以让它们在我的子 shell 中运行?
我确定这是张贴在某个地方,但我一直找不到它。
在 Bash 中,如何在不创建子 shell 的情况下指定运算符优先级(又名命令分组)?在大多数其他语言中,()这样做,但在 Bash 中,它在“丢弃”环境更改的子外壳中运行命令。我想在不丢失环境更改的情况下指定运算符优先级。
具体来说,我想做这样的事情,并让整个脚本退出,而不仅仅是 中的子shell ():
die ()
{
echo "[DIE]: $1"
exit 1
}
# When installChruby returns an error, print the error message and exit
[[ $CHRUBY =~ [Yy] ]] && (installChruby || die "Error installing chruby")
Run Code Online (Sandbox Code Playgroud)
我通过这样做找到了一个“解决方法”,但它不是我想要的一个漂亮的单线:
if [[ $CHRUBY =~ [Yy] ]]; then installChruby || die "Error installing Chruby"; fi
Run Code Online (Sandbox Code Playgroud)
期望的结果是什么都不做并在CHRUBY未设置时继续,installChruby如果CHRUBY是Y或则调用函数y,并且die仅当installChruby函数返回 false时才调用函数。
Bash 中是否有一个运算符可以执行此操作 …
我可以grep输出jobs,我可以grep输出 a function。但是为什么我不能jobs在函数中grep 输出呢?
$ # yes, i can grep jobs
$ jobs
[1]+ Running vim
[2]+ Stopped matlab
$ jobs | grep vim
[1]+ Running vim
$ # yes, of course i can grep a function
$ type mockjobs
mockjobs is a function
mockjobs ()
{
echo '[1]+ Running vim banjo'
}
$ mockjobs | grep vim
[1]+ Running vim banjo
$ # now put those two together and surely …Run Code Online (Sandbox Code Playgroud) 在此脚本中,将拉取所有 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"
Run Code Online (Sandbox Code Playgroud)
不wait等待所有git pull子 shell。
为什么会这样?我该如何解决?
I understand that when I run exit it terminates my current shell because exit command run in the same shell. I also understand that when I run exit & then original shell will not terminate because & ensures that the command is run in sub-shell resulting that exit will terminate this sub-shell and return back to original shell. But what I do not understand is why commands with and without & looks exactly the same under pstree, in this …
我有一个简单的脚本来检查接口是否已连接:
connected=$(ping -I $1 -qc 1 8.8.8.8 \
&& echo "connected" \
|| echo "not connected")
echo "$connected" | tail -n1
Run Code Online (Sandbox Code Playgroud)
我期望它应该简单地返回“已连接”或“未连接”。当我实际使用脚本时,我总是在输出之前收到警告。
ping: Warning: source address might be selected on device other than enp0s25.
not connected
Run Code Online (Sandbox Code Playgroud)
这里的警告是什么,它不应该是子外壳的一部分吗?
考虑以下两个 bash 命令。一个创建子shell,另一个不创建。
没有子外壳
$ bash -c "sleep 10000 "
Run Code Online (Sandbox Code Playgroud)
pstree 输出:
bash,100648
??sleep,103509 10000
Run Code Online (Sandbox Code Playgroud)
带子壳
$ bash -c "sleep 10000; sleep 99999 "
bash,100648
??bash,103577 -c sleep 10000; sleep 99999
??sleep,103578 10000
Run Code Online (Sandbox Code Playgroud)
这是某种优化吗?Bash 查看命令并跳过简单命令的进程创建。看起来简单的命令是由父终端的 bash 生成的。
我正在使用的 Bash 版本是
$ bash --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
Run Code Online (Sandbox Code Playgroud)
不过,我认为在 bash 3.X 中也可以观察到同样的情况。
还有一些例子。使用内置函数生成子shell。内置程序不会在父 bash 中执行。
$ bash -c "read"
bash,100648
??bash,104336 -c read
Run Code Online (Sandbox Code Playgroud)
sleep可以通过top输出重定向和输出重定向来重现相同的行为。
$ bash -c "top -b "
bash,100648
??top,104392 -b
Run Code Online (Sandbox Code Playgroud)
和
$ bash -c …Run Code Online (Sandbox Code Playgroud) 我正在尝试nohup通过在后台执行命令并注销并重新登录并期望命令的输出在nohup.out文件中来测试bash shell 中的命令。这是我要执行的命令:
$ nohup (sleep 120; echo "job done") &
Run Code Online (Sandbox Code Playgroud)
但我不断收到错误消息:
bash: syntax error near unexpected token `sleep`
Run Code Online (Sandbox Code Playgroud) subshell ×10
bash ×6
shell ×5
function ×2
jobs ×2
pipe ×2
shell-script ×2
cd-command ×1
env ×1
job-control ×1
ksh ×1
nohup ×1
ping ×1
wait ×1