标签: shell

可移植地过滤掉无效的PID

我写了一个脚本来获取顶级会话PID,即会话启动器,它可能是像bash、dash、ksh甚至systemd这样的shell。该脚本可以得到一个PID的,但是我需要对其进行过滤,以检查它是一个有效的整数,而不是像一个初始参数34fg45,-5467我不希望它开始以零喜欢05467。

这是脚本的一个片段。

if [ "$1" != "" ]; then
    if [[ "$1" == [1-9]*([0-9]) ]]; then                <- Check for Integer; error here in non bash shell 
        if ps -p $1 -o "pid=" >/dev/null 2>&1; then
            pid=$1
        else
            echo "PID $1, no such process." >&2
            exit 1
        fi
    else
        echo "Invalid pid." >&2
        exit 1
    fi
else
    pid=$$
fi
Run Code Online (Sandbox Code Playgroud)

代码在 bash 中运行,但无法在 dash 上运行并出现语法错误:

./tspid: 16: ./tspid: Syntax error: "(" unexpected (expecting "then")
Run Code Online (Sandbox Code Playgroud)

我的理解是

if …

bash shell posix sh

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

如何在bash中仅提取两个字符串之间多行的第一个实例?

我的文件是:

abc
123
xyz
abc
675
xyz
Run Code Online (Sandbox Code Playgroud)

我想提取:

abc
123
xyz
Run Code Online (Sandbox Code Playgroud)

(123 可以是任何东西,重点是我想要第一次出现)

我尝试使用这个:

sed -n '/abc/,/xyz/p' filename
Run Code Online (Sandbox Code Playgroud)

但这给了我所有的实例。我怎么能得到第一个?

shell awk sed

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

无法使用 set -eo pipefail 在管道中成功调用任何外部 shell 脚本

假设以下test-pipefail.sh批处理:

#!/usr/bin/env bash
set -eo pipefail
./echoer.sh | head -n1 >/dev/null
echo "Might work with pipefail"
for i in {1..100} ; do
    ./echoer.sh | head -n1 >/dev/null
done
echo "Stable work with pipefail"
Run Code Online (Sandbox Code Playgroud)

使用echoer.sh内容:

#!/usr/bin/env bash
echo 'head (GNU coreutils) 8.30'
echo 'GNU bash, version 5.0.16(1)-release (x86_64-pc-linux-gnu)'
exit 0
Run Code Online (Sandbox Code Playgroud)

./test-pipefail.sh 的预期结果:

Might work with pipefail
Stable work with pipefail
Run Code Online (Sandbox Code Playgroud)

实际行为:

Might work with pipefail
Run Code Online (Sandbox Code Playgroud)

或(随机)没有输出。

如果我使用任何二进制实用程序而不是echoer.sh,管道中的 Writer 程序永远不会失败,但如果 writer 是 shell 脚本(例如,来自 glibc …

linux bash shell pipe built-in

3
推荐指数
2
解决办法
198
查看次数

如何在脚本启动时隐藏部分未使用的变量

在这种情况下,如何在未使用 bash 命令定义变量时隐藏变量的使用:

EXCLUDE_PORTS="--exclude-ports $2"

if [ "${EXCLUDE_PORTS:-}" == "" ]; then
    EXCLUDE_PORTS=''
fi
Run Code Online (Sandbox Code Playgroud)

我还需要隐藏 --exclude-ports,当未指定 $2 时。实际上,当我没有在脚本启动时指定 $2 时,脚本只取第一部分为空的“--exclude-ports”,这破坏了我的脚本。

linux bash shell

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

为什么我的小 C 程序使用 cat 实用程序打印不同的字符串?

#include <string.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
  char a[6] = "abcdd";
  char b[5] = "1234";

  strcpy(a, b);

  write(1, a, 4);
  printf("AA\n");
  write(1, b, 4);
  printf("CC\n");
}

Run Code Online (Sandbox Code Playgroud)

我正在学习 strcpy func。

使用 ./a.out

1234AA
1234CC
Run Code Online (Sandbox Code Playgroud)

与 ./a.out | 猫-e

12341234AA$
CC$
Run Code Online (Sandbox Code Playgroud)

我读过猫。找不到任何与此。

编译后还能做哪些改变?

发生了什么?我缺少什么概念?

c shell

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

在 sed 中,我可以防止 \S 匹配括号吗?

输入文件有这个内容

you have to identify (http://third-view.herokuapp.com/) 
Run Code Online (Sandbox Code Playgroud)

我想删除 URL,所需的输出是

you have to identify ()
Run Code Online (Sandbox Code Playgroud)

但是下面的 Sed 命令也删除了 ')' 括号。

sed -i -e 's!http[s]\?://\S*!!g' file
Run Code Online (Sandbox Code Playgroud)

shell awk sed

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

为什么“假;回声$?” 返回 1,如果错误的 Solaris 源以 255 退出?

我需要找到实现 false 的源代码。
我在github上找到了源代码并找到了false.c,其中 false 以代码 255 退出。
那么,为什么“false; echo $”会出现?在 shell 中返回 1 而不是 255?我想我错过了某个地方的来源。

false.c文件中的代码:

#pragma ident   "%Z%%M% %I% %E% SMI"

#include <unistd.h>

/*
 * Exit with a non-zero value as quickly as possible.
 */

int
main(void)
{
    _exit(255);
    /*NOTREACHED*/
    return (0);
}
Run Code Online (Sandbox Code Playgroud)

bash shell solaris opensolaris

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

如何使用 sed 或 awk 在模式后删除某些字符?

我有一个包含行数的文本文件,格式如下

001_A.wav;112.680;115.211;;;Ja. Hello; Hi:
Run Code Online (Sandbox Code Playgroud)

我的目标是清理之后的任何东西;;;。删除以下字符的意思,;()~?

我知道我可以做类似的事情sed 's/[,.;()~?,]//g'。但是,如果我这样做,它会给我类似的东西

001_Awav112.680115211Ja Hello Hi
Run Code Online (Sandbox Code Playgroud)

但是我只想在之后删除这些字符,;;;所以我会得到

001_A.wav;112.680;115.211;;;Ja Hello Hi
Run Code Online (Sandbox Code Playgroud)

我怎样才能完成这个任务?

bash shell awk sed

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

linux中基于两个键合并两个长度不等的文件

我有两个不同长度的txt文件。

File 1:
Albania 20200305    0
Albania 20200306    0
Albania 20200307    0
Albania 20200308    0
Albania 20200309    3
Albania 20200310    7
Albania 20200311    4
Albania 20200312    2
Run Code Online (Sandbox Code Playgroud)
File 2: 
Europe  Albania 20200309    2
Europe  Albania 20200310    6
Europe  Albania 20200311    10
Europe  Albania 20200312    11
Europe  Albania 20200313    23
Europe  Albania 20200314    33
Run Code Online (Sandbox Code Playgroud)

如果 File1 的第 1 和第 2 列与 File2 的第 2 和第 3 列相同,我想创建一个 File3,它将在 File2 的末尾添加 File1 的 3. 列。它应该是这样的:

File3:
Europe  Albania 20200309    2   3
Europe …
Run Code Online (Sandbox Code Playgroud)

shell merge awk multiple-columns

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

从bash中的句子中删除特定单词?

我想使用 bash 脚本从句子中删除否定词。

我的意思是消极的话:

[dull,boring,annoying,bad]
Run Code Online (Sandbox Code Playgroud)

我的文件文本text.txt包含这句话:

These dull boring cards are part of a chaotic board game ,and bad for people 
Run Code Online (Sandbox Code Playgroud)

我正在使用这个脚本

array=( dull boring annoying bad  )
for i in "${array[@]}"
do
cat $p  | sed -e 's/\<$i\>//g' 
done < my_text.txt
Run Code Online (Sandbox Code Playgroud)

但是我得到了以下错误的结果:

These boring cards are part of a chaotic board game ,and bad for people 
Run Code Online (Sandbox Code Playgroud)

正确的输出必须是这样的:

These cards are part of a chaotic board game ,and for people 
Run Code Online (Sandbox Code Playgroud)

bash shell sed

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

标签 统计

shell ×10

bash ×6

awk ×4

sed ×4

linux ×2

built-in ×1

c ×1

merge ×1

multiple-columns ×1

opensolaris ×1

pipe ×1

posix ×1

sh ×1

solaris ×1