linux - How do I write a file path which include a regular expression
问题描述
Depending on the system I am working on, there might be 2 different possible paths (mutually exclusive):
System1: /tmp/aword/foo
System2: /tmp/bword/foo
I am supposed to echo something into the foo
file regardless of which system I encounter (through a shell script).
How do I include a regular expression within the path itself, to take the correct (existent) path?
somethings I have tried:
#doesn't work
echo Hello > /tmp/(a|b)word/foo
#doesn't work
echo Hello > /tmp/[a|b]word/foo
is there a way of doing this without having to include a test before this which tests for path existence?
解决方案
如果字面上是aword
,bword
并且您知道其中只有一个存在,则可以使用
echo 'Hello' > /tmp/[ab]word/foo
这是一个 shell 模式,记录在 Bash 手册或POSIX sh 规范中。
但是,如果两条路径都存在,Bash 将抱怨
-bash: [ab]word: ambiguous redirect
推荐阅读
- ruby-on-rails - 如何将响应格式从 fast_jsonapi 格式更改为 AMS gem 响应格式
- azure-cosmosdb-sqlapi - azure cosmos db 的内存数据库兼容是什么
- oracle - 为什么 Oracle 函数 SYS_CONNECT_BY_PATH 在 Jasper Studio 中有效,但在 Jasper Server 上无效?
- json.net - JsonNet.Deserialize
空引用异常 - c# - 尽管文档不存在,为什么 MongoDb 返回文档
- java - 在 Android 中解密文件的 AES-GCM 问题
- sql-server - 用于 SQL Server 备份的 Windows 帐户
- python - 通过使用其他两个 pandas 列的自定义函数填充新的 python pandas 列的最有效方法
- dart - 为什么我们从服务器获取数据时需要使用异步?
- git - 如何在 Jenkins 的拉取请求中获取 git 更改的文件