Imported from ../bash-2.04.tar.gz.

This commit is contained in:
Jari Aalto 2000-03-17 21:46:59 +00:00
commit bb70624e96
387 changed files with 28522 additions and 9334 deletions

View file

@ -0,0 +1,485 @@
#
# Completion examples
#
#
# This encapsulates the default bash completion code
# call with the word to be completed as $1
#
# Since programmable completion does not use the bash default completions
# or the readline default of filename completion when the compspec does
# not generate any matches, this may be used as a `last resort' in a
# completion function to mimic the default bash completion behavior.
#
_bash_def_completion ()
{
local h t
COMPREPLY=()
# command substitution
if [[ "$1" == \$\(* ]]; then
t=${1#??}
COMPREPLY=( $(compgen -c -P '$(' $t) )
fi
# variables with a leading `${'
if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == \$\{* ]]; then
t=${1#??}
COMPREPLY=( $(compgen -v -P '${' -S '}' $t) )
fi
# variables with a leading `$'
if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == \$* ]]; then
t=${1#?}
COMPREPLY=( $(compgen -v -P '$' $t ) )
fi
# username expansion
if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == ~* ]] && [[ "$1" != */* ]]; then
t=${1#?}
COMPREPLY=( $( compgen -u -P '~' $t ) )
fi
# hostname
if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == *@* ]]; then
h=${1%%@*}
t=${1#*@}
COMPREPLY=( $( compgen -A hostname -P "${h}@" $t ) )
fi
# glob pattern
if [ ${#COMPREPLY[@]} -eq 0 ]; then
# sh-style glob pattern
if [[ $1 == *[*?[]* ]]; then
COMPREPLY=( $( compgen -G "$1" ) )
# ksh-style extended glob pattern - must be complete
elif shopt -q extglob && [[ $1 == *[?*+\!@]\(*\)* ]]; then
COMPREPLY=( $( compgen -G "$1" ) )
fi
fi
# final default is filename completion
if [ ${#COMPREPLY[@]} -eq 0 ]; then
COMPREPLY=( $(compgen -f "$1" ) )
fi
}
#
# Return 1 if $1 appears to contain a redirection operator. Handles backslash
# quoting (barely).
#
_redir_op()
{
case "$1" in
*\\'[\<\>]'*) return 1;;
*[\<\>]*) return 0;;
*) return 1;;
esac
}
# _redir_test tests the current word ($1) and the previous word ($2) for
# redirection operators and does filename completion on the current word
# if either one contains a redirection operator
_redir_test()
{
if _redir_op "$1" ; then
COMPREPLY=( $( compgen -f "$1" ) )
return 0
elif _redir_op "$2" ; then
COMPREPLY=( $( compgen -f "$1" ) )
return 0
fi
return 1
}
# optional, but without this you can't use extended glob patterns
shopt -s extglob
#
# Easy ones for the shell builtins
#
# nothing for: alias, break, continue, dirs, echo, eval, exit, getopts,
# let, logout, popd, printf, pwd, return, shift, suspend, test, times,
# umask
#
complete -f -- . source
complete -A enabled builtin
complete -d cd
# this isn't exactly right yet -- needs to skip shell functions and
# do $PATH lookup (or do compgen -c and filter out matches that also
# appear in compgen -A function)
complete -c command
# could add -S '=', but that currently screws up because readline appends
# a space unconditionally
complete -v export local readonly
complete -A helptopic help # currently same as builtins
complete -d pushd
complete -A shopt shopt
complete -c type
complete -a unalias
complete -v unset
#
# Job control builtins: fg, bg, disown, kill, wait
# kill not done yet
#
complete -A stopped -P '%' bg
complete -j -P '%' fg jobs disown
# this is not quite right at this point
_wait_func ()
{
local cur
cur=${COMP_WORDS[COMP_CWORD]}
case "$cur" in
%*) COMPREPLY=( $(compgen -A running -P '%' ${cur#?} ) ) ;;
[0-9]*) COMPREPLY=( $(jobs -p | grep ^${cur}) ) ;;
*) COMPREPLY=( $(compgen -A running -P '%') $(jobs -p) )
;;
esac
}
complete -F _wait_func wait
#
# more complicated things, several as yet unimplemented
#
#complete -F _bind_func bind
_declare_func()
{
local cur prev nflag opts
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
COMPREPLY=()
if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
COMPREPLY=(-a -f -F -i -r -x -p)
return 0;
fi
if [[ $cur == '+' ]]; then
COMPREPLY=(+i +x)
return 0;
fi
if [[ $prev == '-p' ]]; then
COMPREPLY=( $(compgen -v $cur) )
return 0;
fi
return 1
}
complete -F _declare_func declare typeset
_enable_func()
{
local cur prev nflag opts
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
COMPREPLY=()
if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
COMPREPLY=(-a -d -f -n -p -s)
return 0;
fi
if [[ $prev == '-f' ]]; then
COMPREPLY=( $( compgen -f $cur ) )
return 0;
fi
for opts in "${COMP_WORDS[@]}" ; do
if [[ $opts == -*n* ]]; then nflag=1; fi
done
if [ -z "$nflag" ] ; then
COMPREPLY=( $( compgen -A enabled $cur ) )
else
COMPREPLY=( $( compgen -A disabled $cur ) )
fi
return 0;
}
complete -F _enable_func enable
_exec_func()
{
local cur prev
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
COMPREPLY=(-a -c -l)
return 0;
fi
if [[ $prev != -*a* ]]; then
COMPREPLY=( $( compgen -c $cur ) )
return 0
fi
return 1;
}
complete -F _exec_func exec
_fc_func()
{
local cur prev
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
COMPREPLY=(-e -n -l -r -s)
return 0;
fi
if [[ $prev == -*e ]]; then
COMPREPLY=( $(compgen -c $cur) )
return 0
fi
return 1
}
complete -F _fc_func fc
_hash_func()
{
local cur prev
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
COMPREPLY=(-p -r)
return 0;
fi
if [[ $prev == '-p' ]]; then
COMPREPLY=( $( compgen -f $cur ) )
return 0;
fi
COMPREPLY=( $( compgen -c $cur ) )
return 0
}
complete -F _hash_func hash
_history_func()
{
local cur prev
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
COMPREPLY=()
if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
COMPREPLY=(-a -c -d -n -r -w -p -s)
return 0;
fi
if [[ $prev == -[anrw] ]]; then
COMPREPLY=( $( compgen -f $cur ) )
fi
return 0
}
complete -F _history_func history
#complete -F _read_func read
_set_func ()
{
local cur prev
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
COMPREPLY=()
_redir_test "$cur" "$prev" && return 0;
if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
COMPREPLY=(-a -b -e -f -k -m -n -o -p -t -u -v -x -B -C -H -P --)
return 0;
fi
if [[ $cur == '+' ]]; then
COMPREPLY=(+a +b +e +f +k +m +n +o +p +t +u +v +x +B +C +H +P)
return 0;
fi
if [[ $prev == [+-]o ]]; then
COMPREPLY=( $(compgen -A setopt $cur) )
return 0;
fi
return 1;
}
complete -F _set_func set
_trap_func ()
{
local cur
cur=${COMP_WORDS[COMP_CWORD]}
if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
COMPREPLY=(-l -p)
return 0;
fi
COMPREPLY=( $( compgen -A signal ${cur}) )
return 0
}
complete -F _trap_func trap
#
# meta-completion (completion for complete/compgen)
#
_complete_meta_func()
{
local cur prev cmd
COMPREPLY=()
cmd=$1
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
_redir_test "$cur" "$prev" && return 0;
if (( $COMP_CWORD <= 1 )) || [[ "$cur" == '-' ]]; then
case "$cmd" in
complete) COMPREPLY=(-a -b -c -d -e -f -j -k -v -u -r -p -A -G -W -P -S -X -F -C);;
compgen) COMPREPLY=(-a -b -c -d -e -f -j -k -v -u -A -G -W -P -S -X -F -C);;
esac
return 0
fi
if [[ $prev == -A ]]; then
COMPREPLY=(alias arrayvar binding builtin command directory \
disabled enabled export file function helptopic hostname job keyword \
running setopt shopt signal stopped variable)
return 0
elif [[ $prev == -F ]]; then
COMPREPLY=( $( compgen -A function $cur ) )
elif [[ $prev == -C ]]; then
COMPREPLY=( $( compgen -c $cur ) )
else
COMPREPLY=( $( compgen -c $cur ) )
fi
return 0
}
complete -F _complete_meta_func complete compgen
#
# some completions for shell reserved words
#
#complete -c -k time do if then else elif '{'
#
# external commands
#
complete -e printenv
complete -c nohup exec nice eval trace truss strace sotruss gdb
_make_targets ()
{
local mdef makef gcmd cur prev i
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
# if prev argument is -f, return possible filename completions.
# we could be a little smarter here and return matches against
# `makefile Makefile *.mk', whatever exists
case "$prev" in
-*f) COMPREPLY=( $(compgen -f $cur ) ); return 0;;
esac
# if we want an option, return the possible posix options
case "$cur" in
-) COMPREPLY=(-e -f -i -k -n -p -q -r -S -s -t); return 0;;
esac
# make reads `makefile' before `Makefile'
if [ -f makefile ]; then
mdef=makefile
elif [ -f Makefile ]; then
mdef=Makefile
else
mdef=*.mk # local convention
fi
# before we scan for targets, see if a makefile name was specified
# with -f
for (( i=0; i < ${#COMP_WORDS[@]}; i++ )); do
if [[ ${COMP_WORDS[i]} == -*f ]]; then
eval makef=${COMP_WORDS[i+1]} # eval for tilde expansion
break
fi
done
[ -z "$makef" ] && makef=$mdef
# if we have a partial word to complete, restrict completions to
# matches of that word
if [ -n "$2" ]; then gcmd='grep "^$2"' ; else gcmd=cat ; fi
# if we don't want to use *.mk, we can take out the cat and use
# test -f $makef and input redirection
COMPREPLY=( $(cat $makef 2>/dev/null | awk 'BEGIN {FS=":"} /^[^.# ][^=]*:/ {print $1}' | tr -s ' ' '\012' | sort -u | eval $gcmd ) )
}
complete -F _make_targets -X '+($*|*.[cho])' make gmake pmake
_umount_func ()
{
COMPREPLY=( $(mount | awk '{print $1}') )
}
complete -F _umount_func umount
_configure_func ()
{
case "$2" in
-*) ;;
*) return ;;
esac
case "$1" in
\~*) eval cmd=$1 ;;
*) cmd="$1" ;;
esac
COMPREPLY=( $("$cmd" --help | awk '{if ($1 ~ /--.*/) print $1}' | grep ^"$2" | sort -u) )
}
complete -F _configure_func configure
complete -W '"${GROUPS[@]}"' newgrp
complete -f chown ln more cat
complete -d mkdir rmdir
complete -f strip
complete -f -X '*.gz' gzip
complete -f -X '*.Z' compress
complete -f -X '!*.+(gz|tgz|Gz)' gunzip gzcat zcat zmore
complete -f -X '!*.Z' uncompress zmore zcat
complete -f -X '!*.+(gif|jpg|jpeg|GIF|JPG|bmp)' xv
complete -f -X '!*.pl' perl perl5
complete -A hostname rsh telnet rlogin ftp ping xping host traceroute nslookup
complete -A hostname rxterm rxterm3 rxvt2
complete -u su
complete -f -X '!*.+(ps|PS)' gs gv ghostview psselect pswrap
complete -f -X '!*.+(dvi|DVI)' dvips xdvi dviselect dvitype
complete -f -X '!*.+(pdf|PDF)' acroread
complete -f -X '!*.texi*' makeinfo texi2dvi texi2html
complete -f -X '!*.+(tex|TEX)' tex latex slitex
#
# other possibilities, left as exercises
#
#complete -F _find_func find
#complete -F _man_func man
#complete -F _stty_func stty

View file

@ -0,0 +1,103 @@
# usage: reverse arrayname
reverse()
{
local -a R
local -i i
local rlen temp
# make r a copy of the array whose name is passed as an arg
eval R=\( \"\$\{$1\[@\]\}\" \)
# reverse R
rlen=${#R[@]}
for ((i=0; i < rlen/2; i++ ))
do
temp=${R[i]}
R[i]=${R[rlen-i-1]}
R[rlen-i-1]=$temp
done
# and assign R back to array whose name is passed as an arg
eval $1=\( \"\$\{R\[@\]\}\" \)
}
A=(1 2 3 4 5 6 7)
echo "${A[@]}"
reverse A
echo "${A[@]}"
reverse A
echo "${A[@]}"
# unset last element of A
alen=${#A[@]}
unset A[$alen-1]
echo "${A[@]}"
# ashift -- like shift, but for arrays
ashift()
{
local -a R
local n
case $# in
1) n=1 ;;
2) n=$2 ;;
*) echo "$FUNCNAME: usage: $FUNCNAME array [count]" >&2
exit 2;;
esac
# make r a copy of the array whose name is passed as an arg
eval R=\( \"\$\{$1\[@\]\}\" \)
# shift R
R=( "${R[@]:$n}" )
# and assign R back to array whose name is passed as an arg
eval $1=\( \"\$\{R\[@\]\}\" \)
}
ashift A 2
echo "${A[@]}"
ashift A
echo "${A[@]}"
ashift A 7
echo "${A[@]}"
# Sort the members of the array whose name is passed as the first non-option
# arg. If -u is the first arg, remove duplicate array members.
array_sort()
{
local -a R
local u
case "$1" in
-u) u=-u ; shift ;;
esac
if [ $# -eq 0 ]; then
echo "array_sort: argument expected" >&2
return 1
fi
# make r a copy of the array whose name is passed as an arg
eval R=\( \"\$\{$1\[@\]\}\" \)
# sort R
R=( $( printf "%s\n" "${A[@]}" | sort $u) )
# and assign R back to array whose name is passed as an arg
eval $1=\( \"\$\{R\[@\]\}\" \)
return 0
}
A=(3 1 4 1 5 9 2 6 5 3 2)
array_sort A
echo "${A[@]}"
A=(3 1 4 1 5 9 2 6 5 3 2)
array_sort -u A
echo "${A[@]}"

View file

@ -11,7 +11,7 @@
# BASH NOTE: make sure you have executed `shopt -s extglob' before
# trying to use this function, or it will not work
function isnum # string
isnum() # string
{
case $1 in
?([-+])+([0-9])?(.)*([0-9])?([Ee]?([-+])+([0-9])) )
@ -21,3 +21,32 @@ function isnum # string
*) return 1;;
esac
}
isnum2() # string
{
case $1 in
?([-+])+([[:digit:]])?(.)*([[:digit:]])?([Ee]?([-+])+([[:digit:]])) )
return 0;;
?([-+])*([[:digit:]])?(.)+([[:digit:]])?([Ee]?([-+])+([[:digit:]])) )
return 0;;
*) return 1;;
esac
}
isint() # string
{
case $1 in
?([-+])+([0-9]) )
return 0;;
*) return 1;;
esac
}
isint2() # string
{
case $1 in
?([-+])+([[:digit:]]) )
return 0;;
*) return 1;;
esac
}

View file

@ -0,0 +1,40 @@
#
# replacements for test/[ that do arithmetic expansion on the operands to
# the arithmetic operators, like ksh.
#
function test()
{
local -i n1 n3
case "$#" in
3) case "$2" in
-lt|-gt|-eq|-ne|-le|-ge) n1=$(( $1 ))
n3=$(( $3 ))
builtin test "$n1" $2 "$n3"
return $?;;
*) builtin test "$@" ;;
esac;;
*) builtin test "$@" ;;
esac
}
function [()
{
local -i n1 n3
case "$#" in
4) case "$2" in
-lt|-gt|-eq|-ne|-le|-ge) n1=$(( $1 ))
n3=$(( $3 ))
builtin [ "$n1" $2 "$n3" ]
return $?;;
*) builtin [ "$@" ;;
esac;;
*) builtin [ "$@" ;;
esac
}
q=7
[ q -lt 10 ]
echo $?
[ $q -lt 10 ]
echo $?

View file

@ -10,12 +10,13 @@
# are others, but we already have substitutes for them: "history", "type",
# and "hash".
#
alias r="fc -e -"
alias r="fc -s"
alias functions="typeset -f"
alias integer="typeset -i"
alias nohup="nohup "
alias true=":"
alias false="let 0"
alias command="command "
alias stop="kill -s STOP"
alias redirect="command exec"
alias hist="fc"
#
@ -29,38 +30,65 @@ alias hist="fc"
whence()
{
local vflag
local vflag pflag fflag defarg c
local path
vflag=
vflag= aflag= pflag= fflag=
path=
if [ "$#" = "0" ] ; then
echo "whence: argument expected"
return 1
echo "whence: usage: whence [-afpv] name..." >&2
return 2
fi
case "$1" in
-v) vflag=1 ; shift 1 ;;
-*) echo "whence: bad option: $1" ; return 1 ;;
*) ;;
esac
OPTIND=1
while getopts "avfp" c
do
case "$c" in
a) defarg=-a ;;
f) fflag=1 ;; # no-op
p) pflag=1 ;;
v) vflag=1 ;;
?) echo "whence: $1: unknown option" >&2
echo "whence: usage: whence [-afpv] name..." >&2
return 2 ;;
esac
done
shift $(( $OPTIND - 1 ))
if [ "$#" = "0" ] ; then
echo "whence: bad argument count"
return 1
echo "whence: usage: whence [-afpv] name..." >&2
return 2
fi
for cmd
do
if [ "$vflag" ] ; then
echo $(builtin type $cmd | sed 1q)
if [ -z "$defarg" ]; then
builtin type $cmd | sed 1q
else
if builtin type $defarg -t $cmd | grep 'function$' >/dev/null 2>&1; then
# HAIRY awk script to suppress
# printing of function body -- could
# do it with sed, but I don't have
# that kind of time
builtin type $defarg $cmd | awk '
BEGIN {printit = 1;}
$1 == "'$cmd'" && $2 == "()" {printit=0; next; }
/^}$/ { if (printit == 0) printit=1 ; else print $0; next ; }
/.*/ { if (printit) print $0; }'
else
builtin type $defarg $cmd
fi
fi
else
path=$(builtin type -path $cmd)
path=$(builtin type $defarg -p $cmd)
if [ "$path" ] ; then
echo $path
else
case "$cmd" in
/*) echo "" ;;
*) case "$(builtin type -type $cmd)" in
*) case "$(builtin type -t $cmd)" in
"") echo "" ;;
*) echo "$cmd" ;;
esac
@ -88,22 +116,27 @@ cd()
1) builtin cd "$@" ;;
2) old="$1"
new="$2"
dir=$(echo "$PWD" | sed "s:$old:$new:g")
# dir=$(echo "$PWD" | sed "s:$old:$new:g")
dir=${PWD//$old/$new}
case "$dir" in
"$PWD") echo "bash: cd: bad substitution" >&2 ; return 1 ;;
"$PWD") case "$PWD" in
*$old*) ;;
*) echo "$FUNCNAME: bad substitution" >&2 ; return 1 ;;
esac ;;
*) echo "$dir"
builtin cd "$dir"
;;
esac
;;
*) echo "cd: wrong arg count" >&2 ; return 1 ;;
*) echo "$FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
return 1 ;;
esac
}
#
# ksh print emulation
#
# print [-Rnprsu[n]] [arg ...]
# print [-Rnprsu[n]] [-f format] [arg ...]
#
# - end of options
# -R BSD-style -- only accept -n, no escapes
@ -112,27 +145,34 @@ cd()
# -r no escapes
# -s print to the history file
# -u n redirect output to fd n
# -f format printf "$format" "$@"
#
print()
{
local eflag=-e
local nflag=
local nflag= fflag= c
local fd=1
OPTIND=1
while getopts "Rnprsu:" c
while getopts "fRnprsu:" c
do
case $c in
R) eflag= ;;
r) eflag= ;;
n) nflag=-n ;;
s) sflag=y ;;
f) fflag=y ;;
u) fd=$OPTARG ;;
p) ;;
esac
done
shift $[ $OPTIND - 1 ]
shift $(( $OPTIND - 1 ))
if [ -n "$fflag" ]; then
builtin printf "$@" >&$fd
return
fi
case "$sflag" in
y) builtin history -s "$*" ;;

View file

@ -9,6 +9,7 @@
lowercase()
{
for file; do
[ -f "$file" ] || continue
filename=${file##*/}
case "$filename" in
*/*) dirname=${file%/*} ;;

View file

@ -17,9 +17,6 @@
# AUTHOR:
# Simon J. Gerraty <sjg@zen.void.oz.au>
# RCSid:
# $Id: add_path.sh,v 1.1 1995/09/30 12:45:23 sjg Exp $
#
# @(#)Copyright (c) 1991 Simon J. Gerraty
#
# This file is provided in the hope that it will

View file

@ -6,7 +6,7 @@
which()
{
local aflag sflag ES a
local aflag sflag ES a opt
OPTIND=1
while builtin getopts as opt ; do

View file

@ -0,0 +1,52 @@
#! /bin/bash
#From: kaz@cafe.net (Kaz Kylheku)
#Newsgroups: comp.unix.shell
#Subject: Why not roll your own @#$% find! (was: splitting directory off from filename)
#Message-ID: <6n1117$tp1@espresso.cafe.net>
#Date: Fri, 26 Jun 1998 20:47:34 GMT
# $1 = dirname, $2 = pattern, optional $3 = action
xfind()
{
local x
local dir="$1"
# descend into specified directory
builtin cd -L "$1" || {
echo "${FUNCNAME}: cannot change dir to $1" >&2
return 1
}
#
# default action is to print the filename
#
if [ -n "$3" ]; then
action="$3"
else
action='printf -- "%s\n"'
fi
# process ordinary files that match pattern
for x in $2 ; do
if [ -f "$x" ] ; then
eval "$action" "$x"
fi
done
# now descend into subdirectories, avoiding symbolic links
# and directories that start with a period.
for x in * ; do
if [ -d "$x" -a ! -L "$x" ] ; then
$FUNCNAME "$x" "$2" "$action"
fi
done
# finally, pop back up
builtin cd -L ..
}
#xfind "$@"

View file

@ -1,7 +1,22 @@
#
# Simple makefile for the sample loadable builtins
#
#
# Copyright (C) 1996 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
# Include some boilerplate Gnu makefile definitions.
prefix = @prefix@
@ -43,7 +58,8 @@ SHOBJ_LIBS = @SHOBJ_LIBS@
SHOBJ_STATUS = @SHOBJ_STATUS@
INC = -I. -I.. -I$(topdir) -I$(topdir)/lib -I$(topdir)/builtins \
-I$(BUILD_DIR) -I$(BUILD_DIR)/lib -I$(BUILD_DIR)/builtins
-I$(topdir)/include -I$(BUILD_DIR) -I$(BUILD_DIR)/lib \
-I$(BUILD_DIR)/builtins
.c.o:
$(SHOBJ_CC) $(SHOBJ_CFLAGS) $(CFLAGS) $(INC) -c -o $@ $<
@ -165,11 +181,14 @@ pushd: pushd.o
clean:
$(RM) $(ALLPROG) $(OTHERPROG) *.o
-( cd perl && ${MAKE} ${MFLAGS} $@ )
mostlyclean: clean
-( cd perl && ${MAKE} ${MFLAGS} $@ )
distclean maintainer-clean: clean
$(RM) Makefile pushd.c
-( cd perl && ${MAKE} ${MFLAGS} $@ )
print.o: print.c
truefalse.o: truefalse.c

View file

@ -222,16 +222,28 @@ static const struct conf_variable conf_table[] =
#endif
#if defined (_SC_THREADS)
{ "_POSIX_THREADS", SYSCONF, _SC_THREADS },
#endif
#if defined (_SC_THREADS)
{ "_POSIX_THREAD_ATTR_STACKADDR", SYSCONF, _SC_THREAD_ATTR_STACKADDR },
#endif
#if defined (_SC_THREAD_ATTR_STACKSIZE)
{ "_POSIX_THREAD_ATTR_STACKSIZE", SYSCONF, _SC_THREAD_ATTR_STACKSIZE },
#endif
#if defined (_SC_THREAD_PRIORITY_SCHEDULING)
{ "_POSIX_THREAD_PRIORITY_SCHEDULING", SYSCONF, _SC_THREAD_PRIORITY_SCHEDULING },
#endif
#if defined (_SC_THREAD_PRIO_INHERIT)
{ "_POSIX_THREAD_PRIO_INHERIT", SYSCONF, _SC_THREAD_PRIO_INHERIT },
#endif
#if defined (_SC_THREAD_PRIO_PROTECT)
{ "_POSIX_THREAD_PRIO_PROTECT", SYSCONF, _SC_THREAD_PRIO_PROTECT },
#endif
#if defined (_SC_THREAD_PROCESS_SHARED)
{ "_POSIX_THREAD_PROCESS_SHARED", SYSCONF, _SC_THREAD_PROCESS_SHARED },
# if defined (_SC_THREAD_SAFE_FUNCTIONS)
#endif
#if defined (_SC_THREAD_SAFE_FUNCTIONS)
{ "_POSIX_THREAD_SAFE_FUNCTIONS", SYSCONF, _SC_THREAD_SAFE_FUNCTIONS },
# endif
#endif /* _SC_THREADS */
#endif
/* XPG 4.2 Configurable System Variables. */
#if defined (_SC_ATEXIT_MAX)
@ -249,23 +261,44 @@ static const struct conf_variable conf_table[] =
#if defined (_SC_AIO_LISTIO_MAX)
{ "AIO_LISTIO_MAX", SYSCONF, _SC_AIO_LISTIO_MAX },
#endif
#if defined (_SC_AIO_MAX)
{ "AIO_MAX", SYSCONF, _SC_AIO_MAX },
#endif
#if defined (_SC_AIO_PRIO_DELTA_MAX)
{ "AIO_PRIO_DELTA_MAX", SYSCONF, _SC_AIO_PRIO_DELTA_MAX },
#endif
#if defined (_SC_DELAYTIMER_MAX)
{ "DELAYTIMER_MAX", SYSCONF, _SC_DELAYTIMER_MAX },
#endif
#if defined (_SC_GETGR_R_SIZE_MAX)
{ "GETGR_R_SIZE_MAX", SYSCONF, _SC_GETGR_R_SIZE_MAX },
#endif
#if defined (_SC_GETPW_R_SIZE_MAX)
{ "GETPW_R_SIZE_MAX", SYSCONF, _SC_GETPW_R_SIZE_MAX },
#endif
#if defined (_SC_MQ_OPEN_MAX)
{ "MQ_OPEN_MAX", SYSCONF, _SC_MQ_OPEN_MAX },
#endif
#if defined (_SC_MQ_PRIO_MAX)
{ "MQ_PRIO_MAX", SYSCONF, _SC_MQ_PRIO_MAX },
#endif
#if defined (_SC_RTSIG_MAX)
{ "RTSIG_MAX", SYSCONF, _SC_RTSIG_MAX },
#endif
#if defined (_SC_SEM_NSEMS_MAX)
{ "SEM_NSEMS_MAX", SYSCONF, _SC_SEM_NSEMS_MAX },
#endif
#if defined (_SC_SEM_VALUE_MAX)
{ "SEM_VALUE_MAX", SYSCONF, _SC_SEM_VALUE_MAX },
#endif
#if defined (_SC_SIGQUEUE_MAX)
{ "SIGQUEUE_MAX", SYSCONF, _SC_SIGQUEUE_MAX },
#endif
#if defined (_SC_TIMER_MAX)
{ "TIMER_MAX", SYSCONF, _SC_TIMER_MAX },
#endif /* _SC_AIO_LISTIO_MAX */
#endif
#if defined (_SC_LOGIN_NAME_MAX)
{ "LOGIN_NAME_MAX", SYSCONF, _SC_LOGIN_NAME_MAX },
#endif
@ -276,12 +309,18 @@ static const struct conf_variable conf_table[] =
{ "TTY_NAME_MAX", SYSCONF, _SC_TTY_NAME_MAX },
#endif
#if defined (_SC_PTHREAD_DESTRUCTOR_ITERATIONS)
#if defined (_SC_THREAD_DESTRUCTOR_ITERATIONS)
{ "PTHREAD_DESTRUCTOR_ITERATIONS", SYSCONF, _SC_THREAD_DESTRUCTOR_ITERATIONS },
#endif
#if defined (_SC_THREAD_KEYS_MAX)
{ "PTHREAD_KEYS_MAX", SYSCONF, _SC_THREAD_KEYS_MAX },
#endif
#if defined (_SC_THREAD_STACK_MIN)
{ "PTHREAD_STACK_MIN", SYSCONF, _SC_THREAD_STACK_MIN },
#endif
#if defined (_SC_THREAD_THREADS_MAX)
{ "PTHREAD_THREADS_MAX", SYSCONF, _SC_THREAD_THREADS_MAX },
#endif /* _SC_PTHREAD_DESTRUCTOR_ITERATIONS */
#endif
/* XPG 4.2 Optional Facility Configuration Values */
#if defined (_SC_XOPEN_UNIX)

View file

@ -0,0 +1,97 @@
#
# Makefile for builtin perl interpreter
#
#
# Copyright (C) 1998 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
# Include some boilerplate Gnu makefile definitions.
prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
libdir = @libdir@
infodir = @infodir@
includedir = @includedir@
topdir = @top_srcdir@
BUILD_DIR = @BUILD_DIR@
srcdir = @srcdir@
VPATH = .:@srcdir@
@SET_MAKE@
CC = @CC@
RM = rm -f
SHELL = @MAKE_SHELL@
PERL5 = perl5
CFLAGS = @CFLAGS@
#
# These values are generated for configure by ${topdir}/support/shobj-conf.
# If your system is not supported by that script, but includes facilities for
# dynamic loading of shared objects, please update the script and send the
# changes to bash-maintainers@gnu.org.
#
SHOBJ_CC = @SHOBJ_CC@
SHOBJ_CFLAGS = @SHOBJ_CFLAGS@
SHOBJ_LD = @SHOBJ_LD@
SHOBJ_LDFLAGS = @SHOBJ_LDFLAGS@
SHOBJ_XLDFLAGS = @SHOBJ_XLDFLAGS@
SHOBJ_LIBS = @SHOBJ_LIBS@
SHOBJ_STATUS = @SHOBJ_STATUS@
# Values used for compiling the perl files
PERL_LDOPTS = `${PERL5} -MExtUtils::Embed -e ldopts`
PERL_CFLAGS = ${CCFLAGS} `${PERL5} -MExtUtils::Embed -e ccopts`
SRC = bperl.c iperl.c perlxsi.c
OBJ = bperl.o iperl.o perlxsi.o
BUILTIN = bperl5
INC = -I. -I.. -I$(topdir) -I$(topdir)/lib -I$(topdir)/builtins \
-I$(topdir)/include -I$(BUILD_DIR) -I$(BUILD_DIR)/lib \
-I$(BUILD_DIR)/builtins
${BUILTIN}: ${OBJ}
${RM} $@
${SHOBJ_LD} ${SHOBJ_LDFLAGS} ${SHOBJ_XLDFLAGS} -o $@ ${OBJ} ${PERL_LDOPTS} ${SHOBJ_LIBS}
bperl.o: bperl.c
${RM} $@
$(SHOBJ_CC) $(SHOBJ_CFLAGS) $(CFLAGS) $(INC) -c -o $@ ${srcdir}/bperl.c
iperl.o: iperl.c
${RM} $@
$(SHOBJ_CC) ${SHOBJ_CFLAGS} $(PERL_CFLAGS) -c -o $@ ${srcdir}/iperl.c
perlxsi.c:
${PERL5} -MExtUtils::Embed -e xsinit -- -o $@
perlxsi.o: perlxsi.c
${RM} $@
${SHOBJ_CC} ${SHOBJ_CFLAGS} $(PERL_CFLAGS) -c -o $@ perlxsi.c
clean mostlyclean:
${RM} ${OBJ}
${RM} ${BUILTIN}
distclean maintainer-clean: clean
${RM} perlxsi.c

View file

@ -0,0 +1,6 @@
This illustrates how to build a perl interpreter into bash. It's not
especially useful; more a proof of concept (it provides none of the
bash internals to the perl interpreter, for example).
This *may* require adding "-rpath /path/to/perl/CORE" and -lperl options
when compiling bash itself.

View file

@ -0,0 +1,46 @@
/*
* perl builtin
*/
#include <config.h>
#include <fcntl.h>
#include <errno.h>
#include "builtins.h"
#include "shell.h"
#ifndef errno
extern int errno;
#endif
extern char **make_builtin_argv ();
extern char **export_env;
extern int perl_main();
bperl_builtin(list)
WORD_LIST *list;
{
char **v;
int c, r;
v = make_builtin_argv(list, &c);
r = perl_main(c, v, export_env);
free(v);
return r;
}
char *bperl_doc[] = {
"An interface to a perl5 interpreter.",
(char *)0
};
struct builtin bperl_struct = {
"bperl",
bperl_builtin,
BUILTIN_ENABLED,
bperl_doc,
"bperl [perl options] [file ...]",
0
};

View file

@ -0,0 +1,24 @@
#include <EXTERN.h> /* from the Perl distribution */
#include <perl.h> /* from the Perl distribution */
extern void xs_init _((void));
static PerlInterpreter *iperl; /*** The Perl interpreter ***/
int
perl_main(int argc, char **argv, char **env)
{
int r;
iperl = perl_alloc();
perl_construct(iperl);
perl_parse(iperl, xs_init, argc, argv, (char **)NULL);
r = perl_run(iperl);
PerlIO_flush(PerlIO_stdout());
PerlIO_flush(PerlIO_stderr());
perl_destruct(iperl);
perl_free(iperl);
return (r);
}

View file

@ -1,3 +1,7 @@
/*
* print -- loadable ksh-93 style print builtin
*/
#include "bashtypes.h"
#include <errno.h>
@ -19,8 +23,6 @@ static int printargs ();
static FILE *ofp;
extern char *ansicstr ();
extern char *this_command_name;
static char *print_doc[] = {
@ -150,7 +152,7 @@ printargs (list, ofp)
for (sawc = 0, l = list; l; l = l->next)
{
ostr = ansicstr (l->word->word, strlen (l->word->word), &sawc, (int *)0);
ostr = ansicstr (l->word->word, strlen (l->word->word), 0, &sawc, (int *)0);
fprintf (ofp, "%s", ostr);
free (ostr);
if (sawc)
@ -160,4 +162,3 @@ printargs (list, ofp)
}
return (1);
}

View file

@ -46,8 +46,8 @@ fi
oldpat=$1
newpat=$2
set $1
if [ ! -a "$1" ]; then
set -- $1
if [ ! -e "$1" ]; then
echo "$name: no files match $oldpat."
exit 1
fi
@ -79,7 +79,7 @@ while :; do
done
if [ $i -eq 1 ]; then
print -u2 "No globbing chars in pattern."
echo "No globbing chars in pattern." 1>&2
exit 1
fi

View file

@ -86,7 +86,8 @@ export PATH
trap 'echo Ouch!' 2 3
#trap '' 18 # disable Berkeley job control
ash_lk(){ echo " $1 " | fgrep " $2 " >&- 2>&-; }
#ash_lk(){ echo " $1 " | fgrep " $2 " >&- 2>&-; }
ash_lk(){ echo " $1 " | fgrep -q " $2 " >/dev/null 2>&1 ; }
ash_pr(){ echo $* | tr ' ' '\012' | pr -5 -t -w75 -l$[ ( $# + 4 ) / 5 ]; }
ash_rm(){ echo " $1 " | sed -e "s/ $2 / /" -e 's/^ //' -e 's/ $//'; }
@ -97,10 +98,13 @@ set -o emacs
cd
LIM=.limbo # $HOME/$LIM contains "destroyed" objects
mkdir $LIM >&- 2>&-
mkdir $LIM || {
echo "ash: cannot mkdir $LIM: exiting
exit 1
}
KNAP=.knapsack # $HOME/$KNAP contains objects being "carried"
if [ ! -d $KNAP ]
then mkdir $KNAP >&- 2>&-
then mkdir $KNAP >/dev/null 2>&1
if [ $? = 0 ]
then echo 'You found a discarded empty knapsack.'
else echo 'You have no knapsack to carry things in.'
@ -185,7 +189,7 @@ do room=`pwd`
set --
fi
if [ "$2" ]
then if mv $obj $2 >&- 2>&-
then if mv $obj $2 # >&- 2>&-
then echo "The $obj shimmers and turns into $2."
obs=`ash_rm "$2 $obs" "$obj"`
else echo "There is a cloud of smoke but the $obj is unchanged."
@ -214,7 +218,7 @@ do room=`pwd`
as) if [ "$2" ]
then if [ -f $2 ]
then echo "You must destroy $2 first."
else if cp $obj $2 >&- 2>&-
else if cp $obj $2 # >&- 2>&-
then echo "Poof! When the smoke clears, you see the new $2."
obs="$obs $2"
else echo 'You hear a dull thud but no clone appears.'
@ -240,7 +244,7 @@ do room=`pwd`
do if ash_lk "$kn" "$it"
then if [ -w $it ]
then echo "You must destroy $it first."
else if mv $HOME/$KNAP/$it $it >&- 2>&-
else if mv $HOME/$KNAP/$it $it # >&- 2>&-
then echo "$it: dropped."
kn=`ash_rm "$kn" "$it"`
obs=`echo $it $obs`
@ -281,7 +285,7 @@ do room=`pwd`
for it in $obj $x
do if ash_lk "$obs $hobs $exs $hexs" "$it"
then echo "Upon close inspection of the $it, you see:"
ls -ld $it 2>&-
ls -ld $it 2>/dev/null
if [ $? != 0 ]
then echo "-- when you look directly at the $it, it vanishes."
fi
@ -300,9 +304,9 @@ do room=`pwd`
case "$1" in
to) if [ "$2" ]
then shift
if PATH=$OPATH $* <$obj 2>&-
if PATH=$OPATH $* <$obj 2>/dev/null
then echo "The $1 monster devours your $obj."
if rm -f $obj >&- 2>&-
if rm -f $obj # >&- 2>&-
then obs=`ash_rm "$obs" "$obj"`
else echo 'But he spits it back up.'
fi
@ -331,7 +335,7 @@ do room=`pwd`
do if ash_lk "$obs $hobs" "$it"
then if ash_lk "$kn" "$it"
then echo 'You already have one.'
else if mv $it $HOME/$KNAP/$it >&- 2>&-
else if mv $it $HOME/$KNAP/$it # >&- 2>&-
then echo "$it: taken."
kn="$it $kn"
obs=`ash_rm "$obs" "$it"`
@ -367,7 +371,7 @@ do room=`pwd`
fi
for it in $obj $x
do if ash_lk "$obs $hobs" "$it"
then if mv $it $HOME/$LIM <&- >&- 2>&-
then if mv $it $HOME/$LIM # <&- >&- 2>&-
then if [ $verb = kill ]
then echo "The $it cannot defend himself; he dies."
else echo "You have destroyed the $it; it vanishes."
@ -457,7 +461,7 @@ do room=`pwd`
then for it in $obj $x
do if ash_lk "$obs $hobs" "$it"
then echo "The $it is already alive and well."
else if mv $HOME/$LIM/$it $it <&- >&- 2>&-
else if mv $HOME/$LIM/$it $it # <&- >&- 2>&-
then echo "The $it staggers to his feet."
obs=`echo $it $obs`
else echo "There are sparks but no $it appears."
@ -474,11 +478,11 @@ do room=`pwd`
case "$1" in
from) if [ "$2" ]
then shift
if PATH=$OPATH $* >$obj 2>&-
if PATH=$OPATH $* >$obj 2>/dev/null
then echo "The $1 monster drops the $obj."
obs=`echo $obj $obs`
else echo "The $1 monster runs away as you approach."
rm -f $obj >&- 2>&-
rm -f $obj # >&- 2>&-
fi
else echo 'From what?'
fi

24
examples/scripts/center Normal file
View file

@ -0,0 +1,24 @@
#! /bin/bash
#
# center - center a group of lines
#
# tabs in the lines might cause this to look a little bit off
#
#
width=${COLUMNS:-80}
if [[ $# == 0 ]]
then
set -- /dev/stdin
fi
for file
do
while read -r
do
printf "%*s\n" $(( (width+${#REPLY})/2 )) "$REPLY"
done < $file
done
exit 0

View file

@ -0,0 +1,185 @@
#! /bin/bash
#
#From: kaz@cafe.net (Kaz Kylheku)
#Newsgroups: comp.unix.shell
#Subject: Funky little bash script
#Message-ID: <6mspb9$ft2@espresso.cafe.net>
#Date: Thu, 25 Jun 1998 06:11:39 GMT
#Here is something I wrote a few years ago when I was bored one day.
#Warning: this contains control characters.
# Line input routine for GNU Bourne-Again Shell
# plus terminal-control primitives.
#
# by Kaz Kylheku
# June 1996, Vancouver, Canada
#
# Function to disable canonical input processing.
# Terminal modes are saved into variable "savetty"
#
#
function raw
{
savetty=$(stty -g)
stty -icanon -isig -echo -echok -echonl inlcr
}
#
# Function to restore terminal settings from savetty variable
#
function restore
{
stty $savetty
}
#
# Set terminal MIN and TIME values.
# If the input argument is a zero, set up terminal to wait for
# a keystroke indefinitely. If the argument is non-zero, set up
# an absolute timeout of that many tenths of a second. The inter-keystroke
# timer facility of the terminal driver is not exploited.
#
function settimeout
# $1 = tenths of a second
{
if [ "$1" = "0" ] ; then
min=1
timeout=0
else
min=0
timeout="$1"
fi
stty min $min time $timeout
unset min timeout
}
#
# Input a single key using 'dd' and echo it to standard output.
# Launching an external program to get a single keystroke is a bit
# of a pig, but it's the best you can do! Maybe we could convince the
# GNU guys to make 'dd' a bash builtin.
#
function getkey
{
eval $1="\"\$(dd bs=1 count=1 2> /dev/null)\""
}
#
# Input a line of text gracefully.
# The first argument is the name of a variable where the input line is
# to be stored. If this variable is not empty, its contents are printed
# and treated as though the user had entered them.
# The second argument gives the maximum length of the input line; if it
# is zero, the input is unlimited (bad idea).
# ^W is used to delete words
# ^R redraws the line at any time by backspacing over it and reprinting it
# ^U backspaces to the beginning
# ^H or ^? (backspace or del) delete a single character
# ^M (enter) terminates the input
# all other control keys are ignored and cause a beep when pressed
#
#
function getline
{
settimeout 0 # No keystroke timeout.
save_IFS="$IFS" # Save word delimiter and set it to
IFS="" # to null so ${#line} works correctly.
eval line=\${$1} # Fetch line contents
echo -n "$line" # and print the existing line.
while [ 1 ] ; do
getkey key # fetch a single keystroke
case "$key" in
 |  ) # BS or DEL
if [ ${#line} != 0 ] ; then # if line not empty
echo -n " " # print destructive BS
line="${line%%?}" # chop last character
else # else if line empty
echo -n  # beep the terminal
fi
;;
 ) # kill to line beg
while [ ${#line} != 0 ] ; do # while line not empty
echo -n " " # print BS, space, BS
line="${line%?}" # shorten line by 1
done
;;
 ) # redraw line
linesave="$line" # save the contents
while [ ${#line} != 0 ] ; do # kill to line beg
echo -n " "
line="${line%?}"
done
echo -n "$linesave" # reprint, restore
line="$linesave"
unset linesave # forget temp var
;;
 )
while [ "${line% }" != "$line" -a ${#line} != 0 ] ; do
echo -n " "
line="${line%?}"
done
while [ "${line% }" = "$line" -a ${#line} != 0 ] ; do
echo -n " "
line="${line%?}"
done
;;
 |  |  |  |  |  |  |  |  | | | )
echo -n  # ignore various control characters
;; # with an annoying beep
 |  |  |  |  |  |  |  |  |  |  |  |  )
echo -n 
;;
' ' |  |  |  |  |  )
echo -n 
;;
'' ) # Break out of loop on carriage return.
echo # Send a newline to the terminal.
break # (Also triggered by NUL char!).
;;
* ) # Append character to the end of the line.
# If length is restricted, and the line is too
# long, then beep...
if [ "$2" != 0 -a $(( ${#line} >= $2 )) = 1 ] ; then
echo -n 
else # Otherwise add
line="$line$key" # the character.
echo -n "$key" # And echo it.
fi
;;
esac
done
eval $1=\"\$line\"
IFS="$save_IFS"
unset line save_IFS
}
# uncomment the lines below to create a standalone test program
#
echo "Line input demo for the GNU Bourne-Again Shell."
echo "Hacked by Kaz Kylheku"
echo
echo "Use ^H/Backspace/Del to erase, ^W to kill words, ^U to kill the"
echo "whole line of input, ^R to redraw the line."
echo "Pass an argument to this program to prime the buffer contents"
raw
echo -n "go: "
if [ ${#1} != 0 ] ; then
LINE=$1
fi
getline LINE 50
restore
echo "<$LINE>"

100
examples/scripts/vtree3a Normal file
View file

@ -0,0 +1,100 @@
#!/bin/bash
#
# Name: dirtree
# Programmer:
# Hemant T. Shah
# Life Insurance Data Processing
# July 12 1994
#
# Description:
# Print directory tree structure as follows:
# |___Mail
# |___scheduler
# |___cics_scripts
# |___tar_msdos
# |___awk
# |___attributes
# |___tmp
# |___News
# |___dosscsi
# |___FAQ_xterminal
# |___shell_history.Z
# |___FAQ_AIX
# |___aix_ftp_site
# |___hp_software
# |___dnload
# |___telnet.h
# |___msdos
# |___tnetd.tar.Z
# |___aix
# |___hp
# |___xkey.c
#
# Conversion to bash v2 syntax done by Chet Ramey
# - removed command substitutions calling `basename'
#
ProgramName=${0##*/}
Path="."
ShowAll=1
ShowDir=0
ExpandDirectory()
{
local object # Local variable
cd "$1"
for object in $PWD/.??* $PWD/*
do
if [ -d $object ]; # It is a directory
then
echo "${indent}|___${object##*/}/"
indent="${indent}! " # Add to indentation
if [ -x $object ];
then
ExpandDirectory $object
fi
indent=${indent%????} # Remove from indentation
elif [ -e $object ]; then
if (( ShowAll == 1 ));
then
echo "${indent}|___${object##*/}"
fi
fi
done
}
usage()
{
echo -e "Usage: $ProgramName [-h] [-f] [-d] [path] "
echo -e "\t-h ... display this help message."
echo -e "\t-f path ... shows all files and directories below path (default)."
echo -e "\t-d path ... shows all directories only below path."
}
while getopts "fd" opt
do
case $opt in
f) ShowAll=1 ;;
#d) ShowDir=1 ;;
d) ShowAll=0 ;;
*) usage ; exit 2;;
esac
done
shift $(( $OPTIND - 1 ))
Path=${1:-.}
if [ ! -d "$Path" ]; then
echo "$0: error: specified path is not a directory." >&2
exit 1
fi
echo "!$Path/"
ExpandDirectory $Path

230
examples/scripts/websrv.sh Normal file
View file

@ -0,0 +1,230 @@
#!/bin/sh
#for instructions or updates go to:
#<A HREF="http://math.ucr.edu:8889/">This script's home page</A>
#email me questions or comments at:
#<A HREF="mailto:insom@math.ucr.edu">insom@math.ucr.edu</A>
#copyright chris ulrich; This software may be used or modified
#in any way so long as this notice remains intact.
#
# WWW server in sh
# Author: Chris Ulrich <chris@tinker.ucr.edu>
#
INDEX=index.html
date=`date`
DOCHOME=/home/insom/web-docs
BINHOME=/home/insom/web-bin
LOGHOME=/home/insom/web-logs
LOGFILE=$LOGHOME/access_log
#verbose=:
verbose=echo
exec 2>> $LOGHOME/error_log
hheader() {
echo "HTTP/1.0 200 OK
Server: WebSH/2.00
Connection: close
Date: $date"
}
header() {
echo "Content-type: $1
"
}
no_url() {
header "text/plain"
echo "No such url $1"
}
send() {
#case "$#" in 2) ;; *) echo eep! | mailx insom@math.ucr.edu ; exit 3 ;; esac
if test -f "$DOCHOME/$2"
then
header "$1"
cat "$DOCHOME/$2"
else
no_url "$2"
fi
}
LsToHTML() {
if test -f "$DOCHOME/$url/.title"
then
header "text/html; charset=US-ASCII"
echo "<pre>"
cat "$DOCHOME/$url/.title"
echo "</pre>"
elif test -f "$DOCHOME/$url/.title.html"
then
header "text/html; charset=US-ASCII"
cat "$DOCHOME/$url/.title.html"
else
header "text/html; charset=US-ASCII"
fi
case "$url" in
/) ;;
*) url="$url/"
esac
while read link
do
case $link in
*.cgi) ;;
*)
echo "<A HREF=\"$url$link\">$link</A> <BR>"
;;
esac
done
}
read method data
$verbose "
$date access from ${TCPREMOTEINFO:=NO-IDENT}@${TCPREMOTEHOST:=$TCPREMOTEIP}
on local machine $TCPLOCALHOST
$method $data " >> $LOGFILE
for hopeurl in $data
do
url="${url}${url:+ }$second"
second="$hopeurl"
done
case "$second" in
*[1-9].*)
read inheader
while
case "$inheader" in
?|'') false
;;
*)
read inheader
;;
esac
do
:
done
hheader
;;
esac
case "$url" in
*..*)
no_url "$url"
exit 1
;;
*.txt|*.[ch])
send "text/plain; charset=US-ASCII" "$url"
;;
*.html)
send "text/html; charset=US-ASCII" "$url"
;;
*.cgi)
if test -x "$DOCHOME/$url"
then
read message
echo "$message" | "$DOCHOME/$url"
else
no_url "$url"
fi
;;
*".cgi?"*)
oIFS="$IFS"
echo "$url" | {
IFS='?' read url QUERY_STRING
if test -x "$DOCHOME/$url"
then
IFS="$oIFS"
export QUERY_STRING
"$DOCHOME/$url"
else
no_url "$url"
fi
}
;;
*.[Gg][Ii][Ff])
send "image/gif" "$url"
;;
*.[Jj][Pp][Gg]|*.[Jj][Pp][Ee][Gg])
send "image/jpeg" "$url"
;;
*.tbl)
header "text/html; charset=US-ASCII"
echo "<pre>"
test -f "$DOCHOME/$url" &&
tbl < "$DOCHOME/$url" | nroff ||
no_url "$url"
echo "</pre>"
;;
*.nroff)
header "text/html; charset=US-ASCII"
echo "<pre>"
test -f "$DOCHOME/$url" &&
nroff < "$DOCHOME/$url" ||
no_url "$url"
echo "</pre>"
;;
*mp[23])
if test -f "$DOCHOME/$url"
then
header "application/mpstream"
echo "+$TCPLOCALIP:${MPSERVPORT:=9001}/$url"
else
no_url "$url"
fi
;;
*.[0-9]|*.[0-9][a-z])
header "text/html; charset=US-ASCII"
echo "<pre>"
if test -f "$DOCHOME/$url"
then
#nroff -man "$DOCHOME/$url" | $BINHOME/man2html
echo "perl at the moment is broken, so man2html doesn't work. Sorry."
echo "</pre>"
else
no_url "$url"
fi
;;
*.???|*.??)
send "unknown/data" "$url"
;;
*/)
if test -d "$DOCHOME/$url"
then
ls "$DOCHOME/$url" | LsToHTML
fi
;;
*)
if test -f "$DOCHOME/$url"
then
read filetype < "$DOCHOME/$url"
case "$filetype" in
\#!/*/*|\#!?/*/*)
header "text/plain; charset=US-ASCII"
cat "$DOCHOME/$url"
;;
'<!*>')
header "text/html; charset=US-ASCII"
cat "$DOCHOME/$url"
;;
*)
header "text/html; charset=US-ASCII"
echo "<pre>"
cat "$DOCHOME/$url"
echo "</pre>"
;;
esac
elif test -f "$DOCHOME/$url/$INDEX"
then
header "text/html; charset=US-ASCII"
cat "$DOCHOME/$url/$INDEX"
elif test -d "$DOCHOME/$url"
then
ls "$DOCHOME/$url" | LsToHTML
else
no_url "$url"
fi
;;
esac

27
examples/scripts/xterm_title Executable file
View file

@ -0,0 +1,27 @@
#! /bin/bash
#
# xterm_title - print the contents of the xterm title bar
#
# Derived from http://www.clark.net/pub/dickey/xterm/xterm.faq.html#how2_title
#
P=${0##*/}
[ -z "$DISPLAY" ] && {
echo "${P}: not running X" >&2
exit 1
}
if [ -z "$TERM" ] || [ "$TERM" != "xterm" ]; then
echo "${P}: not running in an xterm" >&2
exit 1
fi
exec </dev/tty
old=$(stty -g)
stty raw -echo min 0 time ${1-10}
echo -e "\033[21t\c" > /dev/tty
IFS='' read -r a
stty $old
b=${a#???}
echo "${b%??}"
exit 0

View file

@ -0,0 +1,24 @@
This directory contains some useful bash files.
In order to use this configuration:
echo "source ~/.bashrc" > ~/.profile
echo "source /usr/share/init/bash/rc" > ~/.bashrc
echo "source /usr/share/init/bash/login" > ~/.login
In order to customize this setup:
mkdir ~/Library/init/bash
and create the following files there as necessary:
aliases.mine - shell aliases
completions.mine - completions
environment.mine - environment
rc.mine - run commands
path - command search path
See the corresponding file in /usr/share/init/bash for more information about the role of each file. You can easily extend or override the configuration provided by the default file. For example, you can add more aliases by adding the appropriate commands in aliases.mine.
-Fred
tritan@mit.edu

View file

@ -0,0 +1,34 @@
##
# Bash aliases file
#
# Wilfredo Sanchez Jr. | tritan@mit.edu
##
##
# Aliases
##
alias .='cwd'
alias ..='cd ..'
alias cd..='cd ..'
alias cdwd='cd $(/bin/pwd)'
alias cwd='echo $PWD'
alias l='ls -lg'
##
# Functions
##
files () { find ${1} -type f -print ; }
ff () { find . -name ${1} -print ; }
ll () { ls -lag "$@" | more ; }
word () { fgrep -i "$*" /usr/dict/web2 ; }
wordcount () { cat "${1}" | tr -s ' .,;:?\!()[]"' '\012' | \
cat -n | tail -1 | awk '{print $1}' ; }
##
# Read user's aliases
##
if [ -r ${bash_initdir}/aliases.mine ]; then
source ${bash_initdir}/aliases.mine
fi

View file

@ -0,0 +1,22 @@
##
# Bash
# User preferences file
# Override these in rc.mine
#
# Wilfredo Sanchez Jr. | tritan@mit.edu
# July 09, 1992
#
# MIT Project Athena
##
if [ -n "$PS1" ]; then
# Prompts
PS1='[\h:\w] \u\$ '
PS2=' -> '
#PS3=
#PS4=
set -o emacs
fi

View file

@ -0,0 +1,24 @@
##
# Bourne Again Shell environment file
# Global environment setup
#
# Wilfredo Sanchez Jr. | tritan@mit.edu
# July 09, 1992
#
# MIT Project Athena
#
# ORIGINAL SOURCES: /usr/athena/lib/init/cshrc (ATHENA REL 7.3P)
##
export ENV_SET="YES" # avoid repeat
# File creation mask
umask 022 # all files created are -rw-r--r--
##
# Load user environment
##
if [ -f ${bash_initdir}/environment.mine ]; then
source ${bash_initdir}/environment.mine
fi

View file

@ -0,0 +1,15 @@
##
# Set path
##
export PATH="${HOME}/${MACHTYPE}/bin:${HOME}/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
export MANPATH="${HOME}/man:/usr/local/share/man:/usr/share/man"
##
# Read user's login
##
if (-r ${bash_initdir}/login.mine) then
source ${bash_initdir}/login.mine
fi

View file

@ -0,0 +1,10 @@
##
# Destroy credentials
##
if [ -z "${TERM_PROGRAM}" ]; then
# Don't run these commands if the shell is launched by Terminal,
# even if it's a login shell.
if klist -s; then kdestroy; fi
fi

View file

@ -0,0 +1,63 @@
##
# Bourne Again Shell config file
#
# Wilfredo Sanchez Jr. | tritan@mit.edu
# July 09, 1992
#
# MIT Project Athena
#
# ORIGINAL SOURCES: /usr/athena/lib/init/cshrc (ATHENA REL 7.3P)
##
default_initdir=/usr/share/init
default_bash_initdir=${default_initdir}/bash
user_initdir=~/Library/init
user_bash_initdir=${user_initdir}/bash
if [ -r ${user_bash_initdir} ]; then
initdir=${user_initdir}
bash_initdir=${user_bash_initdir}
else
initdir=${default_initdir}
bash_initdir=${default_bash_initdir}
fi
# SET UP HOST-DEPENDANT VARIABLES, ETC.
host=$(echo $(hostname) | tr A-Z a-z)
user=`whoami`
export HOST=${host}
export USER=${user}
# User ID
if [ -z "${uid}" ]; then uid=$(id | cut -d = -f 2 | cut -d \( -f 1); fi
# SET COMMAND SEARCH PATH AND MAN PATH
if [ -f ${bash_initdir}/path ]; then source ${bash_initdir}/path; fi
# ENVIRONMENT SETUP
if [ -n "${PS1}" ]; then interactive="YES"; fi
if [ -z "${ENV_SET}" ]; then
if [ -f ${default_bash_initdir}/environment ]; then
#echo "Initializing environment..."
source ${default_bash_initdir}/environment
fi
fi
if [ -r ${default_bash_initdir}/bash.defaults ]; then
source ${default_bash_initdir}/bash.defaults
fi
# DEFAULT LOGIN SOURCES
if [ -f ${bash_initdir}/rc.mine ]; then source ${bash_initdir}/rc.mine; fi
if [ "${interactive}" = "YES" ]; then
# These aren't useful for non-interactive sessions
if [ -f ${default_bash_initdir}/aliases ]; then
source ${default_bash_initdir}/aliases
fi
fi