我需要检查一个目录(我们称之为dir
)是否包含两个文件之一(我们称之为filea
和fileb
),但既不包含一个文件,也不包含两个文件。
理想的解决方案是在谓词之间使用 XOR 运算:
if [ -f dir/filea ] ^ [ -f dir/fileb]
then
echo Structure ok
# do stuff
fi
Run Code Online (Sandbox Code Playgroud)
然而,shell 不支持^
作为 XOR 运算符,并且该[
命令没有选项-X
或--xor
像它那样具有-a
and -o
...使用否定相等也不起作用:
if ! [ -f dir/filea -eq -f dir/fileb ]
# or
if ! [ -f dir/filea = -f dir/fileb ]
Run Code Online (Sandbox Code Playgroud)
有没有什么方法可以实现这一点,而不需要求助于像这样的成熟的 AND/OR 表达式
if { [ -f dir/filea ] || [ -f dir/fileb ]; } && ! …
Run Code Online (Sandbox Code Playgroud) 我在Xubuntu(16.04,顺便说一句)但是lsb_release -a
给了我:
user@host:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04
Codename: xenial
Run Code Online (Sandbox Code Playgroud)
它应该显示Xubuntu
,还是合法的输出?
如果是,我如何(通过 CLI)知道我使用的确切 Ubuntu 发行版?
No LSB modules are available.
无论如何,警告是什么意思?
(它输出到stderr:
user@host:~$ lsb_release -a >/dev/null
No LSB modules are available.
Run Code Online (Sandbox Code Playgroud)
所以我猜在名义上它不应该在那里)
我在这里读到这chmod -R 777 /
是一个非常糟糕的主意,因为它会覆盖文件的权限,并删除粘性位setgid
和其他内容。
但是,我认为这chmod -R ugo+rwx /
不会覆盖权限而是添加它们,如果没有的话,它会比前面提到的命令安全得多。
我对吗?或者这些命令基本相同并且都会破坏我的系统?
我不打算这样做,只是要求一般文化。
在 Python 3 中,有没有办法找出系统使用的语言?
即使是一个棘手的一个,虽然,这样的:从文件中读取偷偷摸摸目录,并找到字符串'ENG'
或'FRE'
文件的内容中...
我最近进入了 nautilus 脚本,对于我正在编写的脚本,我需要从文件名中提取一个子字符串。我的问题是我找到了大量的方法来根据字符的位置提取子字符串,而不是关于如何在我的字符串中查找给定字符并从该字符提取子字符串或从该字符提取子字符串的方法。
cut -f1 -d "delimiter"
Run Code Online (Sandbox Code Playgroud)
有效,但cut
只接受 1 个字符的分隔符。
也许awk
或expr
?
编辑:
我正在用 bash 编写,例如我希望有一个名称为的文件
Any.Series.S01E01.VOSTFR.@whatever.com.avi
Run Code Online (Sandbox Code Playgroud)
简单地重命名为
Any Series S01 E01 VOSTFR.avi
Run Code Online (Sandbox Code Playgroud) chmod ×1
command-line ×1
filenames ×1
locale ×1
lsb-release ×1
permissions ×1
python3 ×1
shell ×1
shell-script ×1
string ×1
test ×1
xubuntu ×1