假设我有以下输入文件:
Server 'Test AB'
option type 'ss'
option port '1234'
option timeout '60'
Server 'Test CD'
option type 'ss'
option port '1234'
option timeout '60'
Server 'Test EF'
option type 'ss'
option port '1234'
option timeout '60'
Server 'Test GH'
option type 'ss'
option port '1234'
option timeout '60'
Run Code Online (Sandbox Code Playgroud)
现在我想使用 sed 将“选项端口'1234'”与“选项端口'9876'”交换,但我只想为服务器“Test EF”执行此操作。所有其他端口应保持不变。
输出应如下所示:
Server 'Test AB'
option type 'ss'
option port '1234'
option timeout '60'
Server 'Test CD'
option type 'ss'
option port '1234'
option timeout '60'
Server 'Test …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个 bash 脚本,该脚本将遍历目录\n并将文件重命名为“Chapter1.mp3”、“Chapter2.mp3”(文件名真的很疯狂,任何人都无法识别它们是什么)等,\n但是当我这样做时,出现以下错误:
\nmv: target \'Chapter##.mp3\' is not a directory\nRun Code Online (Sandbox Code Playgroud)\n我记得,mv命令允许您指定文件名和目录...为什么我会收到此错误?以下是我的代码:
cptr=0;\n\nfor i in ./myFolder/*; do\n cptr=$(($cptr+1)); # I can\'t remember, can I use cptr++?\n mv -v $i "Chapter$cptr.mp3";\ndone;\nRun Code Online (Sandbox Code Playgroud)\n我尝试过删除文件扩展名,删除引号,甚至使用cp代替,但我尝试过的任何方法都不起作用。\xc2\xa0\n为什么它要求目标只能是目录而不是文件名?
我正在使用带有适用于 Linux 的 Linux 子系统的 Ubuntu 20.04 LTS 终端;\n这有什么区别吗?
\n我需要编写一个zsh具有多个退出点的函数,并在每个退出点处执行相同的清理命令:
function foo {
if ... ; then
...
run_cleanup
return
elif ... ; then
some_command || (run_cleanup ; return 1)
else
...
fi
...
run_cleanup
}
Run Code Online (Sandbox Code Playgroud)
有没有办法避免run_cleanup在每个退出点重复(这很容易出错)?
我想为 git log 创建一个特殊的别名,这样当我使用它时,它会使用自定义的漂亮格式--graph并进行标记。--pretty
我知道我可以使用 git 别名,但是,我不想这样做,因为我想让它动态化,这样它也可以接受额外的参数/标志。另外,我想了解 bash 中的包装是如何工作的,所以我想尝试一下。
这就是我想要它做的事情;
当我使用
git log spc
^_______(custom argument)
Run Code Online (Sandbox Code Playgroud)
它运行如下
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %C(bold blue)<%an>%Creset %C(grey)(%cr) ' --abbrev-commit --date=relative
Run Code Online (Sandbox Code Playgroud)
因此,为此,我在我的中添加了此命令(来自此答案)~/.bash_profile
## git wrapper
git()
{
if [ $# -gt 0 ] && [ "$1" == "log" ] && [ "$2" == "spc" ]; then
shift
set -x
command git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %C(bold blue)<%an>%Creset %Cgreen(%cr) ' --abbrev-commit --date=relative "$@"
else
command git …Run Code Online (Sandbox Code Playgroud) 我经常发现在使用命令行时,我需要复制最后一个命令的输出中出现的路径。例如我可能会find . | grep这样使用:
[user@localhost /tmp]$ find . | grep B
./directoryAB
./directoryBA
./directoryBB
./directoryBC
./directoryCB
Run Code Online (Sandbox Code Playgroud)
此时我需要拿起鼠标,复制我想要的路径,然后输入cd,然后粘贴。但如果我可以循环输出中的路径并仅使用键盘复制我想要的路径,那就太棒了。有什么可以帮助我做到这一点吗?
我使用 zsh 和 iTerm2,fwiw。
if [ -h "demo" ]; then
echo "-h: true"
fi
if [ -L "demo" ]; then
echo "-L: true"
fi
Run Code Online (Sandbox Code Playgroud)
我发现“-h”和“-L”都可以用来判断指定文件是否是链接。它们之间有什么区别?
我想检查命令是否正在使用以下if子句运行,但它总是true,无论命令实际上是否正在运行:
if [ $"(ps aux | grep curl)" ]; then echo is; else echo not; fi
Run Code Online (Sandbox Code Playgroud)
结果:
[root@is ~]# if [ $"(ps aux | grep curl)" ]; then echo is; else echo not; fi
is
Run Code Online (Sandbox Code Playgroud)
ps外部的命令if:
[root@is ~]# ps aux | grep curl
root 216564 0.0 0.2 6408 2064 pts/0 S+ 22:22 0:00 grep --color=auto curl
Run Code Online (Sandbox Code Playgroud)
如何检查curl我的示例是否正在运行?
我尝试使用不同的参数ps,但我总是有grep命令(这在我看来是正常的),但我不知道如何检查它是否正在运行。
更新1
我想我可以做到这一点,但我不知道如何在命令中做到这一点:
if ps aux blah
if it has …Run Code Online (Sandbox Code Playgroud) 当打印带有 Windows 行结尾 (CRLF) 的文件时,如果使用选项,则在空行中less显示回车符。包含任何文本的行都会正确显示,跳过回车符。^M--hilite-unread
我看了一遍说明书,好像只要CR后面加LF就应该是一样的。添加--raw-control-chars会删除标记,但不建议使用它,并且它会产生我不想要的其他后果。
$ printf 'first\r\n\r\nsecond\r\n' | LESS= less --quit-if-one-screen
first
second
$ printf 'first\r\n\r\nsecond\r\n' | LESS= less --quit-if-one-screen --hilite-unread
first
^M
second
$ printf 'first\r\n\r\nsecond\r\n' | LESS= less --quit-if-one-screen --hilite-unread --raw-control-chars
first
second
Run Code Online (Sandbox Code Playgroud)
这是 中的已知错误less,还是我可以做些什么?现在我将不再使用--hilite-unread.
有趣的是,当文本足够长以滚动时,向上滚动时^M标记会消失。因此,这些仅针对新打印的行显示。可能less还不知道 LF 会在那里吗?但为什么它适用于带有文本的行呢?
我使用 Windows 版 Git,版本:git 2.43.0.windows.1、bash 5.2.21(1)、less 643。
有没有办法禁用双 Tab 键行为(按 Tab 两次显示 Bash 中的可用命令)?
我有大约 15 个 100 多个目录,特别是使用伪代码你怎么能:
rename * *suffix
rename * prefix*
rename * CAPITALIZE*
Run Code Online (Sandbox Code Playgroud)
但是,将不胜感激特定于处理多个文件和目录的教程的提示或链接。
bash ×10
linux ×4
command-line ×2
grep ×2
shell-script ×2
zsh ×2
git ×1
key-binding ×1
less ×1
mv ×1
newlines ×1
ps ×1
script ×1
sed ×1
text-editing ×1