The example explains how to use ksh in Linux-general Linux technology-Linux programming and kernel information. The following is a detailed description. Anyone familiar with Linux must know bash. It is the default command line interpreter in Linux. Users familiar with Solaris should also know csh, but csh is difficult to use, since the Korn Shell (http://www.kornshell.com) is generated, ksh combines the strong csh
As a loyal Linuxer, You have to master this powerful tool with great functionality and ease of use of bash!
There are a lot of materials on shell usage and shell programming, but it seems that there are few ksh specific explanations. This is also a combination of several E
I would like to add some of my best practices and try my best to explain ksh to you. I hope it will play a role for you.
1. Preparations
First, make sure that the ksh program has been installed in the/bin/directory. A few days ago, hybriddb installed Fedora7, but no ksh was installed. If not
Go directly to the ksh official website, and rename it to ksh and copy it to the/bin directory.
Like most script interpreters, you can choose to run interactive or batch processing. If it is interactive, enter # ksh at the bash prompt and enter the ksh interactive mode. If it is batch processing, then:
Create a ksh script file: # vi myscript. ksh
Then add the script code to the file, run Esc +: + wq to save it, And then grant the executable permission: # chmod 777 myscript. ksh
Finally, run the command: #./myscript. ksh or # ksh./myscript. ksh. If you need a parameter, run the command directly.
Enter # ksh./myscript. ksh arg1 arg2 arg3...
Of course, these parameters can be called directly in the program body using $1, $2, $3, especially: $0 always indicates the script file name, because it
It is actually the first parameter (the first parameter after the ksh command is always the script file name)
Note: To ensure that 100% of the batch processing file is interpreted by ksh, it is best to add
#! /Bin/ksh (this line indicates that ksh is used to explain the following program body. It is a special method of remark #!)
2. Variables
How to define variables: myval = "hello world"
Call: echo $ myval, and output: hello world
To perform numeric operations, we must explicitly declare the variable type, for example:
Integer val1 = 1
Integer val2 = 2
Integer val3 = $ val1 + $ val2
Print $ val3
If the program does not explicitly specify the type, the output will be 1 + 2 instead of 3.
Note: general remark method: # my comment
Line feed: \ n
Note: ksh depends on the size, and all Linux programs seem to be case sensitive. This is not confirmed by the hybriddb for MySQL.
3. Process Control
If... else... fi statement
If (ANIMAL = "cat") then
Print "Good"
Else
Print "Bad"
Fi
Judgment condition:
= Equal
> Greater
<Less
> = Greater than or equal
<= Less than or equal
! = Not equal
Case... in... esac Statement (wildcards can be used in condition judgment)
Echo input yes or no
Read answer
Case $ answer in
Yes | Yes | y)
Echo got a positive answer ;;
No)
Echo got a 'no ';;
Q * | Q *)
Exit ;;
*)
;;
Esac
While... do... done statement
Keeplooping = 1;
While [[$ keeplooping-eq 1]; do
Read quitnow
If [["$ quitnow" = "yes"]; then
Keeplooping = 0
Fi
If [["$ quitnow" = "q"]; then
Break;
Fi
Done
Util... do... done... statement
Until [[[$ stopnow-eq 1]; do
Echo just run this once
Stopnow = 1;
Echo we shoshould not be here again.
Done
For... do... done statement
List = "one two three"
For var in $ list; do
Echo $ var
List = "nolist"
Done
PWD-Current Directory
RANDOM-generate a RANDOM number
$-Output current process number
PPID-your process ID
$1 to $9-Parameter
$? -Exit code
$ REPLY-select items from the number menu
6. Functions
Printmessage (){
Echo "Hello, this is the printmessage function"
}
Printmessage
Built-in functions:
Read varname -- read standard input from the terminal and assign it to varname
Set $ varname -- set the value for the parameter. If set $ val = "first second third" is called, $1 = "first"
Eval -- directly execute the command, for example, eval 'print hello'. Then directly execute the command print hello.
Text Location, color adjustment-usage:
Tput init
Tput clear
Tput cup 3 2
Print-n "Here is a clean screen, with these words near the top"
Endline = 'tput cols'
Tput cup $ ($ endline-2 ))
Print "and now, back to you"
Sleep 2
Build your own number menu options:
Select word in one two three exit; do
Echo word is $ word
Echo reply is $ REPLY
If [["$ word" = "exit"]; then
Break;
Fi
Done
Run this script and wait for the user to make a selection.
1) one
2) two
3) three
4) exit
#?
7. Call other Linux system tools in Ksh
Such as cut, join, comm, fmt, grep, egrep, sed, awk
We are now studying awk, a powerful text analysis and processing tool. I will attach my research experience to it later.
8. Differences between exit and return
Exit exits the entire program body, while return only exits the current function body, not the program body, and has a return value.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service