Linux shell script comprehensive learning

  

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

Copyright © Windows knowledge All Rights Reserved