BSH is the user's default shell in Sco Open Server, as well as many other OS support. In addition, since KSH is an extension of BSH, almost all BSH scripts can be run in KSH. So it is necessary to know about BSH.
In the SCO Open Server, the bsh command is located / bin / sh, which may be called / bin / bsh in some systems.
SHELL start and environment
1. Options at the shell startup
-A outputs all variables
-c "string" reads commands from String
-e uses non-interactive mode
-f prohibits the Shell file name
-H definition
-i interactive mode
-k is the execution setting option for the command
-n reads command but does not execute
-r limited mode
-s command reads from standard input
-T Execute a command, then exit the shell
-u When replacing, use unset variables will be wrong.
-v Shows the input line of the shell
-x trace mode, display execution command
The above unlicensed options can be used in combination.
2. Execute SH -R or / BIN / RSH to enter the restricted mode. In limited mode,
Users cannot change their directory, can not modify the path variable, can not use absolute path name
The execution command cannot be redirected.
3. Use the set to change the shell option.
SET -X is plus this option
Set X is removed
4. User shell Start the file to .profile, you can add environment variables and scripts in this file.
5. Shell environment variable
Search path used by cdpath cd command
Domain separators within IFS, generally spaces, tabs, or newline
PS1 master command prompt, default $
PS2 defaults from the command prompt>
Special characters and their meaning
1. Special characters used by Shell variable names
$ # Parameters for the command shell
$ - Provides an option when shell starts or uses the set command
$? The value returned after the last command executes
$$ Current Shell Process Number
$! Process number of the last child
$ @ All parameters, each is enclosed in brackets.
$ * All parameters, enclose with double brackets.
The $ n position parameter value, n represents the location. $ 0 is the current shell script name, $ 1 is the first parameter, ...
2. Wildcard
LS [! M] * Looking for file names not starting with M
3. Special characters or programs control usage
`` Command replacement, redirect the output of a command as the input of another command. (Note: It is the symbol opposite the single quotes `)
4. Use dual quotation marks, initially in the form of variables can be referenced
Using / escape, indicating that the following characters do not have special meanings or not the SHELL function.
shell variable
9.2.1 User-defined variables
$ count = 1
$ x = 20 $ y = aaaa
When the variable is not assigned, the shell does not report an error, but gives an null value for the variable.
9.2.2 Condition variable replacement
$ {count: -2} meaning: Quote COUNT variable, if the variable is not set up, it gives it a default 2
$ {count:? message} meaning: Quote COUNT variable, if the variable is not set in front, Show Message
9.2.3 Readonly Var Sets VAR to read-only variables
The set variables are role in the sub-shell, which means a local variable. Use the export command to make the variables are identified by other child shells.
Calculation and condition cycle structure
9.3.6 Arithmetic operation for shell variables
Expr Integer Operator Integer
Operator is: - * /% of which * To use / * to escape
No parentheses are prioritized, so you need to use `` hierarchical operations: EXPR `EXPR 5 7` / 39.3.8 Conditions:
IF command_1
THEN
Command_2
Command_3
Else
Command_4
COMMAND_5
Fi
IF Command
THEN
Command
Elif Command
THEN
Command
Elif Command
THEN
Command
Fi
Test command condition test:
Test Condition
Condition: Test string: str1 = str2 has the same content, the same length is true
STR1! = STR2
-N STR1 string length> 0 is true
-z str1 string length = 0 is true (empty string)
STR1 is true
Test the relationship between the two integers
INT1-EQ INT2 =
INT1-NE INT2! =
INT1 -GT INT2>
INT1 -GE INT2> =
INT1 -LT INT2 <
INT1 -LE INT2 <=
Test file
-r file user can be read
-w File user can write
-x file user can execute
-f file is a formal file
-d file is a directory file
-c file is a character special equipment file
-B file is a block special equipment file
-s file is not 0 byte file
-t file When the device developed by the file descriptor is a terminal
And OR combination multiple conditions
Interactive reading data
Read var1 Var2 Var3
CASE statement: Pattern | or OR connection different mode
Case "$ VAR" in
Pattern1)
Command
Command ;;
Pattern2)
Command
Command ;;
*)
Command
Command ;;
ESAC
Empty command
:
loop statement:
While Condition
DO
Command
Command
DONE
Until Condition
DO
Command
Command
DONE
The shift command moves the parameter variable forward
For var in arg1 arg2 arg3 ...
DO
Command1
Command2
DONE
VAR in the FOR cycle Value ARG1, Arg2, Arg3 in times for cycles
Break, Continue can be used in the loop
TRAP
Command1 && command2 is: only Command2 is executed only when Command1 is successful.
Command1 || comand2 means: only Command2 is executed only if Command1 failed