小编Ale*_*502的帖子

如何在自己的进程组中启动 bash 脚本

我想从另一个 bash 脚本启动一个 bash 脚本,但在它自己的进程组中启动它,就像从终端运行它一样。

有几个类似的问题,但我找不到与我的示例相匹配的答案。

拿这两个脚本

$ cat main.sh 
#! /usr/bin/env bash

set -e

echo if this has been started in its own group the following will pass
ps -o ppid,pgid,command $$
ps -o pgid | grep $$
echo passed

$ cat wrapper.sh 
#! /usr/bin/env bash

set -e

./main.sh
Run Code Online (Sandbox Code Playgroud)

当我main.sh单独运行时,终端将其放入自己的组中:

$ ./main.sh 
if this has been started in its own group the following will pass
 PPID  PGID COMMAND
20553  1276 bash ./main.sh
 1276
 1276
 1276
passed
$ …
Run Code Online (Sandbox Code Playgroud)

bash process shell-script process-groups

9
推荐指数
1
解决办法
3509
查看次数

使 tail -f 在损坏的管道上退出

我想观看一个文件,直到出现一些文字

我找到了这个答案:`tail -f`直到看到文字

但是当我在 ubuntu 上尝试时,它没有退出:

$ echo one > test.txt
$ echo two >> test.txt
$ echo three >> test.txt
$ echo four >> test.txt
$ cat test.txt | sed '/w/ q'
one
two
$
Run Code Online (Sandbox Code Playgroud)

按预期工作。但是当我尝试拖尾文件时

$ tail -f test.txt | sed '/w/ q'
one
two
Run Code Online (Sandbox Code Playgroud)

它永远不会退出。即使管道破裂,尾巴也不会停止。

有人知道tail退出时如何sed退出吗?

pipe sed tail

5
推荐指数
1
解决办法
1868
查看次数

尝试使用 printf 解码作为参数传入的 unicode 字符

我正在尝试打印一些像这样输入的 unicode 代码

echo 0024 0025 | xargs -n1 echo # one code per line
  | xargs printf '\u%s\n'
Run Code Online (Sandbox Code Playgroud)

希望得到这个

$
%
Run Code Online (Sandbox Code Playgroud)

但这就是我得到的

printf: missing hexadecimal number in escape
Run Code Online (Sandbox Code Playgroud)

经过一些试验和错误,我实际上有两个较小的问题,一种是有道理的,另一种似乎完全是个谜。


问题1:

echo 0024 0025 | xargs -n1 echo # one code per line
  | xargs printf '\u%s\n'
Run Code Online (Sandbox Code Playgroud)

给我这个

-bash: printf: missing unicode digit for \u
\u0024
-bash: printf: missing unicode digit for \u
\u0025
Run Code Online (Sandbox Code Playgroud)

问题2:

$
%
Run Code Online (Sandbox Code Playgroud)

(使用>for$以便您可以$在输出中看到)

出于某种原因,有些字符适用于 exe 版本,但有些字符即使使用内置 printf 也适用。


所以这里有一个解决方法,如果不是问题#2,它会起作用(但可能比我原来的想法慢很多) …

bash unicode printf

3
推荐指数
1
解决办法
841
查看次数

标签 统计

bash ×2

pipe ×1

printf ×1

process ×1

process-groups ×1

sed ×1

shell-script ×1

tail ×1

unicode ×1