bash search for a pattern within a string variable
Does anyone know a bash command to check for a pattern match within a string. For example, if I have a string "1 2 3 4 5". I need a function that would return true if I searched for "2 3", but false if I searched for "8 9".
Thanks PS I tried searching this forum, but the search kept locking up, so sorry if this has been answered already. |
Hi,
I don't know any bash internal that can do this, but the following code snippet is one way of solving this (there are probably a lot more): Code:
#!/bin/bash |
There are built-in tests for extended regular expressions (similar to what druuna uses) or Bash patterns:
Code:
#! /bin/bash |
Thanks I've got it working now
|
Needs tweaking
It works more like this:
Code:
kovacsbv@leviticus:~$ cat tmp.sh |
To get #1 matching remove quotes around RE or set compat31
Code:
shopt -s compat31 |
Actually 'case' is much much faster than even simple matching using 'if', and
can even do pattern and glob matching: Code:
#!/bin/sh |
Thanks brother !
Quote:
|
Hidden Files don't work
All,
I'm trying to read files and directories in a hidden directory. EX: Working with ThunderBird EMail client where it resides in: /home/user/.thunderbird The 2 things in this directory are: DIR==> encryptedname.default FILE=> profile.ini I either need to pull this directory name directly or read it from the file where file contents are something like: ********************************************************* [General] StartWithLastProfile=1 [Profile0] Name=default IsRelative=1 Path=encryptedname.default ********************************************************* Anyway always get error: Invalid file or directory when trying to read either. Tried cat, grep, find, etc. always get the same error. Could use some help around this roadblock! Cheers! OMR |
All times are GMT -5. The time now is 09:25 AM. |