07-May-24
Shell
Check if script is running with privileges and warn if it isn't. This works
with ksh
and bash
but not sh
..
:-(
#!/usr/bin/oksh
if [[ usr=$(id -u) -ne '0' ]]; then
echo -e $USER "sir, you need to run" \"$0\" "as root or with sudo! \n"
exit 1
else
echo "you are root."
# run commands here
fi
Books
I have ordered a well used and dog eared copy of
Unix Shell Programming (3rd Edition)
from Amazon. Looking forward to reading it.
03-May-24
Shell
Today I learned about the timeout
command after adapting my
pinglan
script to work on OpenBSD. The problem here is that the
-w
(maxwait) option of OpenBSD's version of ping only
supports seconds. This caused the script to take over 8 minutes to ping all
possible hosts on my LAN.
So now with timeout
inserted into the main loop of the script:
timeout $TIMEOUT ping -c 1 $ip > /dev/null
With $TIMEOUT
configured to 0.2
the script now
takes just 52 seconds to ping all possible hosts up and return the results.
02-May-24
IRC
> Zoomers have always existed.
> typeset is a shell builtin that is POSIX, but Bash replaced typeset with
declare, but then set typeset as an alias. So POSIX scripts will work in Bash,
but a Bash script using declare not typeset, will not work in POSIX shell.
> You have to be an absolute c^_^t to do something like this.
Shell
Changed my default shell to match OpenBSD and to avoid bashisms whilst I'm
learning to write scripts.
$ chsh void
Changing shell for void.
Password:
New shell [/bin/bash]: /bin/ksh
Shell changed.
$_
Shell script
A script to find IP addresses of hosts on my network
#!/usr/bin/bash
# Find hosts on LAN
# Edit variables to suit your needs
###################################
NETWORK="192.168.1" #
SECS="0.2" #
###################################
typeset -i i
echo -e "\nPinging addresses 1 to 255\n"
for (( i = 1; i < 255; i++ )); do
ip=$NETWORK.$i
ping -c 1 -W $SECS $ip >/dev/null
if [ $? == '0' ]; then
echo $ip exists
fi
done