You are not logged in.
Pages: 1
Hi guys.
This is the first bash script I wrote (well - beyond really basic ones).
I run it from ~/.config/openbox/autostart.sh and it is supposed to check whether I'm online and if I am - it starts several internet dependent apps I use. It works great, I would like to have your input though on how I could've done it better/differently.
Here it comes:
#!/bin/bash
STATE_IS="offline"
exec 10</sys/class/net/wlan0/operstate
while read LINE <&10; do
if [ $LINE = "up" ]; then
STATE_IS="online"
fi
done
exec 10</sys/class/net/eth0/operstate
while read LINE <&10; do
if [ $LINE = "up" ]; then
STATE_IS="online"
fi
done
if [ $STATE_IS = "online" ]; then
thunderbird &
liferea &
kadu &
skype &
transmission &
firefox3-bin &
fi
arch(3) adj amused because you think you understand something better than other people ;P
Offline
Do you really need it to check for the wireless and Ethernet? will that not cuase problems if they are both active.
Good idea thought i might use that my self for a few things.
"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson
Offline
They are never both active - I only use a cable on rare occasion when there's no wireless network around.
arch(3) adj amused because you think you understand something better than other people ;P
Offline
Interesting use of 'exec n<'. I had to read http://tldp.org/LDP/abs/html/io-redirection.html
Simplest I can think of is this
if [[ $(cat /sys/class/net/{eth0,wlan0}/operstate) =~ up ]]; then
firefox &
fi
Offline
what does "=~" do?
arch(3) adj amused because you think you understand something better than other people ;P
Offline
It matches a regex
i.e. you could use this to make sure it's not in a word
if [[ $(cat /sys/class/net/{eth0,wlan0}/operstate) =~ [^a-z]up[^a-z] ]]; then
firefox &
fi
edit: or ... =~ (up|unknown)
Mine seems to be unknown.
Last edited by Procyon (2008-10-08 11:34:05)
Offline
Interesting. I don't know too much about bash... Even the double brackets are a little bit of mystery to me. I tried to read about this but never really got to understand the difference between the double brackets and single brackets in an "if" statement.
arch(3) adj amused because you think you understand something better than other people ;P
Offline
Double brackets allow more advanced ==, != and =~
[[ foobar == *oo* ]] && echo yes
while single brackets do not. (they expand the * to filenames)
Some syntax is different, but you get an error message about it anyway. Like you can use && inside the brackets, but not -a.
[ p = p -a a = a ] && echo yes
[[ p = p && a = a ]] && echo yes
Offline
Thanks.
I must dive into bash scripting sometime...
arch(3) adj amused because you think you understand something better than other people ;P
Offline
Something like this would achieve the same result in a lot less?
#!/bin/bash
STATE_IS="offline"
/bin/egrep -q '^up$' /sys/class/net/wlan0/operstate && STATE_IS='online'
/bin/egrep -q '^up$' /sys/class/net/eth0/operstate && STATE_IS='online'
if [ $STATE_IS = "online" ]; then
thunderbird &
liferea &
kadu &
skype &
transmission &
firefox3-bin &
fi
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline
Pages: 1