#!/bin/ksh #This shellscript is udp_sender. #Read input from the program named as a commmand line argument. #The first character of the program's name is the program's "port number". #Turn the input input into one "UDP packet" of at most 6 bytes. #The first two bytes will be decimal digits. #The first byte is the length of the packet (at most 6). #The second byte is the port number, taken from the command line argument. #This leaves at most 4 bytes for the data. #Sample use: udp_sender 0_host/0_progname_udp if [[ $# -ne 1 ]] then echo $0: requires program name as command line argument 1>&2 exit 1 fi if [[ ! -x $1 ]] then echo $0: sorry, I have no permission to execute $1 1>&2 exit 2 fi if [[ ! ${1##*/} == [0-9]* ]] then echo $0: program name $1 must begin with a digit 1>&2 exit 3 fi port=`echo ${1##*/} | sed 's/\(.\).*/\1/'` $1 > /tmp/$$ length=`wc -c < /tmp/$$ | awk '{print $1}'` if [[ $length -gt 4 ]] then echo $0: packet can hold only at most 4 bytes of data 1>&2 exit 4 fi #Output the two-byte UDP header. echo $((length + 2))'\c' echo $port'\c' cat /tmp/$$ rm /tmp/$$ exit 0