You are not logged in.

#1 2008-10-08 10:07:24

JeremyTheWicked
Member
From: Poland
Registered: 2008-05-23
Posts: 193

Please comment on my bash script

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

#2 2008-10-08 10:15:03

genisis300
Member
From: Uk
Registered: 2008-01-15
Posts: 284

Re: Please comment on my bash script

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 smile 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

#3 2008-10-08 10:32:20

JeremyTheWicked
Member
From: Poland
Registered: 2008-05-23
Posts: 193

Re: Please comment on my bash script

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

#4 2008-10-08 10:47:28

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Please comment on my bash script

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

#5 2008-10-08 11:20:05

JeremyTheWicked
Member
From: Poland
Registered: 2008-05-23
Posts: 193

Re: Please comment on my bash script

what does "=~" do?


arch(3) adj amused because you think you understand something better than other people ;P

Offline

#6 2008-10-08 11:29:05

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Please comment on my bash script

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

#7 2008-10-08 11:37:57

JeremyTheWicked
Member
From: Poland
Registered: 2008-05-23
Posts: 193

Re: Please comment on my bash script

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

#8 2008-10-08 12:42:26

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Please comment on my bash script

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

#9 2008-10-08 12:58:03

JeremyTheWicked
Member
From: Poland
Registered: 2008-05-23
Posts: 193

Re: Please comment on my bash script

Thanks.

I must dive into bash scripting sometime...


arch(3) adj amused because you think you understand something better than other people ;P

Offline

#10 2008-10-08 21:45:11

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,224
Website

Re: Please comment on my bash script

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

Offline

Board footer

Powered by FluxBB