| Korn Shell: Unix and Linux Programming Manual, Third Edition, TheBy Anatole Olczak
|
|
|
Appendix A. Sample .profile File
This appendix contains a sample .profile file. Notice that the environment variables are set and exported with one typeset command. This speeds up processing of the .profile file.
# # Sample .profile File # # Set/export environment variables typeset -x CDPATH=:$HOME:/usr/spool \ EDITOR=vi \ ENV=${HOME:-.}/.env \ HISTFILE=$HOME/.history \ HISTSIZE=200 \ HOME=/home/anatole \ LOGNAME=anatole \ MAILCHECK=300 \ MAILPATH=~/inbox:/usr/spool/mail/$LOGNAME \ PAGER=$(whence more) \ PATH=${PATH#:}:/usr/etc:$NEWS/bin: \ PS1='!:$PWD> ' \ PS4='[$LINENO]+ ' \ SHELL=/bin/ksh \ TERM=sun \ TMOUT=600 \ USER=$LOGNAME # Set global options set -o noclobber -o markdirs +o bgnice # Execute commands from the ~/.logout file on exit trap '. ~/.logout' EXIT
|
| Korn Shell: Unix and Linux Programming Manual, Third Edition, TheBy Anatole Olczak
|
|
|
Appendix B. Sample Environment File
This appendix contains a sample environment file. It sets the global functions, aliases, and prompt variable.
# # Sample env File # # Function md - make a directory and _cd to it function md { mkdir $1 && _cd $1 } # Set up the echo alias alias -x echo='print -' # Set temporary prompt variable to the command number # followed by a colon PS0='!:' # Function _cd - changes directories, then sets the # command prompt to: "command-number:pathname>" function _cd { if (($# == 0)) then 'cd' PS1="$PS0$PWD> " fi if (($# == 1)) then 'cd' $1 PS1="$PS0$PWD> " fi if (($# == 2)) then 'cd' $1 $2 PS1="$PS0$PWD> " fi } # Alias the cd command to the _cd function alias -x cd=_cd # Export the _cd and md functions typeset -fx _cd md
|
| Korn Shell: Unix and Linux Programming Manual, Third Edition, TheBy Anatole Olczak
|
|
|
Appendix C. C Shell Functionality
This appendix contains the source code listings for the C Shell directory management functions, and other miscellaneous C Shell commands.
|
| Korn Shell: Unix and Linux Programming Manual, Third Edition, TheBy Anatole Olczak
| Appendix C. C Shell Functionality |
Directory Functions These Korn shell functions implement the C shell directory management commands: dirs , popd , and pushd . They can be put into a separate cshfuncs file and read in with the . command when necessary. Their basic functionality is: dirs | display directory stack | popd | remove directory stack entry and cd to it | pushd | add directory stack entry and cd to it |
The dirs function lists the current directory stack. With no argument, the popd function removes the top entry (previous working directory) from the directory stack and changes directory to it. If a + n argument is given, then the nth directory stack entry ( nth previous working directory) is removed. With no argument, the pushd function switches the top two entries (current and previous working directory) and changes directory to the previous working directory. This is equivalent to " cd ?/span>". If a directory argument is given, pushd puts the directory on the top of the directory stack and changes directory to it. If a + n argument is given, pushd puts the nth directory stack entry ( nth previous working directory) on the top of the stack and changes directory to it. # # Sample C Shell Directory Management Functions # # Function fcd - verify accessibility before changing # to target directory function fcd { # Make sure directory exists if [[ ! -d $1 ]] then print "$1: No such file or directory" return 1 else # Make sure directory is searchable if [[ ! -x $1 ]] then print "$1: Permission denied" return 1 fi fi # Otherwise change directory to it cd $1 return 0 } # Function dirs - display directory stack function dirs { # Display current directory and directory stack print "$PWD ${DSTACK[*]}" } # Function pushd - add entry to directory stack function pushd { # Set stack depth (number of stack entries) integer DEPTH=${#DSTACK[*]} case $1 in "" )# No argument - switch top 2 stack elements if ((${#DSTACK[*]} < 1)) then print "$0: Only one stack entry." return 1 else fcd ${DSTACK[0]} || return DSTACK[0]=$OLDPWD dirs fi ;; +@([1-9])*([0-9])) # Number argument 1-999* - move entry to top # of directory stack and cd to it integer ENTRY=${1#+} if ((${#DSTACK[*]} < $ENTRY)) then print "$0: Directory stack not that deep" return 1 else fcd ${DSTACK[ENTRY-1]} || return DSTACK[ENTRY-1]=$OLDPWD dirs fi ;; * ) # Directory argument - verify argument # before changing directory and adjusting # rest of directory stack fcd $1 || return until ((DEPTH == 0)) do DSTACK[DEPTH]=${DSTACK[DEPTH-1]} ((DEPTH-=1)) done DSTACK[DEPTH]=$OLDPWD dirs ;; esac } # Function popd - remove entry from directory stack function popd { # Set stack depth (number of stack entries) integer i=0 DEPTH=${#DSTACK[*]} ENTRY=${1#+} case $1 in "" ) # No argument - discard top stack entry if ((${#DSTACK[*]} < 1)) then print "$0: Directory stack empty." return 1 else fcd ${DSTACK[0]} || return while ((i < (DEPTH-1))) do DSTACK[i]=${DSTACK[i+1]} ((i+=1)) done unset DSTACK[i] dirs fi ;; +@([1-9])*([0-9])) # Number argument 1-999* - discard nth # stack entry if ((${#DSTACK[*]} < ENTRY)) then print "$0: Directory stack not that deep" return 1 else while ((ENTRY < DEPTH)) do DSTACK[ENTRY-1]=${DSTACK[ENTRY]} ((ENTRY+=1)) done unset DSTACK[ENTRY-1] dirs fi ;; # Invalid argument given * ) print "$0: Invalid argument." return 1 ;; esac } |
|
|
|
| Korn Shell: Unix and Linux Programming Manual, Third Edition, TheBy Anatole Olczak
| Appendix C. C Shell Functionality |
Miscellaneous Commands The following sections contain Korn shell equivalents of some miscellaneous C shell commands and functions. The .logout File In the C shell, commands in the ~/.logout file are executed on exit . If the following command is added to the ~/.profile file, then the same thing will happen in the Korn shell: trap '. ~/.logout' EXIT The chdir Command The C shell chdir command changes to the specified directory and can be set to a Korn shell alias like this: |
|
|
Next page