Linux----->shell Advanced Programming----1

Source: Internet
Author: User

1. Background

the role of the shell is to explain the execution of the user's commands, the user enters a command, the shell interprets the execution of one, which is called Interactive (Interactive), and the shell has a way of executing the command called batch processing. The user writes a shell script in advance, with a number of commands that allow the shell to execute the commands at once without having to hit the command one at a time. Shell scripts are similar to programming languages, and there are variables and process control statements, but shell scripts are interpreted and do not need to be compiled, and the shell program reads and executes the commands from a single line of script, which is equivalent to a user knocking a line of commands from the script to the shell prompt for execution.


2. classification and history [ There are many kinds of shells on Unix systems for historical reasons]

    • SH (Bourne Shell): Developed by Steve Bourne, various Unix systems are equipped with SH.

    • CSH (c Shell): Developed by Bill Joy, with the BSD UNIX release, its process control statements are much like the C language and support many features that the Bourne Shell does not support: Job control, Command history, command-line editing.

    • Ksh (Korn Shell): Developed by David Korn, backwards compatible with SH's functionality, and added new features introduced by CSH, are the most current UNIX system standard configuration of the shell, on which/bin/sh is often a symbolic link to/bin/ksh.

    • TCSH (Tenex C Shell): is an enhanced version of the CSH, the introduction of command complement the same function, in the FreeBSD, MacOS X and other systems to replace the CSH.

    • Bash (Bourne Again shell): The shell developed by GNU, the main goal is to be consistent with the POSIX standard, while taking into account the compatibility of SH, bash from CSH and Ksh learn a lot of features, is a variety of Linux distribution standard configuration of the shell, /bin/sh on Linux systems are often symbolic links to/bin/bash. However, bash and SH are a lot different, on the one hand, bash expands some commands and parameters, on the other hand, bash is not completely compatible with SH, some behaviors are inconsistent, so bash needs to simulate the behavior of SH: when we start bash through the program name SH, Bash can pretend that it is sh, does not recognize the extended command, and behaves the same as SH.



3. Write the first script: [1.sh]

#!             /bin/bash #指定命令解释器shell CD. #切换到上一级目录 ls #显示当前目录下所有文件

# indicates a comment, and the interpreter does not interpret what follows the comment

#! Must be at the beginning, called (shebang), which indicates that the script uses the following specified interpreter/bin/bash to interpret the execution


4. Execute Shell scripting methods


      • Give the script back execute permissions such as: chmod a+x 1.sh then directly executable

      • Pass. 1.sh execution

      • Executed via source 1.sh

      • By specifying SH to perform such as:/bin/bash 1.sh or/bin/bin 1.sh


5. Basic Syntax


      • \ is used to remove the special meaning of the following single character (except carriage return)


      • Counting judgment

      • Send-part-judge

    • -eq equals

    • -ne Not equal to

    • -GT Greater than

    • -ge greater than or equal to

    • -lt less than

    • -le less than or equal to

    • -D to determine if the file is a directory

    • -F to determine if the file is a normal file

    • -P to determine if the file is a pipe file

    • -R to determine if the file has Read permission

    • -W to determine if the file has write permissions

    • -X to determine if the file has Execute permissions

    • -Z returns 0 if the file length is 0 (true)

    • -N returns 0 (true) if the file length is not 0

    • -A with such as [-P pipe-a $var 2 = "SADSA"] pipe is pipe and string is equal

    • -O or as [-P pipe-a $var 2 = "SADSA"] pipe is a pipeline or string is equal

    • ' Command ' to expand command such as sea= ' ls ' At this time sea is saving all the files in the current directory list

    • $ (command) above

    • * Match 0 or more of any characters

    • ? Match an arbitrary character

    • [several characters] match any one of the characters in parentheses and appear once

    • Variable classification

    • Variable use:

      Variable name definition variable name = variable value (cannot have spaces on either side of the equal sign) such as sea= "Hello world!"

      $ variable Name: Take the value of the variable such as sea= "Hello world!" echo $sea Output Hello world!

      To delete a defined variable: unset variable name such as Unset Sea

    • Built-in variables

    • Environment variable: The environment variable is passed to the child process by the parent process, which refers to the env environment variable of the current system

    • Local variables: variables defined within a script we collectively refer to as local variables, such as sea= "Hello wolrd!"

    • $? Last Command execution result

    • $$ Current Process PID

    • $ $ current Script file name

    • $[1-n] Script Parameters

    • [Email protected] all participating listings

    • $* Ibid.

    • Number of $# parameters

    • Variable:

    • Character wildcard

    • Command expand

    • Arithmetic:

    • Escape character

Simple Output Statement script:

#! /bin/sh#print hello world              #注释 #authors sea 2016/06/24        /bin/echo  "hello world!"      #打印hello  world/bin/pwd                      #输出当前路径/bin/ls                       #输出当前目录下所有文件/ bin/ls | wc                  #统计当前目录下所有文件/bin/echo  "Sea"                #输出sea/bin/ps aux                   #输出当前进程详细情况 

tip: From a programmer's point of view, the shell itself is a program written in C, and from the user's point of view, the shell is a bridge between the user and the Linux operating system. The user can either enter command execution or use shell scripting to do more complicated operations. In the increasingly perfect Linux GUI today, in the field of system management, shell programming still plays a role that can not be ignored. An in-depth understanding and proficiency in shell programming is one of the required lessons for every Linux user .

This article is from the "Sea" blog, be sure to keep this source http://lisea.blog.51cto.com/5491873/1792761

Linux----->shell Advanced Programming----1

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.