You are not logged in.
Pages: 1
Bash builtin shopt doesn't seem to function when called from within an if block:
#!/bin/bash
if [[ "$1" ]]; then
eths=($1)
else
shopt -s extglob
eths=(/sys/class/net/!(lo))
shopt -u extglob
fi
printf "%s\n" "${eths[@]}"
Output:
$ ./wtf
./wtf: line 7: syntax error near unexpected token `('
./wtf: line 7: ` eths=(/sys/class/net/!(lo))'
./wtf: line 9: syntax error near unexpected token `fi'
./wtf: line 9: `fi'
Output is the same if the shopt lines are commented out.
OTOH, when shopt is called from without the block:
#!/bin/bash
shopt -s extglob
if [[ "$1" ]]; then
eths=($1)
else
eths=(/sys/class/net/!(lo))
fi
shopt -u extglob
printf "%s\n" "${eths[@]}"
I get the expected result:
$ ./wtf
/sys/class/net/net0
/sys/class/net/net0b
/sys/class/net/net1
/sys/class/net/net2
/sys/class/net/net3
/sys/class/net/virbr0
/sys/class/net/virbr0-nic
I didn't see anything in bash(1) suggesting this is expected behaviour, and my google-fu is too weak to craft a useful query.
Edit: Maybe vim is trying to tell me something as the syn highlighting is different without
shopt -s extglob
and within
shopt -s extglob
the block?
Edit: To be clear, this is just a small component of a larger script I use to print network details:
[net0b]
199.200.1.110
1000Mb/s
3.38M / 51.83M
[net1]
192.168.1.110
192.168.2.110
1000Mb/s
2.27G / 2.15G
[net2]
192.168.0.110
[net3]
10.157.4.118
100Mb/s
105.10M / 5.47M
[virbr0]
192.168.122.1
Last edited by alphaniner (2016-07-08 18:03:34)
But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner
Offline
From http://mywiki.wooledge.org/glob :
Because the extglob option changes the way certain characters are parsed, it is necessary to have a newline (not just a semicolon) between the shopt command and any subsequent commands that use extended globs. Likewise, you cannot put shopt -s extglob inside a statement block that uses extended globs, because the block as a whole must be parsed when it's defined; the shopt command won't take effect until the block is evaluated, at which point it's too late. In fact as bash parses the entire statement block before evaluating any of it, you need to set extglob outside of the outermost block.
Last edited by edacval (2016-07-08 18:00:22)
Offline
Thanks!
But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner
Offline
Pages: 1