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-142980

Erwann's questions

Martin Hope
Erwann
Asked: 2024-03-13 14:10:06 +0800 CST

获取当前 shell 环境和干净 shell 环境之间“declare -F”的增量

  • 6

主要问题:如何获得declare -F当前 shell 中的值与 shell 刚刚启动时的值之间的增量(下面的前两个命令)。$(declare -F)并不能解决问题,因为子shell是shell进程的副本。子公司:为什么下面第三个命令没有输出任何内容?

$ exec env -i bash                   
$ declare -F      
declare -f ShowInstallerIsoInfo
declare -f __expand_tilde_by_ref
declare -f __get_cword_at_cursor_by_ref
declare -f __load_completion
declare -f __ltrim_colon_completions
declare -f __parse_options
declare -f __reassemble_comp_words_by_ref
declare -f _allowed_groups
declare -f _allowed_users
declare -f _available_interfaces
declare -f _bashcomp_try_faketty
declare -f _bq_completer
declare -f _cd
declare -f _cd_devices
declare -f _command
declare -f _command_offset
declare -f _complete_as_root
declare -f _completer
declare -f _completion_loader
declare -f _configured_interfaces
declare -f _count_args
declare -f _dvd_devices
declare -f _expand
declare -f _filedir
declare -f _filedir_xspec
declare -f _fstypes
declare -f _get_comp_words_by_ref
declare -f _get_cword
declare -f _get_first_arg
declare -f _get_pword
declare -f _gids
declare -f _have
declare -f _included_ssh_config_files
declare -f _init_completion
declare -f _installed_modules
declare -f _ip_addresses
declare -f _kernel_versions
declare -f _known_hosts
declare -f _known_hosts_real
declare -f _longopt
declare -f _mac_addresses
declare -f _minimal
declare -f _modules
declare -f _ncpus
declare -f _open_files_for_editing
declare -f _parse_help
declare -f _parse_usage
declare -f _pci_ids
declare -f _pgids
declare -f _pids
declare -f _pnames
declare -f _python_argcomplete
declare -f _quote_readline_by_ref
declare -f _realcommand
declare -f _rl_enabled
declare -f _root_command
declare -f _service
declare -f _services
declare -f _shells
declare -f _signals
declare -f _split_longopt
declare -f _sysvdirs
declare -f _terms
declare -f _tilde
declare -f _uids
declare -f _upvar
declare -f _upvars
declare -f _usb_ids
declare -f _user_at_host
declare -f _usergroup
declare -f _userland
declare -f _variables
declare -f _xfunc
declare -f _xinetd_services
declare -f dequote
declare -f quote
declare -f quote_readline
$ "$SHELL" -c 'declare -F'   

其他:

$ uname -a
Linux elitebook 6.7.3-arch1-2 #1 SMP PREEMPT_DYNAMIC Fri, 02 Feb 2024 17:03:55 +0000 x86_64 GNU/Linux
$ bash --version
GNU bash, version 5.2.26(1)-release (x86_64-pc-linux-gnu)
$ echo $SHELL
/bin/bash

更新:

干净状态是一个空函数列表,如输出所示bash -c 'declare -F'

根据第一个命令,我预计第二个命令会输出两个函数而不是零。

$ grep -F -f <(declare -F | cut --delimiter=' ' --fields=3) ~/.bashrc | grep -v -E '^#' |  wc -l
2
$ bash -c 'source ~/.bashrc; declare -F'

更新2

我期望第二个输出两个函数而不是零。

采购不生效的原因可能是:

$ cat ~/.bashrc
#
# ~/.bashrc
#

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

解决方法确实输出两个函数。

$ bash -c 'source <(grep -v '\''!= *i*'\'' ~/.bashrc); compgen -A function' 2>/dev/null
subshell
  • 1 个回答
  • 27 Views
Martin Hope
Erwann
Asked: 2023-04-24 12:48:15 +0800 CST

rsync:从源中排除目录

  • 6

它出现在下面的代码中,rsync忽略--exclude. 我将如何更正它并将其扩展到多个目录?

$ rsync --help | grep Usage
Usage: rsync [OPTION]... SRC [SRC]... DEST

$ man rsync | grep -- '--exclude'
   --exclude=PATTERN        exclude files matching PATTERN

$ sudo rsync -avz --exclude='/home/erwann/.cache/' '/home/erwann' '/run/media/erwann/bkup'
sending incremental file list
erwann/.cache/mozilla/firefox/otj9do4j.default-release/cache2/
erwann/.cache/mozilla/firefox/otj9do4j.default-release/cache2/index
erwann/.cache/mozilla/firefox/otj9do4j.default-release/cache2/doomed/
erwann/.cache/mozilla/firefox/otj9do4j.default-release/cache2/entries/
rsync
  • 1 个回答
  • 30 Views
Martin Hope
Erwann
Asked: 2023-04-06 05:15:54 +0800 CST

如何将脚本文件作为 heredoc 传递给 sed?

  • 5

这会将“a”转换为“A”。

sed 's/a/A/g' <(echo "foobar")
foobAr

这提供了使每个元音大写所需的命令:

$ cat << EOF
s/a/A/g
s/e/E/g
s/i/I/g
s/o/O/g
s/u/U/g
EOF

我想做的是扩展第一个示例,以便将每个元音字母大写,并使用此处的文档,如第二个示例所示。有什么建议吗?

男人:

sed [OPTION]... {script-only-if-no-other-script} [输入文件]...

-f script-file, --file=script-file 将script-file的内容添加到要执行的命令中

here-document
  • 2 个回答
  • 20 Views
Martin Hope
Erwann
Asked: 2022-05-20 19:00:32 +0800 CST

没有使用 sed 获得惰性评估

  • 0

我本来期望'a'为此,为什么'a:b'?

$ echo "a:b:c" | sed -E 's/(.+?):.+/\1/'
a:b
regular-expression sed
  • 1 个回答
  • 49 Views
Martin Hope
Erwann
Asked: 2022-03-19 20:20:36 +0800 CST

使用 heredoc 遍历命令列表

  • 0

这工作正常:

$ eval 'echo "1" > ~/Desktop/in/foo'

但不是这个:

$ while IFS= read _cmd; do eval "$_cmd"; done < <(cat << EOF
'echo "1" > ~/Desktop/in/foo'
'echo "2" > ~/Desktop/in/bar'
EOF
)
bash: echo "1" > ~/Desktop/in/foo: No such file or directory
bash: echo "2" > ~/Desktop/in/bar: No such file or directory

如何解决?


更新

正如建议的那样,删除单引号是可行的,但不适用于这种变体(同上,仅用单引号将文件名括起来):

$ while IFS= read _cmd; do eval "$_cmd"; done < <(cat << EOF 
echo "1" > ~/Desktop/in/foo (bis)
echo "2" > ~/Desktop/in/bar
EOF
)

连同提供的解决方案:

"$SHELL" << EOF
echo "1" > ~/Desktop/in/'foo (bis)'
echo "2" > ~/Desktop/in/bar
EOF
here-document eval
  • 1 个回答
  • 66 Views
Martin Hope
Erwann
Asked: 2022-03-15 21:51:52 +0800 CST

如何告诉`xargs`忽略空标准输出?

  • 0

如何更改此代码以免发出错误,换句话说,xargs如果 stdout 为空,则不执行代码。

终端:

$ cat << EOF > dummy.sh
#! /usr/bin/env bash
[[ -f "\$1" ]] &&  echo "file=\$1" || { echo "error"; exit 1; } 
shift 
(( \$# == 0 )) || "\$0" "\$@" 
EOF
$ chmod +x dummy.sh
$ mkdir trash.later && touch trash.later/foo
$ find trash.later -type f | xargs -n 1 ./dummy.sh 
file=trash.later/foo
$ find trash.later -type f | grep 'bar' | xargs -n 1 ./dummy.sh 
error
xargs
  • 1 个回答
  • 505 Views
Martin Hope
Erwann
Asked: 2022-03-15 20:55:26 +0800 CST

一个管道可以复合命令吗?

  • 0

我的问题是:一个管道可以/如何复合命令({ list; })?见附件 B。为了比较而给出的附件 A。

展品 A:

$ cat << EOF | xargs -n 1 echo           
foo
bar
qux
EOF
foo
bar
qux

展品 B:

$ cat << EOF | xargs -n 1 sh -c '{ var="$1"; echo "$var"; }'
foo
bar
qux
EOF


man sh:

-c               Read commands from the command_string operand
                            instead of from the standard input.  Special parame‐
                            ter 0 will be set from the command_name operand and
                            the positional parameters ($1, $2, etc.)  set from
                            the remaining argument operands.
shell pipe
  • 1 个回答
  • 53 Views
Martin Hope
Erwann
Asked: 2022-03-10 17:52:29 +0800 CST

如何正确使用 spawn-expect-send 进行“git push”?

  • 0

下面的代码改编自“在 Bash 脚本中使用 Expect 为 SSH 命令提供密码”的解决方案,以便将参数传递给git push. 我没有因为传递错误的 uname+pwd 而遇到任何异常,相反,传递正确的 uname+pwd 并不会真正推动任何事情。如何纠正?

git_push.sh

if (( $# == 2 ))
then
    :
else
    echo "expecting 'username pass', got $@"
    exit 1
fi

user="$1"
pass="$2"
expect - <<EOF
 spawn git push
 expect 'User*'
 send "$user\r"
 expect 'Pass*'
 send "$pass\r"
EOF

终端:

$ [path]/git_push.sh
spawn git push
Username for 'https://github.com': foo
Password for 'https://[email protected]': 

或者(没有通配符):

 spawn git push
 expect "Username for 'https://github.com': "
 send "$user\r"
 expect "Password for 'https://[email protected]': "
 send "$pass\r"
git expect
  • 1 个回答
  • 226 Views
Martin Hope
Erwann
Asked: 2022-03-06 17:57:39 +0800 CST

拆分空分隔字符串

  • 0

以下是两个read语句,一个使用空格作为分隔符,另一个使用\0. 只有第一个作品。第二个我做错了什么?

$ IFS=' '; read first second  < <(printf "%s " "x" "y" ); echo "$first+$second"

x+y

$ IFS=$'\0'; read first second  < <(printf "%s\0" "x" "y" ); echo "$first+$second"

xy+

read
  • 1 个回答
  • 218 Views
Martin Hope
Erwann
Asked: 2022-02-26 20:31:48 +0800 CST

选择行匹配模式,然后使用 `sed` 进行替换

  • 0

这是我要复制的内容:

$ cat << EOF | { var=$(grep 'foo=');\
 if (( $? == 0 )); then echo "$var"\
| sed -E 's/foo=(.+)/\1/'; else exit 1; fi; }
foo
foo=bar
EOF

酒吧

sed独占使用:

$ cat << EOF | sed -nE '/foo=/p s/foo=(.+)/\1/'
foo
foo=bar
EOF

sed:-e 表达式 #1,字符 9:命令后的额外字符

错误的原因是什么,如何解决?

regular-expression sed
  • 1 个回答
  • 66 Views
Martin Hope
Erwann
Asked: 2022-02-26 12:10:19 +0800 CST

是否有一个命令封装了 while+shift...?

  • 0

我的问题与编写可能与xargs. 我发现将迭代步骤和循环结合起来很麻烦,因为第二个可以变成命令。我必须重新发明轮子,所以:这方面存在什么?

while_do_it.sh:

#! /bin/bash

while (( $# > 0 ))
do    
    echo "($1)"
    shift
done

可以分为

while.sh

#! /bin/bash

command="$1"
shift

while (( $# > 0 ))
do
    "$command"  "$1" 
    shift
done

do_it.sh

#! /bin/bash

echo "($1)"

应用:

$ cat << EOF | xargs ./while.sh ./do_it.sh
foo 
bar 
qux 
EOF 
(Ctrl+D)

(foo)
(bar)
(qux)

xargs
  • 1 个回答
  • 88 Views
Martin Hope
Erwann
Asked: 2022-02-25 07:59:57 +0800 CST

将制表符分隔的条目传递给 readarray

  • 3

显然我做得不对,但我认为预期的结果很明确(${#arr[@]}=3)

$ readarray -d "\t" arr < <(printf "%s\t%s\t%s" "x" "y" "z"); echo "${#arr[@]}"
> 1
shell array
  • 1 个回答
  • 143 Views
Martin Hope
Erwann
Asked: 2022-02-22 16:07:11 +0800 CST

将 `"(.+)"` 或 `{(.+)}` 替换为 `\1`

  • 0

我可以

  1. {(.+)}-->\1和
  2. "(.+)"--> \1,或
  3. ({.+}|".+")--> \1。

我会做{(.+)}|"(.+)"-->\1吗?

$ echo "\"x\"" | sed -E 's/"(.+)"/\1/'
x
$ echo "{x}" | sed -E 's/{(.+)}/\1/'
x
$ echo "\"x\"" | sed -E 's/^(\{.+\}|".+")$/\1/' 
"x"
regular-expression sed
  • 2 个回答
  • 75 Views
Martin Hope
Erwann
Asked: 2022-02-19 09:12:12 +0800 CST

如何编写脚本以便我可以通过管道找到它?

  • 0

我有一个脚本,它需要文件作为参数,并为每个文件执行一组指令。我应该如何编写它才能将其通过管道传输到find?在下面的示例中,我没有find这样使用。我假设它的输出具有相同的结构printf "%s\n..."(不是吗?)

my-script.sh:

# !/bin/bash

help()
{
    echo "help"
    echo
}

while [[ $# -gt 0 ]]
do
    case $1 in
        *)
            echo "$1"
            shift # past argument
            ;;
    esac
done

美好的:

$ cat foo; echo; cat bar
foo  

bar
$ ./my-script.sh foo bar
foo
bar

但是之后:

$ printf "%s\n%s\n" "./foo" "./bar" | xargs -n 1 ./my-script.sh
./my-script.sh: 9: ./my-script.sh: [[: not found
./my-script.sh: 9: ./my-script.sh: [[: not found
pipe xargs
  • 1 个回答
  • 66 Views
Martin Hope
Erwann
Asked: 2019-10-30 13:14:33 +0800 CST

如何读取`apt search`返回的第一个输出字段?

  • 3

我猜i是安装了,但是其他的呢?

$ apt search * | sort -t' ' -k1 | cut -c1 | uniq
c
i
p
v
apt
  • 1 个回答
  • 404 Views
Martin Hope
Erwann
Asked: 2019-07-03 14:42:42 +0800 CST

将标准输出及其替换加入表中

  • 0

说我有:

$ cat tmp1.txt
a
b
c

然后

$ paste tmp1.txt <(tr '[:lower:]' '[:upper:]'<tmp1.txt)
a   A
b   B
c   C

我将如何将以上内容修改为

1/ 只调用tmp1.txt一次?(我怀疑tee)

2/ 提供tmp1.txt来自终端的内容(就像由命令的输出产生的一样)。为此,我试图对此进行修补,但并没有走得太远:

$ cat<< 'EOF' | tee >(tr '[:lower:]' '[:upper:]')
a
b
c
EOF

输出:

a
b
c
A
B
C
io-redirection tee
  • 2 个回答
  • 58 Views
Martin Hope
Erwann
Asked: 2019-06-28 20:00:54 +0800 CST

来自 getopts 的意外行为

  • 3

考虑:

#!/bin/sh

while getopts ":h" o; do
  case "$o" in
    h )
    "Usage:
    sh $(basename "$0") -h      Displays help message
    sh $(basename "$0") arg     Outputs ...

     where:
    -h   help option
        arg  argument."
    exit 0
    ;;
    \? )
    echo "Invalid option -$OPTARG" 1>&2
    exit 1
    ;;
    : )
    echo "Invalid option -$OPTARG requires argument" 1>&2
    exit 1
    ;;
  esac
done

这个调用返回not found为什么?

$ sh getopts.sh -h
getopts.sh: 12: getopts.sh: Usage:
    sh getopts.sh -h        Displays help message
    sh getopts.sh arg   Outputs ...

     where:
    -h   help option
        arg  argument.: not found

还行吧:

$ sh getopts.sh arg

对于这个,我期待“无效选项”:

$ sh getopts.sh

还行吧:

$ sh getopts.sh -s x
Invalid option -s
shell-script getopts
  • 1 个回答
  • 403 Views
Martin Hope
Erwann
Asked: 2019-06-27 14:06:16 +0800 CST

sed -E 用于组

  • 1

这个

$echo '| foo | bar | baz |' | sed 's@^|[^|]*|\([^|]*\)|.*@\1@'

根据需要返回bar。我如何对 opt 做同样的事情-E?

https://www.gnu.org/software/sed/manual/html_node/Extended-regexps.html

regular-expression sed
  • 1 个回答
  • 115 Views
Martin Hope
Erwann
Asked: 2019-06-26 20:04:34 +0800 CST

sed 的贪婪似乎被违反了

  • 0

我会认为这两个调用sed会返回相同的输出,因为默认情况下它会以贪婪的方式查找模式。为什么不?

$ echo '<a href="/topic/null-hypothesis/" data-sc="text link:topic link">Null hypothesis</a>' | grep -E '<a href="/topic.*</a>' | sed 's/<a href=.*">//'
Null hypothesis</a>

$ echo '<a href="/topic/null-hypothesis/" data-sc="text link:topic link">Null hypothesis</a>' | grep -E '<a href="/topic.*</a>' | sed 's/<a href=.*>//'

PS:

$ bash --version
bash --version
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)

PS2:我从 更正sed为grep。

PS3:改回grep为sed。

sed
  • 1 个回答
  • 54 Views
Martin Hope
Erwann
Asked: 2019-05-06 12:17:59 +0800 CST

使用 curl 安装的 Phantom 二进制文件

  • 0

youtube-dl我按照这个指令升级了二进制文件。locate似乎找到它,不是ls,并且它不能被调用。怎么了?

/usr/bin $ sudo apt-get remove youtube-dl
/usr/bin $ sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
/usr/bin $ sudo chmod a+rx /usr/local/bin/youtube-dl
/usr/bin $ locate youtube-dl
/usr/bin/youtube-dl
/usr/share/bash-completion/completions/youtube-dl
/usr/share/doc/youtube-dl
/usr/share/man/man1/youtube-dl.1.gz
/usr/share/zsh/vendor-completions/_youtube-dl
/var/lib/dpkg/info/youtube-dl.list
/var/lib/dpkg/info/youtube-dl.md5sums
/var/lib/dpkg/info/youtube-dl.postinst
/var/lib/dpkg/info/youtube-dl.postrm
/var/lib/dpkg/info/youtube-dl.preinst
/var/lib/dpkg/info/youtube-dl.prerm
/usr/bin $ youtube-dl --version
bash: /usr/bin/youtube-dl: No such file or directory
/usr/bin $ ls y*
yacc  ybmtopbm  yelp  yes  yuvsplittoppm  yuvtoppm
curl youtube-dl
  • 2 个回答
  • 85 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