0

I have written a shell script that outputs a modified time, according to UTC. At present, my if statements to grab and alter the hour are as follows:

if [[ "$emoji" =~ \.☁ ]];
    then
        ((timeShow = time - modValue))
fi

At present, the emoji will be wrapped in format tags for polybar. Namely, the variable will be something like %{F#1a88a4}☁%{F-}. So I am trying to check whether the string contains the ☁ character using the regex above. Unfortunately, this does not appear to work.

1
  • Your code is trying to find a . in a string that doesn't contain a .. Just don't do that.
    – Ed Morton
    Commented Aug 14, 2023 at 17:01

1 Answer 1

0

In the course of searching for a solution, I found that POSIX Basic Regular Expressions have a different syntax to encapsulate the character and search for it within the string.

Thus, the correct code should read (changes highlighted), with thanks to @EdMorton:

if [[ "$emoji" =~ ☁ ]];
    then
        ((timeShow = time - modValue))
fi
7
  • It's unlikely that that is the best code for your problem as your emoji is not a regexp metacharacter and so doesn't have to be enclosed in a bracket expression, may actually be 2 characters and, if so, then so definitely must not be enclosed in a bracket expression, and you probably should be using awk instead of shell for this and the surrounding code anyway.
    – Ed Morton
    Commented Aug 14, 2023 at 17:02
  • Could you explain why that is? The code does function as expected, though I would be happy to improve it. Just for clarity, I have omitted other elif statements from the code block to keep the question brief.
    – twelfth
    Commented Aug 14, 2023 at 17:06
  • Ask a new question that includes a minimal reproducible example with relevant surrounding code (where do those variables get populated/used) and concise, testable sample input and expected output so we can help you further. Right now we can't advise on how to do "it" the right way as we don't know what "it" is you're trying to do.
    – Ed Morton
    Commented Aug 14, 2023 at 17:07
  • Here's an example of how you could be getting the expected output but your code be broken - imagine your emoji is actually 2 characters Control-X and Control-A (it actually looks like M-bM-^XM-^A$ on my screen with cat -A) which when printed one after the other tell your terminal to display a cloud image. If you uses "$emoji" =~ ☁ you're trying to match the whole multi-character string which is good but when you use "$emoji" =~ [☁] you're trying to match any of the characters than make up that string which is bad because then it'd falsely match Control-X on it's own.
    – Ed Morton
    Commented Aug 14, 2023 at 17:16
  • 1
    Hi @EdMorton, thanks very much for this explanation. I see what you mean now. I have just been writing up a new form of this question, but unfortunately I am not able to post for another 90 minutes yet. I will ping you when I do this, however, my code currently reads "$emoji" =~ ☁ as you suggested and continues, so I think that might solve it. I shall post my question nonetheless as it contains further context. Thank you again.
    – twelfth
    Commented Aug 14, 2023 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.