1

Following from my previous question here, please see below a fuller form of my question.

Code use case

The current code is a module for use in polybar, a customisable status bar. As per the first link, polybar is drawn using a configuration file and each bar that is called (by, for instance, running the polybar mybar) reads the configuration file for any given modules. A full example can be seen here. Custom or user defined modules are loaded by calling the relevant script file. For instance:

[module/test]
type = custom/script
exec = some/script/file
tail = true

Polybar accepts, at the very least, shell scripts and python scripts (and perhaps others, though I have not tested this). For the purposes of this question, I am concerned with the appropriateness of my shell script and not its interface with polybar (which currently works as expected; though as @EdMorton explains, this may not be intended functionality, hence this question).

Current code

The following is a minimal working example of my code.

#!/bin/bash

smile=#d1dadf;
sad=#ff5833;
angry=#1a88a4;
laugh=#daf7a6;

while true; do

    # get hour
    time=$(date +%_H -u);

    # set empty hour
    timeShow=TEST;

    # set empty emoji
    emoji=;

    if [ $time -ge 0 ] && [ $time -lt 6 ];
    then
            emoji=%{F$smile}πŸ˜€%{F-}
    elif [ $time -ge 6 ] && [ $time -lt 12 ];
    then
            emoji=%{F$sad}😒%{F-}
    elif [ $time -ge 12 ] && [ $time -lt 18 ];
    then
            emoji=%{F$angry}😑%{F-}
    elif [ $time -ge 18 ] && [ $time -ne 0 ];
    then
            emoji=%{F$laugh}πŸ˜†%{F-}
    fi

    if [[ "$emoji" =~ [😒] ]];
    then
        ((timeShow = time - 1))
    elif [[ "$emoji" =~ [😑] ]];
    then
        ((timeShow = time - 2))
    elif [[ "$emoji" =~ [πŸ˜†] ]];
    then
        ((timeShow = time - 3))
    fi

    echo "$timeShow:$(date '+%M' -u) $emoji"
    
    sleep 1
done

Explanation of inputs (and outputs)

Running the script as is requires no input (the output is calculated from within the script itself). Hence running the script is simply done via:

./script.sh

The output should (and does) return the formatted time, along with the emoji wrapped in colour format tags, as described above.

The program as it stands takes the current UTC hour from date. This value (stored in the variable time) is then modified and stored within timeShow. Assuming a particular range of hours (and hence emoji), the script then modifies timeShow by either subtracting 1, 2, or 3. The emoji are found in the string emoji by performing a regex operation to locate the character, which is wrapped by colour formatting.

Expected output given hardcoded input

Changing the following:

 while true; do... done

To this:

for time in 8 12 22 0; do... done

In addition, the following lines should be changed to match below:

# get hour
#time=$(date +%_H -u);

# set default hour
timeShow=0

Should give the following as expected outputs, assuming the script is run on the hour.

Loop 1 (at 8 am) (offset of -1):

7:00 %{F#ff5833}😒%{F-}

Loop 2 (at 12 pm) (offset of -2):

10:00 %{F#1a88a4}😑%{F-}

Loop 3 (at 10 pm) (offset of -3):

19:00 %{F#daf7a6}πŸ˜†%{F-}

Loop 4 (at 12 am) (offset of 0):

0:00 %{F#d1dadf}πŸ˜€%{F-}

Desired output

The script works as intended. As mentioned above, however, this may not be accidental. The purpose of this question is to understand whether the approach used is producing the correct output, or only happens to do so. The regex at present isolates the character by using the [πŸ˜€]. For any glyph which may comprise 2 or more characters, M-bM-^XM-^A$ for instance, isolating the emoji thusly, [πŸ˜€] is potentially inaccurate as it would match the first corresponding character, leading to undesirable or unexpected behaviour. Further explanation of this problem is provided here.

5
  • Why use pattern matching at all? Why not just set an offset variable in the first if/elif block, then subtract that from time?
    – Barmar
    Commented Aug 14, 2023 at 19:38
  • You don't need the [] around the emoji. [] is used to match any of a set of characters; if there's just one character, =~ [char] is the same as =~ char
    – Barmar
    Commented Aug 14, 2023 at 19:39
  • @Barmar, unfortunately I am not very familiar with how to do this. As I understand it, I would keep the first if elif fi block, and set values into a variable there. Then instead of using regex to check as the expression in the next if elif fi block, I use this new variable and set all the string formatting there?
    – twelfth
    Commented Aug 14, 2023 at 20:06
  • 1
    let itself is non-POSIX syntax bash supports only for backwards compatibility. Use either regular string assignments (foo=$bar) or POSIX arithmetic contexts (foo=$(( bar )). For strings that aren't numeric in nature, neither let nor any other kind of arithmetic context is appropriate at all. Commented Aug 15, 2023 at 12:59
  • @CharlesDuffy Thank you, I will amend my local code accordingly.
    – twelfth
    Commented Aug 15, 2023 at 13:02

2 Answers 2

2

Instead of matching $emoji with a regexp, set another variable when you're setting emoji, and use that when setting timeShow.

#!/bin/bash

smile=#d1dadf;
sad=#ff5833;
angry=#1a88a4;
laugh=#daf7a6;

while true; do

    # get hour
    time=$(date +%_H -u);

    # set empty hour
    timeShow=TEST;
    timeOffset=0

    # set empty emoji
    emoji=;

    if [ $time -ge 0 ] && [ $time -lt 6 ];
    then
            emoji=%{F$smile}πŸ˜€%{F-}
            timeOffset=3
    elif [ $time -ge 6 ] && [ $time -lt 12 ];
    then
            emoji=%{F$sad}😒%{F-}
            timeOffset=1
    elif [ $time -ge 12 ] && [ $time -lt 18 ];
    then
            emoji=%{F$angry}😑%{F-}
            timeOffset=2
    elif [ $time -ge 18 ] && [ $time -ne 0 ];
    then
            emoji=%{F$laugh}πŸ˜†%{F-}
            timeOffset=3
    fi

    timeshow=$((time - timeOffset)

    echo "$timeShow:$(date '+%M' -u) $emoji"
    
    sleep 1 &
    wait
done
1
  • Thank you, this is a brilliant alternative solution; I could not see it at first when reading the comment but it is very clear now.
    – twelfth
    Commented Aug 14, 2023 at 20:15
1

This may be what you want to do:

#!/usr/bin/env bash

emojis[3]=%{F#d1dadf}πŸ˜€%{F-}
emojis[1]=%{F#ff5833}😒%{F-}
emojis[2]=%{F#1a88a4}😑%{F-}
emojis[0]=%{F#daf7a6}πŸ˜†%{F-}

while true; do

    # get hour
    TZ=UTC printf -v hour '%(%k)T' -1

    if   (( hour == 0 )); then delta=0
    elif (( hour <  6 )); then delta=3
    elif (( hour < 12 )); then delta=1
    elif (( hour < 18 )); then delta=2
    else                       delta=0
    fi

    emoji="${emojis[$delta]}"
    hourShow=$(( hour - delta ))
    TZ=UTC printf -v min '%(%M)T' -1

    printf '%d:%02d %s\n' "$hourShow" "$min" "$emoji"

    sleep 1
done

Since you're using bash and arithmetic operations in the assignment contexts there's no reason not to also use them in comparisons, there's no point always testing the previous range end in an if-else, and no reason I can think of to use sleep 1 &; wait instead of just sleep 1.

Here it is tweaked to use the specific hour values the OP provided for testing, done at 2:58pm UTC:

$ cat tst.sh
#!/usr/bin/env bash

emojis[3]=%{F#d1dadf}πŸ˜€%{F-}
emojis[1]=%{F#ff5833}😒%{F-}
emojis[2]=%{F#1a88a4}😑%{F-}
emojis[0]=%{F#daf7a6}πŸ˜†%{F-}

#while true; do
for hour in 8 8 12 22 0; do

    # get hour
    #TZ=UTC printf -v hour '%(%k)T' -1

    if   (( hour == 0 )); then delta=0
    elif (( hour <  6 )); then delta=3
    elif (( hour < 12 )); then delta=1
    elif (( hour < 18 )); then delta=2
    else                       delta=0
    fi

    emoji="${emojis[$delta]}"
    hourShow=$(( hour - delta ))
    TZ=UTC printf -v min '%(%M)T' -1

    printf '%d:%02d %s\n' "$hourShow" "$min" "$emoji"

    sleep 1
done

$ ./tst.sh
7:58 %{F#ff5833}😒%{F-}
7:58 %{F#ff5833}😒%{F-}
10:58 %{F#1a88a4}😑%{F-}
22:58 %{F#daf7a6}πŸ˜†%{F-}
0:58 %{F#daf7a6}πŸ˜†%{F-}

You might want to change each %d to %02d in the printfs so you get 2-digits hour values, e.g. 07:58 instead of 7:58.

1
  • 1
    If you're going to use one bashism (arrays), might think about using another (built-in time formatting). printf -v hour '%(%H)T' -1 is a fair bit faster than spawning date as a subprocess. Commented Aug 15, 2023 at 13:14

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.