Example: bachelor of science

KSH(1) (CheatSheet) KSH(1)

KSH(1) ( cheatsheet ) KSH(1) GeneralIn the following group, the filename may be replaced with the value of a variable, if desired, as longas the rules in the VARIABLES section are redirection mechanisms can be applied to complexshell commands as a whole if the shell com-mands are placed within parenthesis, > filels -l >> filewho >> fileis the same as(date; ls -l; who ) > filewhich runs the commands in a subshell (causesfork()), or{date; ls -l; who; } > filewhich runs the commands within the current shell (nofork())<fileRead input from the givenfile instead of from the keyboard, ie."redirect stdin fromfile".>fileWrite output to the givenfile instead of to the screen, ie."redirect stdout tofile".2>fileWrite errors to the givenfile instead of to the screen, ie.

KSH(1) (CheatSheet) KSH(1) General In the following group, the filename may be replaced with the value of a variable, if desired, as long as the rules in the VARIABLES section are followed.

Tags:

  Cheatsheet

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of KSH(1) (CheatSheet) KSH(1)

1 KSH(1) ( cheatsheet ) KSH(1) GeneralIn the following group, the filename may be replaced with the value of a variable, if desired, as longas the rules in the VARIABLES section are redirection mechanisms can be applied to complexshell commands as a whole if the shell com-mands are placed within parenthesis, > filels -l >> filewho >> fileis the same as(date; ls -l; who ) > filewhich runs the commands in a subshell (causesfork()), or{date; ls -l; who; } > filewhich runs the commands within the current shell (nofork())<fileRead input from the givenfile instead of from the keyboard, ie."redirect stdin fromfile".>fileWrite output to the givenfile instead of to the screen, ie."redirect stdout tofile".2>fileWrite errors to the givenfile instead of to the screen, ie.

2 "redirect stderr tofile".2>&1 Send stderr to the same place that stdout is copies the file descriptor from stdout to stderrinstead of open ing stderr again, so the twodata streams share the same buffer instead of each havingit sown Quotes (stands for DO dollar signs)Allowvariable substitution,but prevent anyother interpretation, such as I/O redirection, wildcards, Quotes (stands for SUPPRESS dollar signs)Preventanyinterpretation of the contents.\The backslash does the same thing as single quotes, but only for the immediately following are used to contain values that may change during the execution of the ,these are filenames, data read from the keyboard or a data file, calculated values, or may contain spaces, tabs, or other characters considered to be delimiters by the shell, soanytime you want to use the value of a variable you should place double quotes around value of variables can be modified before being used if the variable name is enclosed withinbraces ("{"and "}") and the appropriate modifier characters are used:${var-string}Use the value ofvarunless it sempty; then usestring.

3 ${var=string}Use the value ofvarunless it sempty; then assignstringtovarand use (1) ( cheatsheet ) KSH(1)${var?string}Use the value ofvarunless it sempty; then printstringas an error message end terminate theshell.${var+string}Use the value ofvarunless it sNOTempty; then usestring.${var#wildpat}Use the value ofvarafter removing anycharacters from the front of the value which match thewildcard characters matched by the pattern are the shortest possiblestring.${var##wildpat}Same as above,but the match is the longest possible string.${var%wildpat}Same as#,but this one trims from the end of the string instead of the beginning. Itmatches theshortest possible string.${var%%wildpat}Same as above,but the match is the longest possible string.

4 $$ Theprocess id of the is usually used for temporary filenames, such as/tmp/$0.$$.$! Theprocess id of the last job to be executed in the background.$0 Nameof the shell script that is may include a pathname, so it would be more appropriateto usebasename,orasimilar construct, to remove the directory component before using it in an out-put statement.#!/bin/kshPROG=$(basename $0)USAGE="Usage: $PROG fromfile tofile .."or#!/bin/kshPROG=${0##*/}USAGE="Usage : $PROG fromfile tofile .."$1-$nThese represent the command line parameters provided by the user of the script when it wasexecuted.$# Thenumber of command line parameters."$@"The values of the command line parameters with double quotes around individual last three variables, $n,$#, and $@, are specific shortcuts of the general technique of accessingelements of an array:${array[n]}This accesses thenth element ofarray.

5 ${array[@]}This accesses all of the elements ofarray.${#array[@]}This provides the number of elements (1) ( cheatsheet ) KSH(1) Always put double quotes around variable asif [[ -f "$dir/$1" -o -d "$dir/$1" ]]This rules prevents the shell from interpreting anyspaces that might appear within the value of a vari-able (obviously,ifavariablecannotcontain spaces, tabs, or newlines, then you needn tuse doublequotes an example would be$$or$#). Italso prevents errors when a variable is empty and thevalue is used in a location where a value is common inside [[ and ]] and whenchanging the command line parameter list viaset use (( and )) for numeric asif (( count < high ))Always use [[ and ]] for string and file as[[ "$var1" = "$var2" -o -r "$file" ]]Neverput spaces around the equal sign in variable astypeset -i count=10,orprogname="$0"Always put spaces everywhere else!

6 Such as around the fields within [[ and ]] or (( and )).Always put spaces on both sides of commandnames andkeywordssuch asif,then,else,elif,fi,while,for,select, do,done,case,in,esac,let,andanyother built-in or regular (external) commands.


Related search queries