The 10th chapter: Build the basic Script _ Shell Script Daquan Second Edition

Source: Internet
Author: User
Tags integer division

first, the implementation

1, parallel execution of commands, multiple commands separated by semicolons

[Email protected] network-scripts]#Date;W.H.O.Tue May3  -: -: -Cst .Root Tty1 .- to-Geneva  on: +Root pts/0         .- to-Geneva  -: A(192.168.78.1)

2. The first line of the shell script indicates that bash is required to execute

#! /bin/bash

3. Shell scripts are executed in sequence

4. The single point operator represents the shell script that will be executed
[Email protected] network-scripts]#./test. SH
ii. display of information5. Use echo output information in the script
Echo " Hello World " Hello World

6. Echo-n indicates that the output information is on the same line,-n means that the newline character is ignored .

Echo "  "Hello World [[email protected] network-scripts]#
iii. use of variables7. Environment variables (also known as system variables, corresponding to user-defined user variables)
[Email protected] network-scripts]# setbash=/bin/bashbashopts=checkwinsize:cmdhist:expand_ Aliases:extquote:force_fignore:hostcomplete:interactive_comments:login_shell:progcomp:promptvars: Sourcepathbash_aliases=() bash_argc=() bash_argv=() bash_cmds=() Bash_lineno =() bash_source=() .....

8, reference variable, in the name of the variable before the $ symbol or ${variable name} way

Echo $USERroot
Echo ${user}root

9. User variables are case-sensitive

[Email protected] network-scripts]# val="Val"[[email protected] network- scripts]# val="val"[[email protected] networkecho  $valval [[Email Protected] Networkecho  $ValVal

10. When assigning variables, the "=" number cannot have spaces around

" 123 "-Bash:val:command not found[[email protected] network-scripts]# val="123"  [email protected] networkecho  $val123
11, the shell script will automatically determine that the variable is worth the data type 12, the reference variable to assign a value to the variable
[Email protected] network-scripts]# val="123"[[email protected] network  echo  $val123[[email protected] network-scripts]# val=$val [[Email protected ] Networkecho  $Val123

13, anti-quotation: ", the inverted quote is the command, return the command after the execution of the output content

[Email protected] network-scripts]# day= 'date +%d ' [[email protected] networkecho  $day  Geneva
iv. redirect input and output14. Save the output of a command to a file instead of the output redirection that is normally displayed on the display. > added; >> append;Commonly used in command output redirection to a file
[Email protected] network-scripts]#Date>Datefile.txt[[email protected] Network-scripts]#CatDatefile.txttue May3  the:Panax Notoginseng: $Cst .[[Email protected] network-scripts]#Echo "Hello worl">Datefile.txt[[email protected] Network-scripts]#CatDatefile.txthello worl[[email protected] Network-scripts]#Echo "lzq Hello">>Datefile.txt[[email protected] Network-scripts]#CatDatefile.txthello worllzq Hello

Enter the redirect,< file to get the data source from the file to the command execution; << Inline input redirection This way allows redirection of input data at the command line without having to be a file

WC <2  4
WC << EOF> test1> test2> test3>3  3
The EOF string is used to identify the beginning and end of the data for the redirected input(Example: FTP script)The second prompt is controlled by the PS2 variable, and PS1 is the main variable
Echo $PS 2>
Five, Pipeline15, the output of a command as input to a command, this is called Pipe connection (pinping), the command on both sides of the pipeline, after the first command produces output, immediately sent to the second command, the second after the completion of the output sent to the third .... The pipeline can be entered into a file with redirection
[Email protected] network-scripts]# Rpm-qa |Sort> RPM.Info[[Email protected] network-scripts]#CatRpm.Info| LessACL-2.2. the-6. el6.x86_64aic94xx-firmware- --2. El6.noarchalsa-lib-1.0. A-3. EL6.X86_64APR-1.3.9-5. EL6_2.X86_64APR-util-1.3.9-3. El6_0.1. X86_64ATK-1.30.0-1. El6.x86_64atmel-firmware-1.3-7. El6.noarch .....
Vi. Mathematical OperationsEnclose a mathematical representation using square brackets and a $ symbol ($[mathematical expression])
[[email protected] network-scripts]# var=$[17][[email protected] NetworkEcho $var 7

The Bash Shell supports integer division only, and if you need to operate floating-point numbers, you can use a z shell that supports floating-point arithmetic

[[email protected] network-scripts]# var=$[32][[email protected] NetworkEcho $var 1

BC Calculator is actually a programming language that allows you to enter floating-point expressions, interpret expressions, evaluate, and return results at the command line

[[Email protected] ~]# BCBC1.06. theCopyright1991-1994,1997,1998, -,2004,2006Free software Foundation, Inc.this is Freesoftware with absolutely NO WARRANTY. For details type ' warranty'. A/71 A //7(standard_in)2: Syntax error -/0.7 - Scale=2#该变量必须设置, controlling the number of decimal places A/71.71quit

-Q option to block welcome messages

[[email protected] ~]# bc-q #加上-q option is to block welcome informationScale =2 /71.71

Define variables within the BC and apply them, which can only be used in BC

val=0.6 / val20.00

How to use the expression as output in the script to the BC calculator

Echo " val=0.5, scale=2, 12/val " | BC 24.00

Inline redirection combined with BC calculator (BC Calculator only recognizes input redirection)

[[email protected] ~]# val= ' BC << EOF> scale=48>Echo  $val1.5000
Vii. Exit ScriptEach command running in the shell is told to the shell with an exit status code that it finishes processing to view the exit status code ($?).
Date Tue   May 3 +: echo $? 0

Linux Exit Status Code

Status code Describe
0 Command completed successfully
1 Generic Unknown error
2 Misuse of SHELL commands
126 Command not executable (no permissions)
127 No command found.
128 Invalid exit parameters
128+x Critical error for Linux signal x
130 Command terminated with CTRL + C
255 Exit status code out of bounds

Exit command to specify exiting status code

[Email protected] ~]# vim exitstatus.SH[[Email protected]~]#chmod 755Exitstatus.SH[[Email protected]~]#./exitstatus.SHHello World[[email protected]~]#Echo$?5[[Email protected]~]#CatExitstatus.SH#! /bin/BashEcho "Hello World"Exit5

The 10th chapter: Build the basic Script _ Shell Script Daquan Second Edition

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.