将 3 行 bash 脚本拆分为一行

use*_*229 2 scripting bash

以下是我在谷歌搜索后发现的:

dpkg --list 'linux-image-*' \
    | perl -ane 'BEGIN { $r = `uname -r` or die; chomp $r } print $F[1], "\n" if $F[0] eq "ii" && $F[1] !~ /\Q$r\E\b/' \
    | xargs -r aptitude purge -y
Run Code Online (Sandbox Code Playgroud)

有人可以将上述 3 行合二为一吗?我发现将一行复制并粘贴到 sudo 命令中比三行更容易,一次一行。

对于 Hauke Laging:当我尝试按照 sudo 命令一次复制三个分割线时,我编辑了这篇原始帖子以包含错误消息:

username@hostname:~$ sudo -i
[sudo] password for username: 
root@hostname:~# dpkg --list 'linux-image-*' \
> 
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version      Architecture Description
+++-==============-============-============-=================================
un  linux-image-2. <none>                    (no description available)
un  linux-image-2. <none>                    (no description available)
un  linux-image-2. <none>                    (no description available)
un  linux-image-2. <none>                    (no description available)
ii  linux-image-3. 3.14.4-1~bpo amd64        Linux 3.14 for 64-bit PCs
ii  linux-image-3. 3.2.57-3+deb amd64        Linux 3.2 for 64-bit PCs
ii  linux-image-am 3.14+57~bpo7 amd64        Linux for 64-bit PCs (meta-packag
root@hostname:~#     | perl -ane 'BEGIN { $r = `uname -r` or die; chomp $r } print $F[1], "\n" if $F[0] eq "ii" && $F[1] !~ /\Q$r\E\b/' \
-bash: syntax error near unexpected token `|'
root@hostname:~# 
root@hostname:~#     | xargs -r aptitude purge -y
Run Code Online (Sandbox Code Playgroud)

Hau*_*ing 6

没有必要在这三个中制作一行。如果换行符被 转义,shell 将删除换行符\。换句话说:带有超过三行参数的命令只能“看到”一行,而不是三行。

下一个问题是:

sudo cmd1 | cmd2 | cmd3
Run Code Online (Sandbox Code Playgroud)

导致仅以cmd1root 身份运行。你可以做类似的事情

sudo bash -c 'cmd1 | cmd2 | cmd3'
Run Code Online (Sandbox Code Playgroud)

但这可能会导致引用烦恼。

可能最简单的解决方案是:创建一个空文件,将三行复制到其中,然后使用以下命令运行它:

sudo bash file
Run Code Online (Sandbox Code Playgroud)

使用|安全换行符

您可以将 放在|一行的末尾。这样(额外的)换行符就不会出现问题。尝试这个:

dpkg --list 'linux-image-*' |
  perl -ane 'BEGIN { $r = `uname -r` or die; chomp $r } print $F[1], "\n" if $F[0] eq "ii" && $F[1] !~ /\Q$r\E\b/' |
  sudo xargs -r aptitude purge -y
Run Code Online (Sandbox Code Playgroud)

  • 还有`sudo sh &lt;&lt; \E`,粘贴并输入`E`。 (2认同)
  • @user66229 这对我来说看起来很奇怪。shell 中的第二行是 `&gt; ` 而不是 `&gt; | perl -ane ...`。因此,命令之间似乎有两个换行符,而不仅仅是一个。如果将其粘贴到文件中:那么命令之间是否有空行?但是这个问题可以通过移动`|`来避免。我会将其添加到我的答案中。 (2认同)

Gil*_*il' 5

反斜杠换行符是一种在语法通常不允许换行的地方拆分行的方法,因此您可以将其删除。

dpkg --list 'linux-image-*' | perl -ane 'BEGIN { $r = `uname -r` or die; chomp $r } print $F[1], "\n" if $F[0] eq "ii" && $F[1] !~ /\Q$r\E\b/' | xargs -r aptitude purge -y
Run Code Online (Sandbox Code Playgroud)

如果你想在 sudo 下运行它,你不能只是在 之后复制粘贴那一行sudo,因为只有dpkg命令才会以 root 身份运行。其实只aptitude需要以root身份运行即可。

dpkg --list 'linux-image-*' | perl -ane 'BEGIN { $r = `uname -r` or die; chomp $r } print $F[1], "\n" if $F[0] eq "ii" && $F[1] !~ /\Q$r\E\b/' | xargs -r sudo aptitude purge -y
Run Code Online (Sandbox Code Playgroud)

该命令的作用过于复杂。perl 脚本过滤一些已安装的包,但 aptitude 完全有能力进行过滤。

sudo aptitude purge -y "~i ^linux-image !?exact-name(linux-image-$(uname -r))"
Run Code Online (Sandbox Code Playgroud)