BourneShell and shell programming (1)

Source: Internet
Author: User
Article title: BourneShell and shell programming (1 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Permission statement:
This article is a handout of the LINUX elective course of Dalian University of Technology. You are welcome to repost it, but this document is prohibited from any commercial or profit-making activities. Please retain this copyright statement when reprinting.
Author: he binwu, hbwork@dlut.edu.cn, Dalian University of Technology Network Center
/******** Sorry, I pasted *********/in code mode to ensure the format is not messy *********/
Source code :--------------------------------------------------------------------------------
------------------------------------------------------------------------------
Bourne Shell
Introduction: Bourne Shell basics and many other useful features, shell programming and organization.
Main content:
Basic Introduction to. shell, environment, options, and special characters
. Shell variables user-defined variables, environment variables, location variables (shell parameters)
. Shell script programming
Conditional testing, loop and repetitive control
. Shell customization
1. Basic shell knowledge
Developed by Stephen Bourne at the Bell Lab
Suggestion: Use man sh to View improvements or features on UNIX.
(1) shell prompt and its environment
/Etc/passwd file
Prompt: $
/Etc/profile $ HOME/. profile
(2) shell execution options
-N: Test the shell script syntax structure. only shell scripts are read but not executed.
-X: Enter the tracking mode to display each command executed for scheduling.
-A Tag all variables for export
-C "string" reads commands from strings
-E non-interactive mode
-F disable the shell file name generation function
-H locate and remember functions as defind
-I interaction mode
-K read command parameters from environment variables
-R restrictions
-S: Read commands from standard input
-T execute the command and exit (shell exits)
-If undefined variables are used in replacement, the u is incorrect.
-V verbose: displays the shell input line.
These options can be used together, but some of them obviously conflict with each other, such as-e and-I.
(3) restricted shell (Restircted Shell)
Sh-r or/bin/rsh
You cannot perform the following operations: cd, change PATH, specify full PATH name, and output redirection. Therefore, you can provide
Good control and security mechanisms. Generally, rsh is used for application users and dial-up users.
. The main directory of a restricted user cannot be written.
Insufficient: if the user can call sh, the rsh restriction will not work. In fact, if the user
The program calls the shell, and the rsh restrictions will no longer work.
(4) use set to change shell options
You can use the set command at the $ prompt to set or cancel shell options. Use-set options, + cancel the corresponding
Option, most UNIX systems allow a, e, f, h, k, n, u, v, and x to switch/cancel.
Set-xv
Start tracing method. all commands and replicas are displayed, and input is also displayed.
Set-tu
Disable the check for undefined variables during replacement.
Use echo $-to display all configured shell options.
(5) user startup file. profile
PATH = $ PATH:/usr/loacl/bin; export PATH
(6) shell environment variables
CDPATH is used to find the path of the cd command.
HOME/etc/passwd file
IFS Internal Field Separator. the default value is space, tab, and line feed.
Use MAIL/var/mail/$ USERNAME mail and other programs
PATH
PS1, PS2 default prompt ($) and line feed prompt (>)
TERM terminal type. Common types include vt100, ansi, vt200, and xterm.
Example: $ PS1 = "test:"; export PS1
Test: PS1 = "\ $"; export PS1
$ Echo $ MAIL
/Var/mail/username
(7) reserved characters and their meanings
$ Start of shell variable name, such as $ var
| Pipeline, which transfers the standard output to the standard input of the next command
# Start annotation
& Execute a process in the background
? Match one character
* Matches 0 to multiple characters (different from DOS, which can be used in the middle of the file name and contain .)
$-Use set and the flag passed to shell during execution
$! Process ID of the last sub-process
$ # Number of parameters passed to shell script
$ * Parameters passed to shell script
$ @ All parameters. some parameters are enclosed in double quotation marks.
$? Return code of the previous command
$0 name of the current shell
$ N (n: 1-) location parameter
$ Process Identifier Number (PID)
> File output redirection
   'Command' command replacement, such as filename = 'basename/usr/local/bin/tcsh'
> Fiile output redirection, append
Escape characters and single quotes:
$ Echo "$ HOME $ PATH"
/Home/hbwork/opt/kde/bin:/usr/local/bin:/usr/X11R6/bin:
$ Echo '$ HOME $ path'
$ HOME $ PATH
$ Echo \ $ HOME $ PATH
$ HOME/opt/kde/bin:/usr/local/bin:/usr/X11R6/bin:/home/hbw
Ork/bin
Others:
$ Dir = ls
$ Dir
$ Alias dir ls
$ Dir
Ls> filelist
Ls> filelist
Wc-l <filelist
Wc-l filelist
Sleep 5; echo 5 seconds reaches; ls-l
Ps ax | egrep inetd
Find/-name core-exec rm {}\;&
Filename = 'date "+ % Y % m % d" '. log
2. shell variable
Variable: represents the symbols of some values, such as $ HOME, cd command to find $ HOME, can be used in computer languages
Perform multiple operations and control.
The Bourne Shell has the following four variables:
. User-defined variables
. Location variable is a shell script parameter
. Pre-defined variables (special variables)
. Environment variables (refer to the shell customization section)
(1) user-defined variables (data storage)
$ COUNT = 1
$ NAME = "He Binwu"
Tip: because most UNIX commands use lower-case characters, full-case variables are usually used in shell programming,
Of course this is not mandatory, but using uppercase characters can easily identify variables in programming.
Call a variable: Add $
$ Echo $ HOME
/Home/hbwork
$ WEEK = Satur
$ Echo Today is $ WEEKday
Today is
$ Echo Today is $ {WEEK} day
Today is Saturday
Assign a value to a Shell variable from right to left (assign a value to Linux Shell/bash from left to right !)
$ X = $ Y y = Y
$ Echo $ X
Y
$ Z = z Y = $ Z
$ Echo $ Y
$
Use the unset command to delete the value assignment of a variable
$ Z = hello
$ Echo $ Z
Hello
$ Unset Z
$ Echo $ Z
$
Conditional command replacement
In the Bourne Shell, variable replacement can be executed under specific conditions, that is, conditional environment variable replacement.
This type of variable replacement is always enclosed in braces.
. Set the default value of the variable
The value is null before the variable is assigned a value. The Bourne Shell allows you to set the default value for the variable. Its format is as follows:
Below:
$ {Variable:-defaultvalue}
Example:
$ Echo Hello $ UNAME
Hello
$ Echo Hello $ {UNAME:-there}
Hello there
$ Echo $ UNAME # The variable value has not changed.
  
$ UNAME = hbwork
$ Echo Hello $ {UNAME:-there}
Hello hbwork
$
. Another case: change the variable value in the following format:
$ {Variable: = value}
Example:
$ Echo Hello $ UNAME
Hello
$ Echo Hello $ {UNAME: = there}
Hello there
$ Echo $ UNAME # The variable value has not changed.
There
$
Replace variables with commands
$ USERDIR =$ {$ MYDIR:-'pwd '}
. Replace $ {variable: + value} when the variable has been assigned a value}
. Conditional variable replacement with error check
$ {Variable :? Value}
Example:
$ UNAME =
$ Echo $ {UNAME :? "UNAME has not been set "}
UNAME: UNAME has not been set
$ Echo $ {UNAME :?}
UNAME: parameter null or not set
(2) location variable (Shell parameter)
In shell script, the location parameter can be represented by $1 .. $9. $0 indicates that the content is usually the name of the file of the currently executed program.
. Prevent the variable value from being replaced by readonly variable
. Use the export command to output the variable so that the variable is available to the sub-shell. when the shell executes the program, the shell will set a new environment for it to execute. this is called
Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.