|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Home Genealogie Hemochromatose Certificaat Test Manuals Korn shell SQL commands Robots.txt Encoding SSL setup WSDL definition XSLT scrips |
Table of contents1 - SortUnix has standard sorting functionality. The command sort has all sorting functionality you wish.1.1 - SyntaxSort can be used in 2 ways, first as single command. Second as piped command. Output will be displayed or piped to standard output if no output file is specified.sort [-options] [-o outputfile] inputfile cat inputfile | sort [-options] > outputfileMost common used options are listed in below. If no input file is specified, sort assumes standard input. 1.2 - Ascending/descendingBy default sort sorts a file ascending based on the ASCII value. To sort descending the option -r must be used.sort -r inputfile 1.3 - Numeric sortSort will sort based on the ASCII value of a line beginning with the first character. So 10 becomes before 9 because the value of the first character of 10 is 1, and 1 comes before 9. To sort on whole numbers use the -n option.sort -n inputfile 1.4 - Suppress equal linesDuplicate lines will also be sorted and displayed. To print only 1 line in case of one or more duplicates, use the option -u.sort -u inputfile 1.5 - Sort on columnsBy default sort sorts based on the whole line. To sort on columns use the -k option. The default field separator is a space or tab. This default can be changed by the -t option.sort -t ';' -k 4 inputfileThe above command will sort the input file by column 4 (first column is 1). The fields are separated by a ; (semi-column). Use the quotes when using special characters. To numeric sort a column the -k and -n option may be combined to e.g. -k 4n. 2 - VariablesLike other program languages, Unix uses variables. Use the command env to display the environment variables.It is not necessary to declare the variables in Unix like in other languages. Variables can be filled with almost anything, however there are some special Unix characters. E.g., if you want to insert a * in a variable, Unix handles the * the same way as in ls * (places the filenames of a directory at the place of the *). To use the real character quotes must be used. Some examples are placed below: number=1 word=test startuppath="/usr/scripts/testscript.sh"The first variable 'number' will be filled with the number '1'. The second variable will be filled with 'test', and the third variable will be filled with the path. To use the / (slash) in the last variable, quotes are used to tell Unix not to treat the slash as a special character. There is no space around the '=' character in the above example. Do not put spaces here, or Unix will think the variable is a command. To display a variable, a $(dollar) sign must be placed before the variable. Some Unix versions use the following syntax to display a variable: ${number}. The Unix shell replaces the name of the variable by the contents of the variable. This way variable can be used in almost any situation. Variables may also be used in a Unix command string, or even as a Unix command itself. Some examples: echo This is the startuppath ${startuppath}
cp ${word} /tmp/${word}
By entering options to a script, variables can be filled before starting the script.
Options must be separated by a space. The options will be put in a numbered variable.
The display the first option ad the command echo ${1}. This prints the value of the
first option-variable. The second option variable is ${2} etc.Variables can only be used within the started shell. If a script is executed, it's started in its own shell. This means that the variable will be lost when the script ends. It is however possible to export a variable so it will be saved when the script ends. The command to do this is export. var1=test export var1 export var2=100 3 - Special VariablesUnix uses a number of special variables which contain information about the system and its processes. This information can be used in scripts by calling these variable.3.1 - Process variablesThe following variables contain information of the started process (or command). These variable are read-only.
myscript.ksh option1 option2The first positional parameter $1 contains the string "option1", the second positional parameter contains "option2". The string $* contains "option1 option2". Exit codes can be generated in scripts by using the return command. A return 1 will result in a exit code 1. 3.2 - System variablesSystem variables contain information about the system.
4 - Conditional expressionsBy creating conditions it is possible to use loops in scripts. If a condition is true, a command is executed or not.In this chapter the most common options are explained. There are a lot more options in Unix, and even more option is some specific Unix versions. In the Unix man pages (man ksh) all options are listed and explained. 4.3 - String conditionsString conditions are used to control a script based on the value of a variable. One or more variables can be checked on its contents based on value or kind of information.
if [-n ${var1}] then echo good
The use of loops is explained in a next chapter.
4.4 - Numeral conditionsString conditions compare the values of the string based on the ASCII value of the string. To use the numeral value of the sting the options below must be used.
4.5 - File conditionsSometimes it is necessary to manipulate a script based on a file of directory. For instance if a file exists or not.
4.6 - Expression conditionsTo combine expressions some logical commands can be used.
if [[ -n ${var1} && ${var2} = ${var3} ]]
The condition is true if var1 is not empty and var2 and var3 are equal.
5 - LoopsScripts can be controlled by loops, loops are manipulated by conditions. If a condition is true execute a part of the script, or while a conditions is true etc.Loops can be cascaded, this means that other loops may be programmed within other loops. It is however recommended to use tabs like in the examples or your program will be unreadable. In this chapter only the most common used loops and there options are explained. 5.5 - If-then loopThe most simple and common used loop is the if-then loop. If an expression is true, execute the command(s). An extra option is the else option. If an expression is not true execute the command(s) after else. The loop must be closed by the fi command.if expression1
then
function1
elif expression2
then
function2
else
function3
fi
If expression1 is true then execute function1. If expression1 is not true but expression2 is true,
execute function2. If both expression1 and expression2 are not true execute function3.
5.6 - For loopFor loops are used to execute a command with a range of entry's.for identifier [in word...]
do
function
done
The command executes the function for all values in word. If combined with Unix commands a
powerful loop is generated. Look at the example chapter for a more detailed description.The for loop must always be closed by done. 5.7 - Select loopThe select loop is the interactive version of the for loop. The select command displays all values in word. The user can select the values by entering the line number. The loop will than execute the function with the selected value.select identifier [in word...]
do
function
done
The select loop must also be closed by done.
5.8 - While loopWhile loops will execute the function as long the expression is true.while expression
do
function
done
Also the while loop must be closed by done.
5.9 - Until loopThis is the opposite of the while loop. The function will be executed until the expression is true.until expression
do
function
done
Again close the loop with done.
5.10 - Case loopWith the case loop it is possible to manipulate a script based on the users choice. It's also possible to use this loop with a variable. The case loop executes a function based on the value of the identifier (variable).case identifier in
word) function
;;
esac
The case loop will execute a function and then returns to the beginning of the loop.
The cases must be closed by ;; and the loop is closed by esac. To really exit the
loop the exit command must be used.The example below is a standard yes/no question: case ${i} in
y) echo "Continue"
;;
n) echo "Ok, you out of here"
exit
;;
*) echo "Illegal choice"
;;
esac
The loop asks the user to enter a key that is used to fill variable i (only cursor on screen).
If the value is y, the message Continue is printed. If the value is n, the loop is stopped.
All other choices display the last message. Only by entering a 'n' the loop is stopped.The case loop can only be stopped by the command exit in the script or a ctrl-c (if not disabled!). 6 - ExamplesIn this chapter some part of scripts that I use myself are displayed. The scripts are solutions for common problems. Scripts I get from other people are marked by the name of the person who provided the script.I regularly update this page since I don't know everything yet... 6.3 - Read fullname in passwd fileSometimes we want to know the full name of a user based on the login code of that user. The next example displays the full name belonging to the login code.awk -F":" '{if($1 == "'${login}'"){print $1 " " $8}}' /etc/passwd
The variable $login must be filled with the login code of the user.
6.4 - Check if logincode exists in listfileFor maintenance scripts, it is sometimes necessary to exclude some login codes (like root). The easiest way to do this is generate a list of login codes which must be excluded. Than let the script read the list of login codes.if [ -z "`cat loginlist | grep ${login}`" ]
then
commando...
fi
If the login code exists in the login list file, the expression is filled and the condition
will become not true. The command is skipped for that user.
6.5 - Wait for a variable in a fileTo start scripts if one or more previous scripts are ready, I use the following script. Because it is not possible to export variable to other users. A temporary file must be used.By example, let's take 3 scripts. Script 3 is started if the first 2 are ready. Before all 3 scripts are started the file var must be filled with the number 2. The first 2 scripts must end with the following commands. count=`cat var`
count=`expr ${count} - 1`
echo ${count} > var
The 3rd command writes the value of count back to file var. If both scripts are ready the
value of file var will be 0.The next part must be entered at the start of the 3 script. count=`cat var`
while [ "${count}" -ne "0" ]
do
sleep 60
count=`cat var`
done
Read the value of file var. While var is not equal to 0, wait until it is.
6.6 - Read lines from a fileI have found 3 ways to read a file line by line. If you find other ways to do this, please email them.The fist example is the most simple one. But the file can't contain any spaces or tabs. The Unix script will use the space, tab and new line character as field separator. for rec in `cat datafile`
do
echo ${rec}
done
The next one is a more complex example. It fist converts all spaces to pipes.
So the file doesn't contain any spaces anymore.
for rec in `cat datafile | sed -e 's/ */|/g`
do
echo ${rec}
done
The only disadvantage of this example is that it changes the lines of the file.
The last example doesn't change the lines of the files and reads the whole line at once.
Only the performance is real bad, for every line the file must be opened again.row=0
for i in `cat datafile`
do
row=`expr ${row} + 1`
rec=`tail -${row} file | head -1`
echo ${rec}
done
I use this script only for small files, and if there is no other alternative.
7 - Vi basic commandsThis chapter contains the basic vi commands. Most commands can be combined to make more complex commands.7.1 - Cursor movementThese commands are used for cursor movement.
7.2 - TextmodeVi starts in command mode. To enter text the mode has to be changed to textmode. There are 4 ways to change to textmode. To leave textmode press [ESC].
7.3 - File managementTo load or save files use the following commands. To use these commands, enter the command line mode. In command mode enter : (semicolon).
7.4 - Yanking (buffer)By default yank uses the undo buffer. Yank can also use named buffers. Buffers can be named by one alphabetic character. (e.g. ayy Yank current line to buffer a, to insert the line use ap).
|
Amé Schaake Senna Schaake
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| © Christiaan Schaake | Laatste update January 16 2011 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||