Question

I have been trying to find a working regex to filter out only variables in a shell script but not been able to. I am not talking about the the actual variable but the string that creates it. As an example I have been trying to egrep the variables out of this script.

#!/bin/bash

flavor=${1:-dzen2}

function gmail() {
while true
do
    inbox_count=$(wget -q -O- --auth-no-challenge --user=something@gmail.com --password=something 'https://mail.goog$
    echo "Gg:$inbox_count"
    sleep 60
done
}

function weather() {
while true
do
    weather_stat=$($HOME/.config/i3/./weather.sh)
    echo "F$weather_stat"
    sleep 900
done
}

function time_date() {
while true
do
    klocka=$(date +'%A den %d %B v%V  %H:%M')
    echo "S$klocka"
    sleep 10
done < <(echo)
}

bspc control --put-status
xtitle -sf "T%s" > "$PANEL_FIFO" &
weather > "$PANEL_FIFO" &
gmail > "$PANEL_FIFO" &
time_date > "$PANEL_FIFO" &
case "$flavor" in
bar)
    cat "$PANEL_FIFO" | .config/bspwm/panel_bar | bar
;;
dzen2)
    . .config/bspwm/panel_colors
    PANEL_HEIGHT=14
    FONT_FAMILY='Dejavu Sans'
    FONT_SIZE=9
    cat "$PANEL_FIFO" | .config/bspwm/panel_dzen2 -f "$FONT_FAMILY" -s "$FONT_SIZE" | \
    dzen2 -h $PANEL_HEIGHT -dock -ta l -title-name panel -fn "${FONT_FAMILY}:pixelsize=${FONT_SIZE}" \
    -fg "$COLOR_FOREGROUND" -bg "$COLOR_BACKGROUND"
;;
esac

I need this for my Nano colouring theme. So far I have been using "^[A-Za-z0-9 ].*=" which get some but not all of the variables. And if I do something like "[A-Za-z0-9 ].*=" it takes to much. I think it might be that those other variables like klocka= start with tabulation.. but haven't been able to successfully solve it.

I want it to filter out the variable before the equal sign. Like this FONT_SIZE= so I can theme that part in Nano. I need the regex and not a sed or awk as that is the syntax in Nano.

Was it helpful?

Solution

Well, a simple regex like this seems to work for me:

^\s*(\w*)=

It captures the variable name. Note that as soon as you have scripts which define multiple variables on the same line, this won't work.

Example: http://www.regex101.com/r/sE8zN8

OTHER TIPS

Try this:

^\s*[A-Za-z_][A-Za-z0-9_]*=

This allows for arbitrary whitespace at the beginning of the line, followed by a valid identifier (letter or underscore followed by any number of letters, numbers, and underscores), followed immediately by an equal sign.

This will still let through certain command invocations that prefix a command with a temporary environment modification, but block those is beyond the scope of an easily written regular expression:

FOO=3 BAR=5 myCommand arg1 arg2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top