string - Bash比较两个字符串与字符串中的反斜杠
问题描述
这不是我的确切问题,但为了简单起见,我们开始吧。
如果我运行以下命令,我需要它来返回文件匹配,但由于某种原因,如果我的字符串中包含反斜杠,它总是会失败。
文件 testmonitor.txt 仅包含以下文本
你好世界
#!/bin/bash
file=$(cat /tmp/testmonitor.txt)
if [[ $file == $file ]]; then
echo "files match"
else
echo "files not matching"
fi
解决方案
不加引号,右手边被当作一个模式,该模式 hello\ world
等价于该模式hello world
,与文字文本不匹配hello\ world
。
引用右侧以确保您执行精确的字符串比较而不是模式匹配。
if [[ $file == "$file" ]];
推荐阅读
- php - 使用 fpdi 和 fpdf 库从 phpmailer 发送 pdf union
- javascript - 为什么用 jsonpath-plus 解析 JSON 会抛出 TypeError?
- rust - 找不到正确的生命周期参数来存储加载的字体
- c# - 关于这个 Unity 代码不起作用的任何线索?
- powerbi - 如何删除 Power BI 应用部分中的层次结构
- excel - 试图在另一个范围内查找值范围并在另一个范围内输出
- python - 发生 BytesIO 错误时,如何在 Selenium 中下载图像?
- reactjs - NextJS - 仅为几个特定页面添加提供程序
- asp.net - 如果存在匹配,则在 GridView A 和 B 和 GridView A 中查找匹配日期和自动勾选复选框 - VB.Net
- javascript - JS 等待执行然后放置