shell脚本里查找字符
时间: 2023-07-29 16:10:14 浏览: 117
在shell脚本中查找字符可以使用grep命令。可以使用以下方法来查找字符:
方法一:利用grep命令查找
```shell
strA="long string"
strB="string"
result=$(echo $strA | grep "${strB}")
if \[\[ "$result" != "" \]\]
then
echo "包含"
else
echo "不包含"
fi
```
以上脚本中,我们定义了两个变量strA和strB,然后使用grep命令在strA中查找strB。如果找到了匹配的字符,则输出"包含",否则输出"不包含"。\[2\]
希望这个回答对您有帮助。
#### 引用[.reference_title]
- *1* *3* [(11)Shell脚本【字符串查找】](https://blog.csdn.net/m0_46498597/article/details/128424852)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [linux shell 字符串查找](https://blog.csdn.net/whatday/article/details/105466652)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文