UnixReview.com
June 2006
Listing 5: newbi-sh
#! /bin/bash #@ If no name is given on the command line, prompt the user for it if [ -n "$1" ] then builtin=$1 else read -ep "Name of builtin: " builtin [ -z "$builtin" ] && exit 1 fi #@ If a C file for the builtin exists, ask whether to overwrite it if [ -s "$builtin.c" ] then ls -l "$builtin.c" read -sn1 -p "Overwrite $builtin.c [y/N/c]? " ok case $X in [yY]) ;; [cC]) exit 2 ;; * ) ok= ;; esac printf "\n" else ok=1 fi #@ Use template.c as the basis for the new builtin if [ -n "$ok" ] then sed "s/template/$builtin/g" template.c > "$builtin.c" fi #@ Make a copy of the existing makefile cp Makefile mkf || exit 2 { #@ Add the new builtin to Makefile #@ [should add check to see if it's already there] fmt="\t\$(SHOBJ_LD) \$(SHOBJ_LDFLAGS) \$(SHOBJ_XLDFLAGS)" fmt="$fmt -o \$@ %s.o \$(SHOBJ_LIBS)\n" sed "/^all:/ s/\$/ $builtin/" mkf printf "\n%s: %s.o\n" "$builtin" "$builtin" printf "$fmt" "$builtin" printf "\n%s.c: %s.o\n" "$builtin" "$builtin" } > Makefile