Can any one help, I'm trying to run a script that beeps out the ip address from the PC internal speaker with the following script. It keeps throwing the error "Syntax error: Bad for loop variable" on line 16. I know its picking up the IP ADDRESS correctly. Any ideas on whats wrong. I'm running this on a ubuntu box. Thanks.
#!/bin/sh
# Beep ip address trough internal pc speaker
# Tested on Debian Linux @NSLU2
# Author: Sebastiaan Giebels, 2007
# Retreive dot-separated ipv4 address from the ifconfig information, using grep and cut:
IPADDRESS=`/sbin/ifconfig | grep -A1 "eth0" | grep "inet" | cut -d: -f 2 |cut -d" " -f1`
# Enable next line for debugging:
#echo $IPADDRESS
#Uncomment this for an example with zeroes in the ip address:
#IPADDRESS="10.0.0.1"
# For all characters in dotted ipv4 address:
for ((a=0; a < ${#IPADDRESS} ; a++))
do
# Get the next character to process:
NUMBER=${IPADDRESS:$a:1}
if [ "." == "$NUMBER" ]
then
# Different note to signal a '.' in the address:
`/usr/bin/beep -l 20 -f 25 -d 20`
else if [[ "$NUMBER" -ge "0" && "$NUMBER" -le "9" ]]
then
# Beep 'NUMBER' times, except for 0 (=beep 10 times):
if [ "$NUMBER" == "0" ]
then
NUMBER=10
fi
`/usr/bin/beep -l 45 -f 15 -d 150 -r $NUMBER`
fi
fi
sleep 1
done
The for loop, at least in BASH, is limited to situations like "for x in (list)" situations. Rather than having your (( a=0; a< blah; a++)), you need to replace it with something that will generate a list of values. Try something like:
Some versions of sh might not work, this version of bash seems to be OK:
#!/usr/bin/env bash
# @(#) s1 Demonstrate bash extended for syntax.
# Uncomment to run script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
# Infrastructure details, environment, commands for forum posts.
set +o nounset
pe() { for i;do printf "%s" "$i";done; printf "%s\n"; }
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe ; pe "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
pe "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p
set -o nounset
pe
pe " Results:"
IPADDRESS="10.0.0.1"
# For all characters in dotted ipv4 address:
for ((a=0; a < ${#IPADDRESS} ; a++))
do
# Get the next character to process:
NUMBER=${IPADDRESS:$a:1}
echo $NUMBER
done
exit 0
producing:
% ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0
GNU bash 3.2.39
Results:
1
0
.
0
.
0
.
1