[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Brace expansions in if statement
From: |
João Eiras |
Subject: |
Re: [Help-bash] Brace expansions in if statement |
Date: |
Fri, 30 Mar 2018 00:40:19 +0200 |
>> Excerpts from Felipe Salvador's message of 2018-03-29 20:12:21 +0200:
>> > Hi list,
>> > I'm trying to make bash to expand a brace expansion in a if statement.
>> >
>> > So, I've
>> >
>> > if [ "$GTTY" = tty{0..10} ]; then
>> > msger=echo
>> > else
>> > msger=notify-send
>> > fi
>> >
a) Use echo as a helper to expand inside expressions
b) bash has substring matching
c) mind the spaces
if [[ "$(echo " "tty{0..10}" ")" == *" $GTTY "* ]]; then
msger=echo
else
msger=notify-send
fi
But I'd simplify your expression to
if tty | cut -d'/' -f3 | grep -q '^tty[0-9]\+$'; then
# ...
fi
>> > where $GTTY is $(tty | cut -d'/' -f3).
I'm on ubuntu, which tells me
$ tty
/dev/pts/2
$ tty | cut -d'/' -f3
pts
So I'm not entirely sure your approach is inter-operable, in case
that's an issue for you.