我有一台运行 Windows 10(或 Windows 11)的笔记本电脑。我可以ssh
使用 进入我的 Ubuntu 计算机ssh [email protected]
,但我想 (a) 使用 Windows 中的 GUI 应用程序或 (b) 使用 Windows 中的整个桌面。理想情况下,我想通过 X 转发而不是 VNC 或 RDP 来完成此操作。请注意,我在这里没有使用 Wayland。
我怎样才能做到这一点?
我有一台运行 Windows 10(或 Windows 11)的笔记本电脑。我可以ssh
使用 进入我的 Ubuntu 计算机ssh [email protected]
,但我想 (a) 使用 Windows 中的 GUI 应用程序或 (b) 使用 Windows 中的整个桌面。理想情况下,我想通过 X 转发而不是 VNC 或 RDP 来完成此操作。请注意,我在这里没有使用 Wayland。
我怎样才能做到这一点?
这会隐藏第一个命令的输出Oops
,如果失败则打印到 stderr。到目前为止,一切都很好。
#!/usr/bin/env bash
invalid_command > /dev/null 2>&1 || >&2 echo "Oops"
echo hi
输出如下:
Oops
hi
但是如果第一个命令失败,我需要退出并打印一条消息。所以我尝试使用括号。
#!/usr/bin/env bash
invalid_command > /dev/null 2>&1 || ( >&2 echo "Oops" ; exit )
echo hi
这是它的输出:
Oops
hi
但是现在exit
不起作用,因为它是在子shell中执行的,导致它 print hi
,即使我希望脚本退出。
echo
那么,如果特定命令使用操作符失败,我如何让 Bash 进入某些文本并退出||
?我知道我可以使用单线来做到这一点,但如果可以避免的话if
,我宁愿不必使用完整的声明。if
#!/usr/bin/env bash
if [ "$(invalid_command > /dev/null 2>&1 ; printf $?)" != "0" ]; then >&2 echo 'Oops' ; exit 1; fi
我喜欢Micro 文本编辑器。我个人更喜欢它nano
。然而,即使我已经安装了它,Git 默认为nano
. 如何更改文本编辑器git commit
使用的内容?
我正在使用 Ubuntu 21.10。如果我搞砸了,我可以重新运行最后一个命令!!
。这是一个例子:
$ apt update
Reading package lists... Done
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)
$ sudo !!
sudo apt update
[sudo] password for [me]:
...
Fetched 336 kB in 2s (206 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
98 packages can be upgraded. Run 'apt list --upgradable' to see them.
没关系,但不是重新运行上一个命令,而是如何运行它之前的命令?像这样:
$ echo hi
hi
$ echo hello
hello
$ !! <-- I'm trying to get that to run 'echo hi'
hello
基本上,这就是我要找的
$ echo hi
hi
$ echo hello
hello
$ ??? <-- What can I put here to get it to run 'echo hi'?
echo hello!
hello!
那么,鉴于!!
运行最后一个命令,我如何在前一个命令之前运行该命令?在我的示例中,!!
我可以运行什么来执行echo hi
?
所以我以 root 身份运行了一个愚蠢的命令(不要运行)
# chown -R root:root /{usr,lib,var,etc,bin,sbin,tools}
所以……哎呀。我碰巧打开了一个root shell(从我造成这个的时候开始),所以我可以sudo
通过做
# chown root:root /usr/bin/sudo
# chmod 4755 /usr/bin/sudo
耶,sudo
又工作了。我想安装一个包(ncdu
),所以我跑了sudo apt-get install ncdu
。
$ sudo apt-get install ncdu
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
ncdu
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/43.4 kB of archives.
After this operation, 106 kB of additional disk space will be used.
Selecting previously unselected package ncdu.
(Reading database ... 233654 files and directories currently installed.)
Preparing to unpack .../ncdu_1.15.1-1_amd64.deb ...
Unpacking ncdu (1.15.1-1) ...
Setting up ncdu (1.15.1-1) ...
Processing triggers for man-db (2.9.4-2) ...
/usr/bin/mandb: can't chmod /var/cache/man/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/es/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/es/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/es/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/zh_TW/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/zh_TW/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/zh_TW/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/fi/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/fi/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/fi/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/de/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/de/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/de/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/uk/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/uk/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/uk/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/sr/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/sr/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/sr/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/sl/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/sl/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/sl/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/nb/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/nb/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/nb/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/pt/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/pt/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/pt/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/ru/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/ru/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/ru/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/ko/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/ko/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/ko/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/it/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/it/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/it/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/fr/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/fr/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/fr/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/et/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/et/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/et/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/ca/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/ca/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/ca/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/tr/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/tr/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/tr/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/nl/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/nl/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/nl/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/fr.UTF-8/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/fr.UTF-8/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/fr.UTF-8/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/hu/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/hu/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/hu/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/fr.ISO8859-1/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/fr.ISO8859-1/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/fr.ISO8859-1/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/sv/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/sv/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/sv/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/id/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/id/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/id/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/el/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/el/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/el/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/pt_BR/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/pt_BR/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/pt_BR/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/ro/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/ro/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/ro/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/zh_CN/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/zh_CN/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/zh_CN/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/cs/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/cs/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/cs/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/gl/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/gl/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/gl/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/da/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/da/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/da/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/pl/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/pl/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/pl/1053532: Permission denied
/usr/bin/mandb: can't chmod /var/cache/man/ja/CACHEDIR.TAG: Operation not permitted
/usr/bin/mandb: can't remove /var/cache/man/ja/CACHEDIR.TAG: Permission denied
/usr/bin/mandb: fopen /var/cache/man/ja/1053532: Permission denied
显然我没有做足够的事情来修复我的系统。如果可能的话,我不想重新安装操作系统,但我有一个 root shell(它是我面前的一台物理笔记本电脑),所以我想知道如何修复apt
,以便我可以再次安装包。我正在使用 Kubuntu 21.10。
我需要停止 GUI 并访问 NVIDIA 驱动程序的 TTY。我发现了这个问题。但是,当我尝试sudo service lightdm stop
(或与 相同gdm
)时,它说:
$ sudo service lightdm stop
Failed to stop lightdm.service: Unit lightdm.service not loaded.
$ sudo service gdm stop
Failed to stop gdm.service: Unit gdm.service not loaded.
我不想搞乱 Grub,因为我只需要停止 GUI 一次。那么,如何阻止 GUI 使用在 Kubuntu 21.10 中根本没有运行 GUI 的 TTY?
注意:我知道如何访问 TTY。但是一旦我在 TTY 中,我需要停止 GUI,因为 NVIDIA 驱动程序安装程序希望我在 TTY中并且 GUI not running。
所以我有一个这样的脚本:
somecommad | grep --invert-match something
somecommand
如果失败,我希望能够有条件地运行不同的命令。这是我尝试过的:
somecommand | grep --invert-match something || {
echo 'Oops'
}
但这没有用(grep
没有执行)。这样做的正确方法是什么?