Shell Basics and Programming specifications

Source: Internet
Author: User

One, Shell Environment View 1.1 View system shell support
[Email protected] ~]# cat/etc/shells/bin/sh/bin/bash/sbin/nologin/ usr/bin/sh/usr/bin/bash/usr/sbin/nologin
1.2 Viewing the current system default shell

Method One

Echo $SHELL/bin/bash

Method Two

[[email protected] ~]# grep root/etc/passwdroot:x:0:0: Root:/root:/bin/bash

Tip: The back/bin/bash is the shell interpreter after the user logs in.

Ii. creation and execution of Shell scripts 2.1 script open (first line)

A canonical shell script indicates by which program (interpreter) to execute the contents of the script in the first line, which is typically programmed in Linux bash

#!/bin/bash or #!/bin/sh #<---255 characters or less

Which, at the beginning of the "#!" The character is also called the magic number, and when executing the bash script, the kernel will follow the "#!" The following interpreter confirms which program interprets the contents of this script (note that this line must be the first line at the top of each script)

The difference between 2.2 bash and SH

SH for bash Soft connect

Ll/bin/sh14  6   /bin/sh , bash
2.3 Viewing the system version
cat/etc/redhat- Release 7.2. 1511

View Bash versions

 [[email protected] ~]# bash-- version  GNU Bash, version  4.2 . 46  (1 )-release (X86_64-redhat-linux-gnu) Copyright (C)  2011   free software Foundation, Inc.license GPLv3  +: GNU GPL version 3  or later // GNU.ORG/LICENSES/GPL.HTML>  this is  free  software; You is free   to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.  

Test if Bash has a shell hole and if you return to be careful, you need to upgrade bash as soon as possible.

[[Email protected]]#Bash-- versionGNU bash, version 4.1.2 (1)-release (x86_64-redhat-linux- GNU) Copyright (C) theFree software Foundation, Inc.license GPLv3+: GNU GPL version3or later //gnu.org/licenses/gpl.html> This is FreeSoftware; You are FreeTo change and redistribute it. There is NO WARRANTY, to the extent permitted by law. [[Email protected]]#] [email protected]]#env x= ' () {:;}; echo Be careful ' bash-c "echo this is a test"Be  CarefulThis is a test
2.4 Upgrade Bash method
Yum-  y update bash   -  QA Bashbash-4.1. 2-() env x= ' () {:;}; echo Be careful ' bash-c "echo the is a test"this isa Test  #再次执行已没有be Careful tips

Tip: If you do not have output be careful, you do not need to upgrade

2.5 Script Comments

In a shell script, followed by a comment on the back of the #, when developing the script, if there is no comment, then other people on the team will be difficult to understand the use of overtime corresponding content, and long time you will forget. In order to facilitate others and facilitate themselves, to avoid affecting the effectiveness of the team's assistance,

Develop a habit of writing off notes. Script comments Try not to use Chinese, avoid garbled problems

2.6 Execution of shell scripts

When the shell script runs, it looks for the system environment variable env, the change amount specified the environment file, the load smooth traffic is/etc/profile---> ~/.bash_profile---> ~/.BASHRC--->/etc/ BASHRC and so on, after loading the above environment variable file, the shell begins to execute the contents of the script

Typically, when you execute a shell script, a new process is requested from the system to execute script commands and child shell scripts in the process.

Tip: When setting up the Crond task, it is best to re-define the system environment variables in the scheduled task script, otherwise, some system environment variables will not be loaded, this problem needs attention!

The execution of a shell script can take several ways:

1). Bash script name or SH script name (recommended)

Bash test01.sh Welcome warren[[email protected]sh test01.sh  Welcome Warren

2). Use the absolute path of the script to execute or relative to the path./script name This requires a file execution permission

chmod +x test01.sh [email protected]/root/test01.shWelcome warren[[email protected]-node1 ~]# . test01.shWelcome Warren

3). source script name or. Foot Name

Source test01.sh Welcome warren[[email protected]. test01.sh  Welcome Warren

4) Sh < script name or cat script name |sh

Echo " Welcome Warren " [[email protected]sh < test01.sh  Welcome warren[[email protected]cat test01.sh |  bashWelcome Warren

Creating a script file using cat

Cat > Test02.sh Echo " This is test "

Ctrl+d end Edit, here as an extended knowledge of cat usage

The parent shell does not inherit the environment variables of the child shell and is tested as follows

Cat test.sh user001= 'whoami' [[email protected]echo   $user 001#空 [[Email Protected]

Use Source to import sub-script environment variables into the current environment to get the variables of the sub-script

source  test.sh  [[email protected]echo   $user 001Root
Three, the basic specification of Shell script development

The development specifications and habits of Shell scripts are very important, with good specifications can greatly improve the development efficiency, and can later reduce the maintenance cost of the script.

1). The first line of the shell script is the specified script profiler

#!/bin/bash

2). Shell script opening will be overtime table, copyright and other information

#!/bin/-5#Author: Created by warren#blog:http://  www.cnblogs.com/xiewenming/function is ... #Version:1.1

You can modify the "~/.VIMRC" configuration file configuration to automatically add the above information when vim edits the file

3). Try not to use Chinese in the shell script, to prevent the confusion of Chinese characters after switching the system environment. If you do not want to add Chinese, please adjust the character set according to the system.

such as: Export lang= "ZH_CN. UTF-8 ", and in the script, redefine the character set settings, and the system is guaranteed to be consistent.

4). The shell script should be named with an. sh extension

Example: test.sh

5). Shell scripts should be stored in a fixed directory

Example:/opt/scripts

Four, the good habit of shell script writing

1). The alignment should be written out as much as possible, and then the backspace will be added in conformance to prevent omission.

These pairs of matches include:

{}  []    "   " "

2). At least one space at each end of the bracket, 2 squares First, then one square, and the double brackets.

3). Corresponding to the Process Control statement, you should finish the format once, then add the content.

For example, the one-time completion of the IF statement format, should be

if condition Content    Then     content fi

Once the FOR Loop statement format is completed, it should be

 for  Do   content done 

Once the white statement format is completed, it should be

White Condition  Do   content done 

Tip: Until and case statements are the same

4). Make code easier to read by indenting

5). The value of the string definition variable that corresponds to the regular variable should be double-quoted, and there must be no spaces before and after the equals sign

6). The single and double quotation marks in the script must be in the English State in accordance with the

7). The variable name should have a relevant meaning, not too casual.

Description: Good habits allow us to avoid a lot of unnecessary trouble and suggest productivity

Shell Basics and Programming specifications

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.