作为一个新用户,我最近发现了使用热键在 $EDITOR 中打开当前终端行的技巧C-x C-e
。这改变了我的生活,使编写循环等变得容易得多。
有没有办法在终端进入 python shell 后做到这一点?
在编写快速 Python 脚本时我遇到了同样的挫折。
我的主机上有一个 shell 脚本,它正在调用另一个脚本在容器内运行。这是一个运行良好的简单案例:
主机脚本
#!/bin/sh
results=$(docker exec mycontainer "./scripts/container_script.sh")
echo $results
容器脚本
#!/bin/sh
echo "Hello World"
但是,现在我想使用输入参数来运行容器脚本。像这样:
主机脚本
#!/bin/sh
path="/some/other/path"
results=$(docker exec mycontainer "./scripts/container_script.sh $path")
echo $results
容器脚本
#!/bin/sh
echo "Hello World"
echo $1
这次我收到错误:
OCI 运行时执行失败:exec 失败:无法启动容器进程:exec:“。/scripts/container_script.sh /some/other/path”:stat ./scripts/container_script.sh /some/other/path:没有此文件或目录:未知
我如何在容器内向该脚本传递参数?
我尝试使用 bash 在 oracle linux 8.6 中使用模式取消注释文件中的特定行。某些行上的前导空格未被删除。我尝试使用 sed 和 grep 取消注释注释行以匹配模式。我需要精确匹配输出中的两个数字。文件中每列(共 2 列)有一个单词,每个单词都有数字。
例如: column1:pd 19 _ORA column2:svg 38。
我需要取消注释完全匹配 19 和 38 的行,但不包含 190、1900 或 019 等,例如,
#pd19_ORA svg37
#pd199_ORA svg388
代码:
sed -n '/\<19\>/,+1p' cmfile|grep '38'|sed -i '/38/s/^#//g' cmfile
文件内容:
#pd19_ORA svg38
#pd19_ORA sil38
#pd29_ORA sil37
使用 sed 和 inplace 后第一行仍然被注释,但第二行的注释被删除。
输出:
#pd19_ORA svg38
pd19_ORA sil38
#pd29_ORA sil37
如何删除第一行带有前导空格的注释而不删除前导空格?
预期输出:
pd19_ORA svg38
pd19_ORA sil38
#pd29_ORA sil37
使用以下命令行(我将其转换为别名),我想从我的终端打开 pdf 文件。
别名 pdfLoad="fd . '$HOME/OneDrive/DBCalibre' -tf -e pdf | fzf | xargs -0 -I {} masterpdfeditor5 {}"
然而,我注意到由特定字符(例如“)引起的问题,它不允许我打开该文件。
OS: Arch Linux x86_64
Kernel: 6.12.4-arch1-1
Shell: zsh 5.9
DE: GNOME 47.2
fzf 0.56.3 (add1aec6)
fd 10.2.0
masterpdfeditor5 Build 5.9.86, 64 bit
我有一个同步文件夹的脚本。该脚本
/usr/bin/rsync -av --delete $SOURCE $DEST >> $LOG_FILE 2>> $ERR_LOG_FILE
因此它将输出重定向到LOG_FILE
并将错误输出重定向到ERR_LOG_FILE
。
此外,我希望root
通过电子邮件发送错误信息。我该怎么做?
我在 Linux 机器上有一个非常基本的文本文件,其中包含章节、对话和参考资料等内容。
这就是它的样子
Chapter: 1 One: Birds and Trees
Birds are beautiful and trees are amazing and
they are dependent on each other. Birds most of the time
choose to make their nests on trees since trees provide more
stability. One day the bird sat on a tree and said;
Bird: Oh my I'm so tired from all the flying, I should take a rest
Tree: Mr Bird, you seem tired, perhaps you should take some rest, and
here are some fruits to quench your thirst.
Bird: Oh thank you very much!
Reference: Chapter 1: birds and trees
Chapter: 2 Two: Trees and Fruits
Fruits are very delicious to eat and they are mostly found
in trees. Fruits contain essential vitamins, minerals and loads
of good fibers.
Reference: Chapter 2: trees and fruits
这些是 txt 文件的内容。现在假设我正在搜索quench
,我认为它会从章节号开始直到参考。所以我尝试使用 grep;
$ grep -A 5 -B 5 'quench' file.txt
但是,这并没有产生所需的输出。我期望的是这样的;
Chapter: 1 One: Birds and Trees
Birds are beautiful and trees are amazing and
they are dependent on each other. Birds most of the time
choose to make their nests on trees since trees provide more
stability. One day the bird sat on a tree and said;
Bird: Oh my I'm so tired from all the flying, I should take a rest
Tree: Mr Bird, you seem tired, perhaps you should take some rest, and
here are some fruits to quench your thirst.
Bird: Oh thank you very much!
Reference: Chapter 1: birds and trees
并且,搜索单词“维生素”会打印出来;
Chapter: 2 Two: Trees and Fruits
Fruits are very delicious to eat and they are mostly found
in trees. Fruits contain essential vitamins, minerals and loads
of good fibers.
Reference: Chapter 2: trees and fruits
我想知道这是否可以通过 sed 或 awk 实现。
PS:每一行都是真正的新行
我可以使用date
以下命令进行一些日期运算
$ date -u -d '3 days 2024-12-01T00:00:00Z' +%FT%H:%M:%SZ
2024-12-04T00:00:00Z
$
该date
命令可以解析相当多的日期格式,包括 ISO-8601 格式。有没有一种简单的方法可以让它也能解释 ISO-8601 间隔?简单的方法失败了...
$ date -u -d 'PT3D 2024-12-01T00:00:00Z' +%FT%H:%M:%SZ
date: invalid date ‘PT3D 2024-12-01T00:00:00Z’
$
有人能解释一下,我应该将自己的 shell 脚本和配置文件放在 macOS 上的哪个目录(或目录)中吗?
/usr/local/bin/
?/usr/local/etc/
?$HOME/bin/
?$HOME/etc/
?最佳实践还是最常见的方法?
示例文件为:
webpage2pdf.zsh
- 一个将网页转换为 PDF 的 ImageMagick 脚本.webpage2pdf
- 它的配置文件.zshrc_my-own
~/.zshrc
- 一个我使用中获取的 Zsh 配置文件source
,按照 Gilles 的建议,可以避免意外覆盖:https://unix.stackexchange.com/a/787409我想知道是否可以编写一个符合 POSIX 的find
脚本,根据其参数构建命令。
另一篇 SE文章解释了如何对 Bash(和 zsh)执行此操作,但它使用了特定于 bash(或 zsh)的工具。
最初的问题是能够构建这样的查找命令:
find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'
答案是以下 Bash 脚本代码:
findany() {
local args=('-iname' "*$1*")
shift
while [ "$#" -gt 0 ]; do
args+=('-o' '-iname' "*$1*")
shift
done
find . "${args[@]}"
}
是否可以在符合 POSIX 的脚本中执行类似操作?
在他们的网站上,您被要求通过脚本安装他们的程序,而不是像大多数常规应用程序那样通过软件包存储库下载。
LV_BRANCH='release-1.4/neovim-0.9' bash <(curl -s https://raw.githubusercontent.com/LunarVim/LunarVim/release-1.4/neovim-0.9/utils/installer/install.sh)
安装过程,但我想创建一个脚本来回答这些问题。如何编写一个命令来自动处理这些问题?