小编ila*_*sch的帖子

bash中单次操作获取grep的输出和返回值

我正在写一个 bash 脚本;我执行某个命令和grep。

pfiles $1 2> /dev/null | grep name # $1 Process Id
Run Code Online (Sandbox Code Playgroud)

响应将类似于:

sockname: AF_INET6 ::ffff:10.10.50.28  port: 22
peername: AF_INET6 ::ffff:10.16.6.150  port: 12295
Run Code Online (Sandbox Code Playgroud)

响应可以是无行、1 行或 2 行。
如果 grep 不返回任何行(grep 返回代码 1),我将中止脚本;如果我得到 1 行,我调用 A() 或 B() 如果超过 1 行。当输出为 1-2 行时,grep 的返回码为 0。

grep 有返回值(0 或 1)和输出。
我怎样才能抓住他们?如果我做这样的事情:

OUTPUT=$(pfiles $1 2> /dev/null | grep peername)
Run Code Online (Sandbox Code Playgroud)

然后变量 OUTPUT 将有输出(字符串);我还想要 grep 执行的布尔值。

shell grep bash exit-status

12
推荐指数
2
解决办法
8万
查看次数

用户属于目录组时无权创建子目录

给定一个目录 DirA(目录所有者是root,组是sys)。
给定用户 userA,用户属于组 sys,他应该能够在 DirA 中创建一个子目录?

在 Solaris SunOS 5.9 上,我有一个目录/opt

bash-2.05$ ls -la /
total 1205
drwxr-xr-x  34 root     root        1024 Mar 17 04:21 .
drwxr-xr-x  34 root     root        1024 Mar 17 04:21 ..
.. Removed all..
lrwxrwxrwx   1 root     other         16 Apr 14  2008 opt -> /export/home/opt
Run Code Online (Sandbox Code Playgroud)

这个符号链接带我到:

bash-2.05$ ls -la /export/home
total 524638
drwxr-xr-x  31 root     root        1024 Jan 25  2015 .
drwxr-xr-x   3 root     sys          512 Jul  2  2007 …
Run Code Online (Sandbox Code Playgroud)

filesystems solaris directory

2
推荐指数
1
解决办法
923
查看次数

标签 统计

bash ×1

directory ×1

exit-status ×1

filesystems ×1

grep ×1

shell ×1

solaris ×1