📄 bcsh.sh
字号:
i="`expr \"$i\" : '.*\(!!.*\)'`" ;; *!!*) front="`expr \"$i\" : '\(.*\)!!.*'`" i="`expr \"$i\" : '.*\(!!.*\)'`" ;; !*) front= i="`expr \"$i\" : '.*!\(.*\)'`" ;; *) tmp="$tmp$i " continue ;; esac case "$i" in !!*) # want last command rest="`expr \"$i\" : '!!\(.*\)'`" i=$lastcmd ;; -*) # we want to search back through the history list case "$i" in -) rest="`expr \"$i\" : '-\(.*\)'`" i=$lastcmd ;; -[0-9]*) wanted="`expr \"$i\" : '-\([0-9][0-9]*\).*'`" rest="`expr \"$i\" : '-[0-9][0-9]*\(.*\)'`" i="`tail -$wanted $histfile | sed -e "1q"`" ;; esac ;; [0-9]*) # find which number command is wanted wanted="`expr \"$i\" : '\([0-9][0-9]*\).*'`" rest="`expr \"$i\" : '[0-9][0-9]*\(.*\)'`" i="`grep -n . $histfile | grep \"^$wanted\"`" i="`expr \"$i\" : \"${wanted}.\(.*\)\"`" ;; \?*) # find which 'command-contains' match is wanted case "$i" in \?{*}*) wanted="`expr \"$i\" : '?{\(.*\)}.*'`" rest="`expr \"$i\" : '?.*}\(.*\)'`" ;; \?*:*) wanted="`expr \"$i\" : '?\(.*\):.*'`" rest="`expr \"$i\" : '?.*\(:.*\)'`" ;; \?*) wanted="`expr \"$i\" : '?\(.*\)'`" rest= ;; esac i="`grep \"$wanted\" $histfile | tail -1`" ;; *) # find which 'start-of-command' match is wanted case "$i" in {*}*) wanted="`expr \"$i\" : '{\(.*\)}.*'`" rest="`expr \"$i\" : '.*}\(.*\)'`" ;; *:*) wanted="`expr \"$i\" : '\(.*\):.*'`" rest="`expr \"$i\" : '.*\(:.*\)'`" ;; *) wanted="$i" rest= ;; esac i="`grep \"^$wanted\" $histfile | tail -1`" ;; esac # see if we actually found anything to substitute case "$i" in "") badsub="Event not found" break ;; *) badsub=no ;; esac case "$rest" in "") tmp="$front$tmp$i " continue ;; :[0-9]*) # find which element of $i is wanted number="`expr \"$rest\" : ':\([0-9][0-9]*\).*'`" rest="`expr \"$rest\" : ':[0-9][0-9]*\(.*\)'`" # count through $i till we get to the # right element counter=0 for element in $i do case "$counter" in $number) break ;; *) counter="`expr \"$counter\" + 1`" # counter=$[ $counter + 1 ] ;; esac done case "$counter" in $number) badsub=no ;; *) badsub="Bad command element" break ;; esac tmp="$tmp$front$element$rest " continue ;; :\$*) # spin through $i till we hit the last element rest="`expr \"$rest\" : ':\$\(.*\)'`" for element in $i do : done tmp="$tmp$front$element$rest " continue ;; :\**) # we want all elements except the command itself rest="`expr \"$rest\" : ':\*\(.*\)'`" save=$i set - $i shift case "$*" in "") badsub="No arguments to command '$save'" break ;; *) badsub=no ;; esac tmp="$tmp$front$*$rest " continue ;; :s*|:gs*) # we are doing a substitution # put / on end if needed case "$rest" in :s/*/*/*|:gs/*/*/*) ;; :s/*/*|:gs/*/*) rest="${rest}/" ;; esac # find what substitution is wanted first="`expr \"$rest\" : ':*s\/\(.*\)\/.*\/.*'`" second="`expr \"$i\" : ':*s/.*/\(.*\)/.*'`" # see if it is a global substitution case "$rest" in :gs*) global=g ;; :s*) global= ;; esac rest="`expr \"$rest\" : '.*/.*/.*/\(.*\)'`" i="`echo \"$i\" | sed -e \"s@$first@$second@$global\"`" # see if subsitution worked case "$i" in "") badsub="Substiution failed" break ;; *) badsub=no ;; esac tmp="$tmp$front$i$rest " continue ;; *) tmp="$tmp$front$i$rest " ;; esac done case "$badsub" in no) ;; *) echo "$badsub" badsub=no continue ;; esac cmd="$tmp" echoit=yes getcmd=no continue ;; *) run=yes ;; esac case "$cmd" in *\^*\^*\^*) # see if the substitution is global case "$cmd" in g*) global=g ;; *) global= ;; esac # put a '^' on the end if necessary case "$cmd" in *\^) ;; *) cmd="${cmd}^" ;; esac # find what substitution is wanted first="`expr \"$cmd\" : '*\^\(.*\)\^.*\^.*'`" second="`expr \"$cmd\" : '*\^.*\^\(.*\)\^.*'`" rest="`expr \"$cmd\" : '*\^.*\^.*\^\(.*\)'`" cmd="`echo \"$lastcmd\" | sed -e \"s@$first@$second@$global\"`$rest" # see if the substitution worked case "$cmd" in "") echo "Substitution failed" continue ;; esac echoit=yes getcmd=no continue ;; *~e) echo "$cmd" | sed -e "s@~e@@" > /tmp/bcsh$$ $EDITOR /tmp/bcsh$$ cmd="`cat /tmp/bcsh$$`" getcmd=no continue ;; *~v) echo "$cmd" | sed -e "s@~v@@" > /tmp/bcsh$$ echo "$lastcmd" > /tmp/bcsh$$ $VISUAL /tmp/bcsh$$ cmd="`cat /tmp/bcsh$$`" getcmd=no continue ;; exec[\ \ ]*) tail -$savehist $histfile>/tmp/hist$$ uniq /tmp/hist$$ > $histfile rm -f /tmp/*$$ echo $cmd > /tmp/cmd$$ . /tmp/cmd$$ ;; login[\ \ ]*|newgrp[\ \ ]*) tail -$savehist $histfile>/tmp/hist$$ uniq /tmp/hist$$ > $histfile rm -f /tmp/*$$ echo $cmd > /tmp/cmd$$ . /tmp/cmd$$ ;; logout|exit|bye) if test -s "$logoutfile" then # sh $logoutfile $SHELL $logoutfile fi tail -$savehist $histfile > /tmp/hist$$ uniq /tmp/hist$$ > $histfile rm -f /tmp/*$$ exit 0 ;; h|history) grep -n . $histfile | tail -$history | sed -e 's@:@ @' | $PAGER continue ;; h[\ \ ]\|*|h[\ \ ]\>*|h\|*|h\>*) cmd="`echo \"$cmd\" | sed -e \"s@h@grep -n . $histfile | tail -$history | sed -e 's@:@ @'@\"`" getcmd=no continue ;; history[\ \ ]*\|*|history[\ \ ]*\>*) cmd="`echo \"$cmd\" | sed -e \"s@history@grep -n . $histfile | tail -$history | sed -e 's@:@ @'@\"`" getcmd=no continue ;; source[\ \ ]*) set - $cmd shift echo . $* > /tmp/cmd$$ . /tmp/cmd$$ run=no ;; wait) wait run=no ;; .[\ \ ]*) echo $cmd > /tmp/cmd$$ . /tmp/cmd$$ run=no ;; cd|cd[\ \ ]*) # check if it will work first, or else this shell will terminate # if the cd dies. If you have a built-in test, you might want # to replace the try-it-and-see below with a couple of tests, # but it is probably just as fast like this. echo $cmd > /tmp/cmd$$ if ($SHELL /tmp/cmd$$) ; then . /tmp/cmd$$ fi run=no ;; awk[\ \ ]*|dd[\ \ ]*|cc[\ \ ]*|make[\ \ ]*) # these are the only commands I can think of whose syntax # includes an equals sign. Add others as you find them. echo "$cmd" > /tmp/bcsh$$ ;; setenv*|*=*) # handle setting shell variables, turning cshell syntax to Bourne # syntax -- note all variables must be exported or they will not # be usable in other commands echo "$cmd" > /tmp/cmd$$ ed - /tmp/cmd$$ << ++++ g/^setenv[ ]/s/[ ]/@/ g/^setenv@/s/[ ]/=/ g/^setenv@/s/// g/^set/s/// .t. \$s/=.*// s/^/export / w++++ . /tmp/cmd$$ rm -f /tmp/cmd$$ run=no ;; unset[\ \ ]*|umask[\ \ ]*|export[\ \ ]*|set[\ \ ]*) # handle commands which twiddle current environment $cmd run=no ;; alias|alias[\ \ ]) if [ -f $aliasfile ]; then $PAGER $aliasfile fi lastcmd=$cmd run=no continue ;; alias[\ \ ]*) case "$cmd" in alias[\ \ ]\|*|alias[\ \ ]\>*) cmd="`echo \"$cmd\" | sed -e \"s@alias@cat $aliasfile@\"`" getcmd=no continue ;; alias[\ \ ]*[\ \ ]*) ;; *) echo "Syntax: alias name command" cmd= continue ;; esac set - $cmd shift cmd="$*" # make sure there is always 1 blank line in file so # unaliasing will always work -- ed normally refuses # to write an empty file echo "" >> $aliasfile cat << ++++ >> $aliasfile$cmd++++# ed - $aliasfile << '++++'# g/alias[ ]/s///# g/^['"]\(.*\)['"]$/s//\1/# g/^/s//alias /# w#++++ sort -u -o $aliasfile $aliasfile doalias=yes cmd="alias $cmd" run=no ;; unalias[\ \ ]*) set - $cmd case "$#" in 2) cmd=$2 ;; *) echo "Syntax: unalias alias_name" continue ;; esac ed - $aliasfile << ++++ /^$cmd[ ]/d w++++ case "`set - \`wc -l $aliasfile\`;echo $1`" in 1) # just removed last alias doalias=no ;; esac run=no ;; *) case "$doalias" in yes) set - $cmd tmp="`grep \"^$1 \" $aliasfile`" case "$tmp" in $1[\ \ ]*) shift cmd=$* set - $tmp shift tmp=$* case "$tmp" in *\$*) # uses positional variables cmd="set - $cmd ; $tmp" getcmd=no continue ;; *) cmd="$tmp $cmd" getcmd=no continue ;; esac ;; *) echo "$cmd" > /tmp/bcsh$$ ;; esac ;; no) echo "$cmd" > /tmp/bcsh$$ ;; esac ;; esac case "$cmd" in *+~+p) cmd="`expr \"$cmd\" : '\(.*\)+~+p'`" echoit=yes run=no ;; esac case "$cmd" in "") continue ;; *) case "$exclaim" in yes) cmd="`echo \"$cmd\" | sed -e 's@REALEXCLAMATIONMARK@!@g'`" echo "$cmd" > /tmp/bcsh$$ ;; esac case "$echoit" in yes) echo $cmd ;; esac case "$run" in yes) case "${noclobber+yes}" in yes) case "$cmd" in *\>![\ \ ]*) ed - /tmp/bcsh$$ << ++++ g/>!/s//>/ w++++ ;; *\>\>*) ;; *\>*) outfile="`expr \"$cmd\" : '.*>\(.*\)'`" case "$outfile" in \&*) ;; *) set - $outfile outfile="$1" if test -s "$outfile" then case "${iclobber+yes}" in yes) echo $n "Overwrite ${outfile}? $c" read answer case "$answer" in y*) ;; *) echo ':' > /tmp/bcsh$$ ;; esac ;; *) echo "${outfile}: file exists" echo ':' > /tmp/bcsh$$ ;; esac fi ;; esac ;; esac ;; *) case "$cmd" in *\>![\ \ ]*) ed - /tmp/bcsh$$ << ++++ g/>!/s//>/g w++++ ;; esac ;; esac (trap 'exit 1' 2 3; $BASH /tmp/bcsh$$) ;; esac case "$cmd" in $lastcmd) ;; *) case "$exclaim" in yes) cmd="`echo \"$cmd\" | sed -e 's@!@\\\\!@g'`" ;; esac cat << ++++ >> $histfile$cmd++++ lastcmd=$cmd case "$inc_cmdno" in yes) cmdno="`expr \"$cmdno\" + 1`" # cmdno=$[$cmdno + 1] ;; esac ;; esac ;; esac # The next commented-out line sets the prompt to include the command # number -- you should only un-comment this if it is the ONLY thing # you ever want as your prompt, because it will override attempts # to set PS1 from the command level. If you want the command number # in your prompt without sacrificing the ability to change the prompt # later, replace the default setting for PS1 before the beginning of # the main loop with the following: PS1='echo -n "${cmdno}% "' # Doing it this way is, however, slower than the simple version below. PS1="${cmdno}% " getcmd=yes echoit=no exclaim=nodoneexit 0# Christine Robertson {linus, ihnp4, decvax}!utzoo!globetek!chris
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -