1. Linux scripting basics
1.1 Grammar basic introduction
1.1.1 Beginning
Programs must start with the following lines (required) The first line of the file):
#!/bin/sh
The symbol #! is used to tell the system that the parameter following it is the program used to execute the file. In this example we use /bin/sh to execute the program.
When editing a script, you must also make it executable if you want to execute it.
To make the script executable:
Compile chmod +x filename so that you can run it with ./filename
1.1.2 Comments
When programming, the sentence beginning with # indicates the comment until the end of the line. We sincerely recommend that you use comments in your program.
If you use annotations, you can understand the role of the script
and how it works in a very short time, even if the script is not used for a long time.
1.1.3 Variables
You must use variables in other programming languages. In shell programming, all variables are composed of strings, and you don't need to declare variables
. To assign a value to a variable, you can write it like this:
#!/bin/sh
#Assign a variable:
a="hello world"
# Now print the contents of variable a:
echo "A is:"
echo $a
Sometimes variable names are easily confused with other text , for example:
num=2
echo "this is the $numnd"
This does not print out "this is the 2nd", but only prints "this is the ", because the shell will search for the value of the variable numnd,
but this variable has no value. You can use curly braces to tell the shell that we want to print the num variable:
num=2
echo "this is the ${num}nd"
Print: this is the 2nd
1.1.4 Environment Variables
The variables processed by the export keyword are called environment variables. We don't discuss environment variables because environment variables are usually only used in login
scripts.
1.1.5 Shell Commands and Process Controls
Three types of commands can be used in shell scripts:
1)Unix Commands:
Although in the shell Any Unix command can be used in the script, but it is still a relatively common command. These commands are usually used for
file and text operations.
Common Command Syntax and Features
echo "some text": Print text content on the screen
ls: File List
wc – l filewc -w filewc -c file: Calculate the number of words in the file count calculation file The number of characters in the calculation file
cp sourcefile destfile: File copy
mv oldname newname : Rename the file Or move files
rm file: delete files
grep 'pattern' file: Search for strings in files such as: grep 'searchstring' file.txt
cut -b Colnum file: Specify the range of file contents to be displayed and output them to standard output devices. For example: output
5th to 9th characters per line cut -b5-9 file.txt Do not and Cat command obfuscation,
This is two completely different commands
cat file.txt: Output file contents to standard output device (screen)
file somefile: get File type
read var: prompts the user for input and assigns the input to the variable
sort file.txt: for file.txt The rows are sorted
uniq: delete the text file appears in the ranks such as: sort file.txt |
Uniq
expr: Perform mathematical operations Example: add 2 and 3expr 2 "+" 3
find: Search for files such as: search for find based on file name. -name filename -print
tee: Output data to standard output devices (screens) and files such as: somecommand |
Tee outfile
Computer Store News Microsoft released Windows8 RTM for PC OEMs and manufacturer partners on August
A summaryIn order to manage all server accounts and passwords in the enterprise, you can use the Wi
Linux and BSD are free, open source, Unix-like systems. They even use a lot of the same software. T
you want under Linux How to install the jdk you want under Linux? Last time we talked about how to u
Some tips on how to redirect in Linux I/O
u disk installation linux experience
Manually configure the IP address of linux (centos), DNS, gateway
Linux Administrator: Enterprise Port Scanning Policy
Display all process commands in linux
10 important Linux ps commands are actually
Several security precautions for Linux systems
Linux system computer crashes the cause of failure
VirtualBox installation configuration and new virtual machine
Ssh certificate authentication landing implementation tutorial
How to restore the u disk capacity?
Win7 desktop machine is more expensive than Win8 version
How Windows 8 can pin arbitrary files to the Start screen
How to open Win7 quick launch bar
Win8 resets the task manager method
How about gt630m graphics card? How about gt630 graphics card? The evaluation data tells you!
Hidden files are no longer difficult to teach you to hide files in win xp
Windows 8.1 operating system locale bug collection version
Where is the win8.1 device manager open? Win8.1 open three ways of device manager
Which Win10 versions does Microsoft prepare for public downloads?