我只是想知道,在 bash 脚本中,新行在功能上是否 100% 等同于&&
?
例如:
#!/bin/bash
7z x "${file}"
mv "${file}" "${new_file}"
与
#!/bin/bash
7z x "${file}" && mv "${file}" "${new_file}"
GNU bash,版本 5.1.16(1)-release (x86_64-pc-linux-gnu)
我只是想知道,在 bash 脚本中,新行在功能上是否 100% 等同于&&
?
例如:
#!/bin/bash
7z x "${file}"
mv "${file}" "${new_file}"
与
#!/bin/bash
7z x "${file}" && mv "${file}" "${new_file}"
GNU bash,版本 5.1.16(1)-release (x86_64-pc-linux-gnu)
我的这个字符串在一个文件中出现了 N 次:${FOO}
我必须用这个字符串替换它:${'$'}{BAR}
这是我目前的状态:
sed "s/\\${FOO}/\\${'\\$'}{BAR}/" file.txt
但这引发了“不良替代”。
我怎样才能用sed
(版本4.8)正确实现这一目标?
相关:如何获取当前终端名称?
但我只想要当前终端的 ID,例如tty
返回时/dev/pts/11
我只想打印11
。
我的想法是在我的文件中变量\u
中的用户名之后的 shell 提示符中添加此信息:PS1
.bashrc
想要的结果:username11@localhost:~$
目前我有:
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u$(tty)@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
这使:username/dev/pts/11@localhost:~$
使用sed
or awk
,是否可以在视觉上对齐 CSV 文件中的列?
例如:
例如来自:
a,b,c,some stuff,"some, other, stuff",d,2023-03-10 18:37:00
y,x,z,t,cool,thing,2022-04-12 21:44:00
到:
a, b, c, some stuff,"some, other, stuff", d, 2023-03-10 18:37:00<EOL>
x, y, z, t, cool, thing, 2022-04-12 21:44:00<EOL>
有一些双引号字段包含文本和逗号。
我尝试column
了bsdmainutils
一下,但显然无法处理此类数据。
在基于 Debian 的操作系统上,我如何找出 wxWidgets_LIBRARIES 和 wxWidgets_INCLUDE_DIRS 路径?
实际上,在尝试编译软件时,我实际上遇到了 cmake 的此类错误:
CMake Error at /usr/share/cmake-3.18/Modules/FindPackageHandleStandardArgs.cmake:165 (message):
Could NOT find wxWidgets (missing: wxWidgets_LIBRARIES
wxWidgets_INCLUDE_DIRS)
Call Stack (most recent call first):
/usr/share/cmake-3.18/Modules/FindPackageHandleStandardArgs.cmake:458 (_FPHSA_FAILURE_MESSAGE)
/usr/share/cmake-3.18/Modules/FindwxWidgets.cmake:1008 (find_package_handle_standard_args)
src/pacakge_core/CMakeLists.txt:16 (find_package)
因此,我正在寻找传递给我的 cmake 命令的正确路径。
我在我的 Ubuntu 22.04 笔记本电脑上编译了一个 C++ 软件,并用来checkinstall
安装它。这构建了相应的.deb
文件,这很好。
但现在我想将该.deb
文件安装到基于“Debian GNU/Linux 11 (bullseye)”的 docker 容器中并dpkg
显示此错误:
# dpkg --install mypackage-8.0.0_20221030-1_amd64.deb
dpkg-deb: error: archive 'mypackage-8.0.0_20221030-1_amd64.deb' uses unknown compression for member 'control.tar.zst', giving up
dpkg: error processing archive mypackage-8.0.0_20221030-1_amd64.deb (--install):
dpkg-deb --control subprocess returned error exit status 2
Errors were encountered while processing:
mypackage-8.0.0_20221030-1_amd64.deb
我怎样才能将这个.deb
文件安装到我的容器中而无需再次编译软件(这意味着将所有 C++ 构建工具安装到我的容器中,我想避免这种情况......)?
如果我autossh
在本地机器上启动如下:
autossh -M 20000 -f - nNT -L 192.168.144.1:5433:127.0.0.1:5432 remote-user@remote-server-B
它会在后台自动启动以下ssh
命令,这是我所期望的:
ps aux | grep -i ssh
/usr/lib/autossh/autossh -M 20000 -f - nNT -L 192.168.144.1:5433:127.0.0.1:5432 remote-user@remote-server
/usr/bin/ssh -L 20000:127.0.0.1:20000 -R 20000:127.0.0.1:20001 -N -L 192.168.144.1:5433:127.0.0.1:5432 remote-user@remote-server-B
但在远程服务器 A 上启动的相同命令仅显示ps
:
/usr/lib/autossh/autossh -M 20000 -f - nNT -L 192.168.144.1:5433:127.0.0.1:5432 remote-user@remote-server-B
似乎它没有启动底层ssh
命令。
(请注意,如果我ssh
在远程服务器 A 上手动启动命令,那么我可以正确连接到远程服务器 B。)
我无法弄清楚为什么该命令在远程计算机上的行为与在本地计算机上的行为不同......
这两个autossh
版本都1.4g
在我的本地计算机和远程服务器 A 上。
两台机器都在 Ubuntu 20.04 上。
编辑:
Per Stéphane Chazelas 的评论:在两台不同的远程机器上尝试命令时,我实际上注意到两种略有不同的行为:
1.
$ strace -fe execve autossh -M 20000 -f -nNT -L 192.168.144.1:5433:127.0.0.1:5432 remote-user@remote-server-B
execve("/usr/bin/autossh", ["autossh", "-M", "20000", "-f", "-nNT", "-L", "192.168.144.1:5433:127.0.0.1:5432", "remote-user@remote-server-B"], 0x7fff532c2f68 /* 40 vars */) = 0
strace: Process 23539 attached
[pid 23539] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=23539, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
strace: Process 23540 attached
[pid 23540] execve("/bin/egrep", ["egrep", "-q", "--", "-f?M ?[0-9]+"], 0x55b83714eae8 /* 38 vars */) = 0
[pid 23540] execve("/home/username/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/home/username/.local/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/home/username/anaconda3/condabin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/opt/blenderc/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/usr/local/sbin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/usr/local/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/usr/sbin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/usr/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/sbin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = 0
[pid 23540] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=23540, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
execve("/usr/lib/autossh/autossh", ["/usr/lib/autossh/autossh", "-M", "20000", "-f", "-nNT", "-L", "192.168.144.1:5433:127.0.0.1:5432", "remote-user@remote-server-B"], 0x55b83714f470 /* 38 vars */) = 0
strace: Process 23541 attached
[pid 23538] +++ exited with 0 +++
strace: Process 23542 attached
[pid 23542] execve("/usr/bin/ssh", ["/usr/bin/ssh", "-L", "20000:127.0.0.1:20000", "-R", "20000:127.0.0.1:20001", "-nNT", "-L", "192.168.144.1:5433:127.0.0.1:5432", "remote-user@remote-server-B"], 0x7ffce7ca53e0 /* 38 vars */) = 0
[pid 23542] +++ exited with 255 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=23542, si_uid=1000, si_status=255, si_utime=2, si_stime=0} ---
strace: Process 23543 attached
[pid 23543] execve("/usr/bin/ssh", ["/usr/bin/ssh", "-L", "20000:127.0.0.1:20000", "-R", "20000:127.0.0.1:20001", "-nNT", "-L", "192.168.144.1:5433:127.0.0.1:5432", "remote-user@remote-server-B"], 0x7ffce7ca53e0 /* 38 vars */) = 0
(最后 3 行以固定间隔无限重复,每次使用不同的进程 ID)
2.
$ strace -fe execve autossh -M 20000 -f -nNT -L 192.168.144.1:5433:127.0.0.1:5432 remote-user@remote-server-B
execve("/usr/bin/autossh", ["autossh", "-M", "20000", "-f", "-nNT", "-L", "192.168.144.1:5433:127.0.0.1:5432", "remote-user@remote-server-B"], 0x7fff7be64dc8 /* 34 vars */) = 0
strace: Process 3850 attached
[pid 3850] +++ exited with 0 +++
strace: Process 3851 attached
[pid 3849] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=3850, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
[pid 3851] execve("/bin/egrep", ["egrep", "-q", "--", "-f?M ?[0-9]+"], 0x55690bd42a08 /* 32 vars */) = 0
[pid 3851] execve("/usr/local/sbin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x5637ad7d9958 /* 32 vars */) = -1 ENOENT (No such file or directory)
[pid 3851] execve("/usr/local/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x5637ad7d9958 /* 32 vars */) = -1 ENOENT (No such file or directory)
[pid 3851] execve("/usr/sbin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x5637ad7d9958 /* 32 vars */) = -1 ENOENT (No such file or directory)
[pid 3851] execve("/usr/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x5637ad7d9958 /* 32 vars */) = -1 ENOENT (No such file or directory)
[pid 3851] execve("/sbin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x5637ad7d9958 /* 32 vars */) = -1 ENOENT (No such file or directory)
[pid 3851] execve("/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x5637ad7d9958 /* 32 vars */) = 0
[pid 3851] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=3851, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
execve("/usr/lib/autossh/autossh", ["/usr/lib/autossh/autossh", "-M", "20000", "-f", "-nNT", "-L", "192.168.144.1:5433:127.0.0.1:5432", "remote-user@remote-server-B"], 0x55690bd433a0 /* 32 vars */) = 0
strace: Process 3852 attached
[pid 3849] +++ exited with 0 +++
+++ exited with 1 +++
(然后它立即将控制台返回给用户)
我有一个幼稚的问题,但是通过阅读这个线程,例如:https ://superuser.com/questions/37738/how-to-reliably-keep-an-ssh-tunnel-open
可以在接受的答案中看到如何使用autossh
,例如:
autossh -M 20000 -f -N your_public_server -R 1234:localhost:22 -C
但是从autossh
https://manpages.ubuntu.com/manpages/focal/man1/autossh.1.html的手册页我只能看到 3 个可用选项-M
:-f
和-V
.
在我的系统上同样的事情:
$ autossh --help
/usr/lib/autossh/autossh: invalid option -- '-'
usage: autossh [-V] [-M monitor_port[:echo_port]] [-f] [SSH_OPTIONS]
-M specifies monitor port. Overrides the environment
variable AUTOSSH_PORT. 0 turns monitoring loop off.
Alternatively, a port for an echo service on the remote
machine may be specified. (Normally port 7.)
-f run in background (autossh handles this, and does not
pass it to ssh.)
-V print autossh version and exit.
Environment variables are:
AUTOSSH_GATETIME - how long must an ssh session be established
before we decide it really was established
(in seconds). Default is 30 seconds; use of -f
flag sets this to 0.
AUTOSSH_LOGFILE - file to log to (default is to use the syslog
facility)
AUTOSSH_LOGLEVEL - level of log verbosity
AUTOSSH_MAXLIFETIME - set the maximum time to live (seconds)
AUTOSSH_MAXSTART - max times to restart (default is no limit)
AUTOSSH_MESSAGE - message to append to echo string (max 64 bytes)
AUTOSSH_PATH - path to ssh if not default
AUTOSSH_PIDFILE - write pid to this file
AUTOSSH_POLL - how often to check the connection (seconds)
AUTOSSH_FIRST_POLL - time before first connection check (seconds)
AUTOSSH_PORT - port to use for monitor connection
AUTOSSH_DEBUG - turn logging to maximum verbosity and log to
stderr
和选项从何而来-N
?我没有找对地方吗?-R
-C
问题很简单如下:
watch "psql -d postgresql://user:pass@host:5432/dbname -c 'select id,name from table where name <> 'not available' order by id;'"
这个'not available'
比较必须像Postgres 一样单引号。但是我无法找到正确转义这些单引号的方法,因为 psql 命令(我的意思是select...
)本身已经是单引号,并且也在psql
for watch 调用所需的双引号内。
如何解决这个问题?通过使用或使用多个双/单引号来转义这些单引号,
我几乎看到了所有可能的语法错误。\
我想与团队成员在线分享PostgreSQL数据库(托管在 dockerized Debian Bullseye 上)的一些实时修改watch
(如果您知道任何其他工具,最好是pgAdmin4 ,但我无法找到一种方法观看现场修改,请告诉我)。
目前我正在这样做:
$ PGPASSWORD=***************
$ reset
$ watch -n 1.0 "psql \
-d postgresql://postgres:${PGPASSWORD}@localhost:5432/dbname \
-c 'select id,name,customer,order,product from public.table where id in (1,2,3,4) order by (id)';"
这样密码就不会存储在 bash 历史记录中(请注意第一行前面的空格),并且在调用后它在屏幕上不再可见reset
(主机在 Ubuntu 18.04 上)。
但是当watch
命令启动时,它实际上在屏幕顶部显示了完整连接查询字符串到纯文本的“翻译”,因此显示了数据库密码(或任何其他变量):
Every 1.0s: psql -d postgresql://postgres:mysecretpassword@localhost:5439/dbname -c (...)
...
有隐藏的可能吗?