From: http://blog.csdn.net/xyp84/article/details/4435899
Today, I wrote a shell script, which runs normally on my machine. To my colleagues, run the syntax error near unexpected token error. I can check the shell script on the left and right, but there is no way to search by Google, I found that a senior man spoke quite well. The content is as follows:
Run Command vi-B to open your
this.
Be sure to keep up with then after 5.if. SimilarlyBe sure to keep up with then after Elif.Otherwise prompt for error message:Syntax error near unexpected token else
1 if statement followed by then, and preceded by a semicolon;2 space is very important, the shell will think that the space before a command, if A=3 think is an assignment operation, if written a = 3, then think of a as a command this= ' ls-l |grep ' ^-' | Wc-l '3 The operator sho
Specify an array for a characterHow to display an arrayA=1echo $a1A= (1234) echo $a1echo ${a[@]} 1 2 3 4echo ${a[*]}1234Specifies the number of digits in the display arrayecho ${a[0]}1echo ${a[2]}3echo ${a[3]}4Echo ${a[4]} spacesAdd arraya[4]=9echo ${a[4]}9Echo ${a[*]}1 2 3) 4 9To change an element in an arraya[2]=7echo ${a[*]}12749Get the number of elements in an arrayecho ${#a [@]} 5Sort the 10 arrays to a random 0-9 composition for inch 0 9 Do ' s//\n/g ' |sort-n17594489614294281134415643216
Copy Code code as follows:
#-------------------the wrong wording-------------------
RM-RF ${path}/*
#----------------------------------------------
#-------------------the right wording-------------------
If [-D "${path}"]
Then
RM-RF ${path}/*
Fi
#----------------------------------------------
7. Use of double brackets [[]]
The double middle bracket of the shell has many functions
Copy Code code as
use external program files to run.(1) such as exit and CD are built-in commands$type exit$type CDSome commands have multiple implementations, such as PWD and Echo, which have both external and built-in commands#type-a EchoThe which only displays external command files.Type-a shows two implementations of each command.Tip: For multiple implementations of the command, the default execution is built-in, if you want to execute an external command can specify the corresponding file is good.Like what$
Process state variables
1, $$ get the current shell process number (PID)2, $! executes the PID of the previous instruction3, $? Gets the return value that executes the previous command (0 is a success, Non-zero is a failure, this is very common)4. $_ the last parameter of a command or script executed before this
Copy Code code as follows:
Cat> test$.sh
Echo ' $$= ' $$
Echo ' $!= ' $!
Echo ' $?= ' $?
Echo ' $@= ' $@
Echo ' $
1. Read command
The read command reads information through a keyboard or a line of text in a file and assigns a value to a variable.
1.1, read reading the keyboard data, the input of the content before the return of the data assigned to a variable:
cb@standalone14:~$ read name
Hello CB I am ubuntu. #回车
cb@standalone14:~$ echo $name
Hello CB I am ubuntu. #显示变量的值1.2, instance shell script, save code as. sh file, change executable permissions, run
1. Print some header informationCommand ............Dilimiter
The standard input of the command as the content of the boundary symbol DilimiterCommonly used in the echo command, this avoids the need to use an echo command without outputting a line, and the adjustment of the output format is also simpler.For example: Echo **********************Hello, welcome to with my shell script
**********************Something_message
will be printed on the scree
Using the parameter-H | --help. Most commands in the Shell support: command--help|-h to see instructions for using commands
Use the man command: Man command to view details of the man manual for the command.
Use Whatis:whatis to display a description of the command
View the use Help for Shell built-in commands (BUILTIN)
Use the Help command to view the use of bui
operations comparisons. The variables in the double brackets can not use the $ symbol prefix, as long as the expressions in parentheses conform to the C-language operation rules, and multiple expressions are supported by commas.For example, you can directly use for ((i=0;iIf you can use the IF (($i [[]] enhanced square bracket usage, often used for string comparisons. Mainly used for conditional testing, expressions in double brackets can use , | |, For example, you can use if [[$a! = 1 $a! =
Directory
Re-recognize variables
Function
Conditional statements
Looping statements
1. Re-identification of variablesThere are 3 types of variables in the Shell: User variables, positional variables, environment variablesUser VariablesUser variables are variables that users define in the shell programming
command line parameter of another command.
Command:
Find.-mtime-1-type F-print
Used to search for files modified in the past 24 hours (-mtime-2 indicates the past 48 hours. If you want to pack all the searched files, you can use the following script:
#! /Bin/sh# The ticks are backticks(') Not normal quotes ('):Tar-zcvf lastmod.tar.gz 'Find.-mtime-1-type F-Print'
3)Process Control
"If"If the expression is true, the part after then is executed:
If...; Then
Certificate ------------
1shell is an interface between the user and the kernel to interoperate,
2shell commands that can be run include Linux commands, built-in commands, utilities, user programs, shell scripts
3shell characters:
4. Get command help: Man,info.--help
5. The file type has a normal file (-), directory (d), Symbolic link files (l), character device files (c), block device files (b), sockets (s), Named Pipes (p), block device files are computer peripherals
. Easy Data Processing6, cross-platform support and learning process is relatively short Disadvantages:Although Shell script claims to be a program, the shell script does not have enough speed to process the data. Because shell script uses external instructions and some of the default tools of the bash shell, he often
There are control structures in all programming languages, and shell programming is no exception. The IF structure is the most commonly used branch control structure.
The most common if statements in Linux Shell programming are: If.....then...fi,if....then....else....fi,if..
is stored in the variable: syntax: Read var1, Var2, ..., VarnWrite the following script file, prompt the user to enter a name, the user uses the keyboard input name, stored in the specified variable, and then display:VI Sayh Echo " "read fname # user input is stored inside the fname!!! Echo " Hello $fname, Lets be friend! "Execute the following statement to run Sayh:chmod +x sayh$. /sayhYour First Name:ZhaoHello Zhao, Lets be friend!2.4 Wildcard charactersReference documents:[1] Linux
1. Piping and redirectionFile descriptor 0: standard input for a programFile descriptor 1: Standard outputFile descriptor 2: standard error output> Operator: Redirects the standard output to a file.>> operator: Attaches the output to a file.| operator: the connection process.2. Shell as a programming language2.1 Creating a ScriptComments begin with a # symbol.#! The character that tells the system to follow
return value.3.shell variable Quantum string knowledge650) this.width=650; "Width=" 720 "height=" 305 "title=" 1.png "style=" width:720px;height:305px; "alt=" Wkiol1j4d1jcztx-aafomugkd9o187.png "src=" https://s5.51cto.com/wyfs02/M01/91/B9/ Wkiol1j4d1jcztx-aafomugkd9o187.png "border=" 0 "vspace=" 0 "hspace=" 0 "/>Tips:The colon of the expression in the above ID 11-14 can be omitted, and if the colon is omitted, the "empty or Unassigned" section in eac
Linux command line and shell script programming Daquan graphic desktop script programming create text menu create menu layout [plain] #! /Bin/bashclear # clear screen echo-e quot; \ t \ tMenu quot; echo-e quot; 1. \ tDisplaydiskspace quot; echo-e quot; Linux command line and shell script
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.