Homepage Wiki Forum Buy

Shell Script

From GNUBLIN

Contents

Bash Scripts

Using shell scripts, it is possible for the user to program complex flow processes for the Linux shell.

In the following you see a small Hello World example as a bash script:

hello.sh

#!/bin/bash          
STR="Hello World!"
echo $STR

The call is as follows:

sh hello.sh


For more information please follow these links:

http://www.linux-services.org/shell/
http://www.chemie.fu-berlin.de/chemnet/general/topics/scripts_sh.html

application examples

LED BLINK

In the following example, the red LED flashes

Switch to the directory of the script and start it:

Debian

cd /root/examples/misc/gpio/
./blink.sh

ELDK

cd /home/root/
./blink.sh

Will you have another pin as 'flashing pin', so you have to edit the line

GPIO=3

(outwardly directed GPIOs =>GPIO11, GPIO14, GPIO15)

#!/bin/sh

# Blink the onboard LED

GPIO=3

cleanup() { # Release the GPIO port
  echo $GPIO > /sys/class/gpio/unexport
  exit
}

# Open the GPIO port
#
echo $GPIO > /sys/class/gpio/export 
echo "high" > /sys/class/gpio/gpio$GPIO/direction 

trap cleanup 2 # call cleanup on Ctrl-C

# Blink forever
while [ "1" = "1" ]; do
  echo 1 > /sys/class/gpio/gpio$GPIO/value
  sleep 1
  echo 0 > /sys/class/gpio/gpio$GPIO/value
  sleep 1
done

cleanup # call the cleanup routine


Probe request

In the following example, the state of the pins GPIO_14 is queried.

Simply go to the directory of the script and then run:

ELDK

cd /root/examples/misc/gpio/
./taster.sh

ELDK

cd /home/root/
./taster.sh
#!/bin/bash

# ask for pin state


GPIO=14

cleanup() { # Release the GPIO port
  echo $GPIO > /sys/class/gpio/unexport
  exit
}

# Open the GPIO port
#
echo $GPIO > /sys/class/gpio/export 
echo "in" > /sys/class/gpio/gpio$GPIO/direction 

trap cleanup 2 # call cleanup on Ctrl-C


#ask for gpio state

state=$(cat /sys/class/gpio/gpio$GPIO/value)

if [ "$state" = "1" ]
then
    echo "Taster auf HIGH"
else
    echo "Taster auf LOW"
fi

cleanup


In other languages