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
    • 最新
    • 标签
主页 / user-38638

TomRoche's questions

Martin Hope
TomRoche
Asked: 2020-01-15 21:43:34 +0800 CST

新十年:如何说出类似 `find /path/ -name 'file.20{19,20}*'` 之类的话(但哪个有效)

  • 3

概括:

  1. 一个给定的系统有很多带有名字的文本文件~= [type of file].[8-digit date]。
  2. 要搜索这些文件,我喜欢(并希望保留)使用这个成语:(find /path/ -name 'file.nnnn*' -print | xargs -e fgrep -nH -e 'text I seek'其中nnnn== 4 位数年份)
  3. ......在过去的十年里,我也做find了很多年的球,比如find /path/ -name 'file.201[89]*' -print | xargs ...
  4. ...但现在我无法find在 2019 年和 2020 年使用find /path/ -name 'file.20{19,20}*' -print | xargs ...
  5. ......虽然那个“花括号通配符”(正确的术语?)适用于ls!

有没有一种{简洁、优雅}的方式来告诉find我想要什么,而不是进行后期find清理(即,我现在正在做什么)?

find /path/ -name 'file.*' -print | grep -e '\.2019\|\.2020' | xargs ...

? FWIW,我更喜欢与xargs.

细节:

我在一个系统上工作,该系统的很多约定早在我之前,并且我无法更改。其中之一是,它有很多名称为 的文本文件~= [type of file].[8-digit date],例如woohoo_log.20191230. 在这些文件中搜索某些给定文本时,我通常(如几乎总是)使用find ... grep成语(通常使用 Emacs' M-x find-grep)。(FWIW,这是一个 Linux 系统

$ find --version
find (GNU findutils) 4.4.2
...
$ bash --version
GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu)

如果我愿意,我目前缺乏改变其中任何一个的状态。)我经常有点知道手头问题的年份范围,因此会尝试限制find返回的内容(以加快处理速度),例如)

find /path/ -type f -name 'file.nnnn*' -print | xargs -e fgrep -nH -e 'text I seek'

其中nnnn== 4 位数年份。这个 WFM,我喜欢(并且想保留)使用上述成语......特别是因为我也可以用它来搜索多年

find /path/ -type f -name 'file.201[89]*' -print | xargs ...

但这个新的十年似乎打破了这个成语,而且(至少对我来说)最奇怪的是。(过去十年发生变化时我不在这里。)假设我选择了我知道在 2019 年的文件中的文本 && 2020 年的文件(例如,我可以打开文件并查看文本)。如果我现在这样做

find /path/ -name 'file.20{19,20}*' -print | xargs ...

grep意外/令人讨厌地完成with no matches found,因为

$ find /path/ -name 'file.20{19,20}*' -print | wc -l
0

但如果我这样做

find /path/ -type f -name 'file.*' -print | grep -e '\.2019\|\.2020' | xargs ...

grep返回预期的结果。这很好,但是......嗯......这很丑陋,特别是因为这个“花括号 glob”(如果这个用法不正确或不推荐使用,请纠正我)从ls!即,这向我显示了相关年份范围内的文件(即 2019..2020)

ls -al /path/file.20{19,20}*

因此我想知道:

  1. 我只是没有find为这个用例提供正确的 glob 吗?我需要告诉什么find才能让它做ls有能力/正确做的事情?
  2. 这是一个问题xargs吗?如果是这样,我可以接受一个find ... -exec解决方案,但是......我的大脑xargs在 . (叫我弱智,但-exec' 的语法让我的大脑受伤。)
find wildcards
  • 3 个回答
  • 774 Views
Martin Hope
TomRoche
Asked: 2019-06-17 23:37:12 +0800 CST

bash `case` 语句将输入分类为非整数和整数

  • 3

摘要:我想使用一个bash case语句(在其他代码中)对输入进行分类,以确定它们是否是

  • 一个正整数
  • 一个负整数
  • 零
  • 一个空字符串
  • 一个非整数字符串

可执行代码如下,正确分类以下输入:

  • ''
  • word
  • a\nmultiline\nstring
  • 2.1
  • -3

但是将以下两者都归类为...负整数:-(

  • 0
  • 42

细节:

将以下内容保存到文件(例如/tmp/integer_case_statement.sh)中chmod,然后运行它:

#!/usr/bin/env bash

### This should be a simple `bash` `case` statement to classify inputs as
### {positive|negative|zero|non-} integers.
### Trying extglob, since my previous integer-match patterns failed.
### Gotta turn that on before *function definition* per https://stackoverflow.com/a/34913651/915044
shopt -s extglob

declare cmd=''

function identify() {
    local -r it=${1}  # no quotes in case it's multiline
#    shopt -s extglob # can't do that here
    case ${it} in
        '')
            # empty string, no need for `if [[ -z ...`
            >&2 echo 'ERROR: null arg'
            ;;
        ?(-|+)+([[:digit:]]))
            # it's an integer, so just say so and fallthrough
            >&2 echo 'DEBUG: int(it), fallthrough'
            ;&
        -+([[:digit:]]))
            # it's negative: just return it
            >&2 echo 'DEBUG: int(it) && (it < 0), returning it'
            echo "${it}"
            ;;
        0)
            # it's zero: that's OK
            >&2 echo 'DEBUG: int(it) && (it == 0), returning it'
            echo '0'
            ;;
        ++([[:digit:]]))
            # it's positive: just return it
            >&2 echo 'DEBUG: int(it) && (it > 0), returning it'
            echo "${it}"
            ;;
        *)
            # not an integer, just return it
            >&2 echo 'DEBUG: !int(it)'
            echo "${it}"
            ;;
    esac
} # end function identify

echo -e "'bash --version'==${BASH_VERSION}\n"

echo "identify '':"
identify ''
echo
# > ERROR: null arg

echo 'identify word:'
identify word
echo
# > DEBUG: !int(it)
# > word

echo 'identify a
multiline
string:'
identify 'a
multiline
string'
echo
# > DEBUG: !int(it)
# > a
# > multiline
# > string

echo 'identify 2.1:'
identify 2.1
echo
# > DEBUG: !int(it)
# > 2.1

echo 'identify -3:'
identify -3
echo
# > DEBUG: int(it), fallthrough
# > DEBUG: int(it) && (it < 0), returning it
# > -3

echo 'identify 0:'
identify 0
echo
# > DEBUG: int(it), fallthrough
# > DEBUG: int(it) && (it < 0), returning it
# > 0

echo 'identify 42:'
identify 42
echo
# > DEBUG: int(it), fallthrough
# > DEBUG: int(it) && (it < 0), returning it
# > 42

exit 0

当前输出被内联在文件中,但为了便于阅读,这里单独列出我当前的输出:

'bash --version'==4.3.30(1)-release

identify '':
ERROR: null arg

identify word:
DEBUG: !int(it)
word

identify a
multiline
string:
DEBUG: !int(it)
a
multiline
string

identify 2.1:
DEBUG: !int(it)
2.1

identify -3:
DEBUG: int(it), fallthrough
DEBUG: int(it) && (it < 0), returning it
-3

identify 0:
DEBUG: int(it), fallthrough
DEBUG: int(it) && (it < 0), returning it
0

identify 42:
DEBUG: int(it), fallthrough
DEBUG: int(it) && (it < 0), returning it
42

后两个输入是我的问题:为什么 case 语句识别

  • 0 作为负整数(而不是 0)
  • 42 作为负整数(而不是正整数)

? 感谢您的帮助。

bash wildcards
  • 1 个回答
  • 1378 Views
Martin Hope
TomRoche
Asked: 2018-02-07 16:17:40 +0800 CST

`declare` 不会导致缺少函数/命令的问题?

  • -5

摘要:如果bash我尝试将缺失函数的输出分配给先前的declared(即,不是 {constant,read-only})变量,我可以通过“正常”测试检测到失败。但是,如果我在对它进行操作时尝试将缺失函数的输出分配给一个变量declare(例如,使 var {constant, read-only}),不仅分配不会因“正常”测试而失败,而且我不能使用“正常”内置函数强制失败。我怎样才能使后一种情况失败?

细节:

我最近在一个更大的bash脚本中遇到了一个问题,我试图将其提炼成以下 2 个脚本。基本上,我有点像用bash( snark > /dev/null) 做 TDD,所以除其他外

  • 我希望缺少的命令/功能快速失败
  • 我想防止重写常量

但是,bash似乎允许我在declare输入 var 时将缺失函数的输出分配给变量。例如,以下脚本(另存为/path/to/assign_at_declare.sh)...

#!/usr/bin/env bash

function foo() {
    return 0
}

# function bar() {}               # does not exist!

declare ret_val=''
declare -r MESSAGE_PREFIX="$(basename "${BASH_SOURCE}"):"
declare -r ERROR_PREFIX="${MESSAGE_PREFIX} ERROR:"

echo -e "\n${MESSAGE_PREFIX} a naïve 1st attempt:\n"

declare -ir FOO1_VAL="$(foo)"   # this should succeed, and does
ret_val="${?}"
if   [[ "${ret_val}" -ne 0  ]] ; then
    >&2 echo "${ERROR_PREFIX} foo returned '${ret_val}', exiting ..."
    exit 3
elif [[ -z "${FOO1_VAL}"  ]] ; then
    >&2 echo "${ERROR_PREFIX} foo returned null, exiting ..."
    exit 4
else
    echo "${MESSAGE_PREFIX} FOO1_VAL='${FOO1_VAL}'"
fi

declare -ir BAR1_VAL="$(bar)"   # this should fail ... but doesn't
ret_val="${?}"
if   [[ "${ret_val}" -ne 0  ]] ; then
    >&2 echo "${ERROR_PREFIX} bar returned '${ret_val}', exiting ..."
    exit 5
elif [[ -z "${BAR1_VAL}"  ]] ; then
    >&2 echo "${ERROR_PREFIX} bar returned null, exiting ..."
    exit 6
else
    echo "${MESSAGE_PREFIX} BAR1_VAL='${BAR1_VAL}'"
fi

echo -e "\n${MESSAGE_PREFIX} get tough using \`set\` builtins:\n"
# see https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
set -o errexit
set -o pipefail

declare -ir FOO2_VAL="$(foo)"   # this should succeed, and does
ret_val="${?}"
if   [[ "${ret_val}" -ne 0  ]] ; then
    >&2 echo "${ERROR_PREFIX} foo returned '${ret_val}', exiting ..."
    exit 3
elif [[ -z "${FOO2_VAL}"  ]] ; then
    >&2 echo "${ERROR_PREFIX} foo returned null, exiting ..."
    exit 4
else
    echo "${MESSAGE_PREFIX} FOO2_VAL='${FOO2_VAL}'"
fi

declare -ir BAR2_VAL="$(bar)"   # this should fail ... but doesn't
ret_val="${?}"
if   [[ "${ret_val}" -ne 0  ]] ; then
    >&2 echo "${ERROR_PREFIX} bar returned '${ret_val}', exiting ..."
    exit 5
elif [[ -z "${BAR2_VAL}"  ]] ; then
    >&2 echo "${ERROR_PREFIX} bar returned null, exiting ..."
    exit 6
else
    echo "${MESSAGE_PREFIX} BAR2_VAL='${BAR2_VAL}'"
fi

exit 0

...产生以下输出:

assign_at_declare.sh: a naïve 1st attempt:

assign_at_declare.sh: FOO1_VAL='0'
/path/to/assign_at_declare.sh: line 27: bar: command not found
assign_at_declare.sh: BAR1_VAL='0'

assign_at_declare.sh: get tough using `set` builtins:

assign_at_declare.sh: FOO2_VAL='0'
/path/to/assign_at_declare.sh: line 56: bar: command not found
assign_at_declare.sh: BAR2_VAL='0'

这看起来很奇怪,因为如果我在输入 var后 declare尝试将缺失函数的输出分配给变量(即,如果 var不是{constant, read-only}) ,我没有观察到这种行为,如以下脚本所示(另存为/path/to/assign_after_declare.sh)...

#!/usr/bin/env bash

function foo() {
    return 0
}

# function bar() {}           # does not exist!

declare ret_val=''
declare -i foo_val=0
declare -i bar_val=0
declare -r MESSAGE_PREFIX="$(basename "${BASH_SOURCE}"):"
declare -r ERROR_PREFIX="${MESSAGE_PREFIX} ERROR:"

echo -e "\n${MESSAGE_PREFIX} following works as expected\n"

foo_val="$(foo)"           # this should succeed, and does with/out `declare`
ret_val="${?}"
if   [[ "${ret_val}" -ne 0  ]] ; then
    >&2 echo "${ERROR_PREFIX} foo returned '${ret_val}', exiting ..."
    exit 3
elif [[ -z "${foo_val}"  ]] ; then
    >&2 echo "${ERROR_PREFIX} foo returned null, exiting ..."
    exit 4
else
    echo "${MESSAGE_PREFIX} foo_val='${foo_val}'"
fi

bar_val="$(bar)"           # this succeeds with `declare`, fails without
ret_val="${?}"
if   [[ "${ret_val}" -ne 0  ]] ; then
    >&2 echo "${ERROR_PREFIX} bar returned '${ret_val}', exiting ..."
    exit 5
elif [[ -z "${bar_val}"  ]] ; then
    >&2 echo "${ERROR_PREFIX} bar returned null, exiting ..."
    exit 6
else
    echo "${MESSAGE_PREFIX} bar_val='${bar_val}'"
fi

exit 0

...产生以下输出:

assign_after_declare.sh: following works as expected

assign_after_declare.sh: foo_val='0'
/path/to/assign_after_declare.sh: line 29: bar: command not found
assign_after_declare.sh: ERROR: bar returned '127', exiting ...

在 a 期间分配时有没有办法强制bash快速失败?如果是这样,我等待您的答复。declare

或者,是否bash按设计工作?如果是这样,请链接到参考资料;我试着搜索这个问题,但我的选择器要么不正确,要么返回了太多不相关的响应,以至于毫无用处。

bash shell-builtin
  • 1 个回答
  • 133 Views
Martin Hope
TomRoche
Asked: 2017-11-29 20:02:29 +0800 CST

来自 `service | 的意外结果 grep`

  • 0
$ sudo service --status-all | fgrep -e 'bluetooth'
 [ ? ]  alsa-utils
 [ ? ]  binfmt-support
 [ + ]  bluetooth
 [ ? ]  cpufrequtils
 [ ? ]  cryptdisks
 [ ? ]  cryptdisks-early
 [ ? ]  hdparm
 [ ? ]  hwclock.sh
 [ ? ]  kmod
 [ ? ]  loadcpufreq
 [ ? ]  networking
 [ ? ]  plymouth
 [ ? ]  plymouth-log
 [ ? ]  pppd-dns
 [ ? ]  udev-finish
 [ ? ]  virtualbox-guest-x11
$ sudo service bluetooth stop
[ ok ] Stopping bluetooth: /usr/sbin/bluetoothd.
$ sudo service --status-all | fgrep -e 'bluetooth'
 [ ? ]  alsa-utils
 [ ? ]  binfmt-support
 [ - ]  bluetooth
 [ ? ]  cpufrequtils
 [ ? ]  cryptdisks
 [ ? ]  cryptdisks-early
 [ ? ]  hdparm
 [ ? ]  hwclock.sh
 [ ? ]  kmod
 [ ? ]  loadcpufreq
 [ ? ]  networking
 [ ? ]  plymouth
 [ ? ]  plymouth-log
 [ ? ]  pppd-dns
 [ ? ]  udev-finish
 [ ? ]  virtualbox-guest-x11

service | grep当只有 1 行(在每个输出集中)与模式匹配时,为什么我会得到 1 个以上的输出行grep?线条开头有什么(坏的)魔力[?]吗?或者,我错过了什么?

FWIW,我在跑步

$ date
Tue Nov 28 20:51:46 MST 2017
$ uname -rsv
Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.43-2+deb8u5 (2017-09-19)
$ lsb_release -ds
LMDE 2 Betsy
$ cat /etc/debian_version
8.9
$ gcc --version | head -n 1
gcc (Debian 4.9.2-10) 4.9.2
grep services
  • 1 个回答
  • 93 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