Getting to know shell scripts

What is a shell?

Shell is a program written in C language, which is a bridge for users to use Linux;

Shell is not only a command language, but also a programming language;

Shell refers to an application that provides an interface through which users can access the services of the Linux kernel

Shell scripts include sh, bash, ksh, csh, zsh, etc. different shell scripts have their own characteristics, but the usage is basically the same. The Mac comes with a shell, which can be viewed with cat /etc/shells. The article mainly focuses on bash.

shell script

The general file suffix is sh form.

Who needs to learn shell?

  • Linux operation and Maintenance Engineer: write Shell program for service cluster management
  • Python and Java programmers: write Shell script programs or server maintenance, such as writing a script for regularly backing up the database
  • Big data programmers: write Shell programs to manage clusters
  • Front end programmer: understanding the shell can better carry out server-side development and operation

What can shell scripts do?

  • Simplify some complex commands (for example, it may take many steps to submit code once, but it can be simplified into one step with Shell);
  • Solve many repetitive operations (such as modifying the same file content multiple times);
  • Automatic packaging, compilation, release and other functions;

Therefore, learning to use shell can help us greatly improve development efficiency

shell writing

The first line is generally like this: (specify the script interpreter)

#!/usr/bin/php
#!/usr/bin/env python3
#!/usr/bin/env bash

#!” Is a convention tag, which tells the system what interpreter this script needs to execute. / env is the search in the PATH directory of the system; Tell the system that the program specified by the subsequent PATH is the Shell interpreter that interprets the script file

Run shell script

Mode 1

sh demo.sh

Mode 2

chmod +x demo.sh // Set up demo SH executable permissions
./demo.sh // Execute demo sh

Let's write a simple demo

// demo1.sh
#!/ usr/bin/env bash / / find the interpreter of the specified script from the system path 
echo "hello, world"

// implement
 Console execution bash demo1.sh
// Output: hello, world

Common grammar

variable

For variables that have been defined, add $. Curly braces outside the variable name are optional. The second form is recommended

name
name="qiao"
use
echo $name
echo ${name}
Basic grammar
  1. Defining variables: variable name = variable value. There must be no spaces on both sides of the equal sign. Variable names are usually capitalized
  2. Delete variable: unset variable name
  3. Declare static variable: readonly variable name, static variable cannot be unset
  4. Use variable: $variable name
// The syntax of accessing variables is ${var} and $var
echo $myName
echo ${myName}

// Delete variable
echo ${Variable name}
Variable naming rules
  • Naming can only use English letters, numbers and underscores. The first character cannot start with a number
  • There can be no spaces in the middle. You can use underscores_
  • Punctuation cannot be used
  • Keywords in bash cannot be used
Variable type
  • Local variable: a variable that is valid only within a script. Cannot be accessed by other programs and scripts
  • Environment variable: a variable that is visible to all programs or scripts in the current shell session. Similar to creating local variables, but using the export keyword, shell scripts can also define environment variables;
Position parameter variable
Basic grammar
  • $n: $1 represents the command itself, $1- 9 generation surface The first 1 reach 9 individual ginseng number , 10 with upper ginseng number use flower Include number , as 9 represents the 1st to 9th parameters, and the parameters above 10 are in curly brackets, such as 9 represents the 1st to 9th parameters, and the parameters above 10 are in curly brackets, such as {10}
  • $*: all parameters in the command line, and all parameters are regarded as a whole
  • $@: all parameters in the command line, and treat each parameter separately
  • $#: number of all parameters

notes

Lines beginning with "#" are comments and will be ignored by the interpreter. There are no multiple lines of comments in sh, only one # sign can be added to each line

character string

Strings can be in single quotation marks, double quotation marks, or no quotation marks. Data types commonly used in shells are strings and numbers.

Let's start with a demo

name="qiao"
echo "my name is $name"

// Output my name is qiao

Single quotation marks and double quotation marks can be used for strings. Single quotation marks cannot be contained in single quotation marks. Even if single quotation marks are escaped, it is not the second time. Double quotation marks can be used, and double quotation marks can also be used for strings

String operation
Splice string
name="qiao";
age="26"
echo $name $age
echo $name$age
Get string length
echo ${#name}

Intercept string
echo ${name:0:2}

array

In Shell, the array is represented by brackets, and the array elements are separated by the "space" symbol. The general form of defining the array is

age=(age1 age2 age3)
// You can also define the values of the array separately:
ary[0]=age1
ary[1]=age2
ary[3]=age3

Watch a demo

names=("zhang" "san" "zi")
echo ${names[0]}
echo ${names[2]}
echo ${names[@]} # @You can get all the elements in the array
// output 
zhang
zi
zhang san zi
Read array
format

${array name [subscript]}

echo ${age[0]}

// Use the @ symbol to get all the elements in the array
echo ${name[@]}
Gets the length of the array
# Gets the number of array elements
length=${#age[@]}
echo $length

# perhaps
length=${#age[*]}
echo $length

# Gets the length of a single element of the array
lengthn=${#age[n]}
echo $length

shell parameter passing

When executing a Shell script, you can pass parameters to the script. The format of obtaining these parameters in the Shell is $n, that is, $1, $2,

// demo2.sh
echo "The first parameter is: $1"
echo "The first parameter is: $2"
echo "The first parameter is: $3"

// implement
bash demo2.sh 1 2 3
// output
 The first parameter is: 1
 The first parameter is: 2
 The first parameter is: 3

Special character processing parameters:

  • $#: number of parameters passed to the script

  • $*: display all parameters

  • $@: returns all parameters

  • $-: displays the current options used by the Shell

  • $?: Exit status, 0 indicates no error, others indicate error

operation

arithmetic operation

bash itself does not support simple mathematical operations. It can be completed with the help of other commands, such as awk and expr. Expr is the most commonly used. Expr is an expression evaluation tool, which can be used to evaluate expressions. Arithmetic operators include: +- × / % = == !=

Watch a demo

// demo.sh
val=`expr 2 + 2`
echo $val

// Execute bash demo sh
// Output 4
be careful
  • A space is required around the operator and backquotes are used
Relational operator

Relational operations only support numbers, not strings, unless the value of the string is a number

Common relational operators
  • -eq: is it equal
  • -ne: unequal
  • -gt: greater than
  • -lt: less than
  • -ge: greater than or equal to
  • -le: less than or equal to

Watch a demo

// demo.sh
a=4
b=3
if [ $a -eq $b ]
	then
	echo "equal"
else
	echo "Unequal"
fi

// bash demo.sh
// Unequal output
Boolean operation
  • !: wrong
  • -o: Or
  • -a: And
Logical operator
  • &&: logic and
  • ||: logical or
String operator
  • =: equal
  • !=: Unequal
  • -z: Whether the string length is 0. If it is 0, return true [- Z $a]
  • -n: Whether the string length is 0. If not, return true [- N $a]
  • str: whether the string is empty. If not, return true [$a]
File test operator
attributedescribeWriting method
-bDetect whether the file is a block device file[ -b $file ]
-cDetect whether the file is a character device file[ -c $file ]
-dDetect whether the file is a directory[ -d $file ]
-fCheck whether the file is a normal file[ -f $file ]
-gCheck whether the SGID bit is set in the file[ -g $file ]
-kCheck whether the file has an adhesive bit set[ -k $file ]
-pDetect whether the file is a named pipe[ -p $file ]
-uCheck whether the SUID bit is set in the file[ -u $file ]
-rCheck whether the file is readable[ -r $file ]
-wCheck whether the file is writable[ -w $file ]
-xDetect whether the file is executable[ -x $file ]
-sDetect whether the file size is greater than 0[ -s $file ]
-eDetect whether the file exists[ -e $file ]

Watch a demo

// demo.sh
file="/Users/user name/Desktop/shell-study/demo.sh"
if [ -e $file ]
	then
	echo "File exists"
else
	echo "file does not exist"
fi

if [ -r $file ]
	then
	echo "readable"
else
	echo "unreadable "
fi

if [ -w $file ]
	then
	echo "Writable"
else
	echo "Not writable"
fi

// bash demo.sh
// output
 File exists
 readable
 Writable

Process judgment

condition
#!/usr/bin/env bash
a=1
b=2
if [ $a == $b ]
    then
        echo "a be equal to b"
 elif [ $a -gt $b ]
    then
        echo "a greater than b"
 elif [ $a -lt $b ]
    then
        echo "a less than b"
 else
    echo "No conditions met"
 fi
for loop
// Writing method 1
for index in 1 2 3 4 5; do
    echo "index="$index
done

// Writing method 2
for ((i=0; i<5; i++)); do
    echo "i="$i
done
while statement
val=1
while(( $val<=5 ))
do
    echo $val
    let "val++"
done
case
Format:
case value in
 Mode 1)
    command1
    command2
    ...
    commandN
    ;;
Mode 2)
    command1
    command2
    ...
    commandN
    ;;
esac

Watch a demo

#!/ usr/bin/env bash / / find the interpreter of the specified script from the system path 
echo "Enter 1 2 3 any number"
read num
case $num in
	1)echo "1 entered"
;;
	2)echo "Entered 2"
;;
	3)echo "3 entered"
;;
	*)echo "The value entered is not 1 2 3"
;;
esac

// output
bash demo3.sh
 Enter 1 2 3 any number
1
 1 entered

bash demo3.sh
 Enter 1 2 3 any number
2
 Entered 2

bash demo3.sh
 Enter 1 2 3 any number
3
 3 entered

bash demo3.sh
 Enter 1 2 3 any number
5
 The value entered is not 1 2 3
function
Function definition

Custom functions can use or not use the function keyword. If a return value is specified, it will be returned. If there is no return statement, the last run result will be used as the return value

Watch a demo

function fn(){
	echo "hello world"
}
// perhaps
fn(){
	echo "hello world"
}
function call

Function name

fn
Function parameters

When calling a function, you can pass in parameters, which are used inside the function; The function itself is a command, so it can only be passed through $? To get the return value

#!/ usr/bin/env bash / / find the interpreter of the specified script from the system path
function add(){
	num=0;
	for((i=1;i<=$#;i++));
	do
		num=`expr $i + $num`
	done
	return $num
}
add 1 2 3 4 5 6 7 8 9 10
a=$?

echo $a

// bash demo.sh
// Output: 55
I / O redirection

Use > to write the echo result to the specified file. This is an output redirection. There are mainly the following redirections:

Writing methoddescribe
command > fileRedirect output to file
command < fileEnter redirect to file
command >> fileThe output is redirected to the file file by appending
n > fileRedirect file with file descriptor n to file
n >> fileRedirect the file with the file descriptor n to the file file by appending
n >& mMerge output files m and n
n <& mMerge input files m and n
<< tagTake the content between the start tag and the end tag tag as input

Common commands

echo

Output for string

echo "hello, world"
// Output hello, world

// Output string with variable:
name = word
echo "hello, \"${name}\""
// Output hello, "word"

// Output string with newline character
echo "name\nage"
// Output name\nage

echo -e "name\nage" # -e open escape
#  output
#  name
#  nage

// echo $PATH gets the path of all programs that can be executed directly in the system

Parse escape character

-The e parameter allows echo to parse escape characters

echo -e "hello \n world" # If - e is not added, it will be output as is, and if - e is added, it will wrap

Use backquotes to display the results of command execution, such as date and pwd

echo `pwd`
echo `date`

printf

Format the output string. By default, printf will not automatically add line breaks like echo. If line breaks are required, you can add them manually. \ n

export

export Variable name=Variable value, set Shell Variable output as environment variable

source

source configuration file path, so that the modified configuration information takes effect immediately

test

Check whether a certain condition is true. You can test values, characters and files

// demo.sh
a=20
b=23
if test a == b
	then
	echo "equal"
else
	echo "Unequal"
fi
// bash demo.sh
// Output: unequal

grep

Good at finding function

Writing method

grep [OPTIONS] PATTERN [FILE...]

The pattern here is either a character (string) or a regular expression

Common parameters

parameterdescribe
-cLists only the number of rows in the file that contain the schema
-iIgnore letter case in mode
-lLists file names with matching lines
-nList the line number at the beginning of each line
-vLists rows that do not have matching patterns
-wSearch the expression as a complete single character, ignoring those partially matching lines

Look at an example

// File path filePath="/Users / username / desktop / shell study / file. JS"

// Find and print the line containing "list" 
grep "list" ${filePath}

// Ignore case search (- i)
grep -i "list" ${filePath}

// Counts the number of occurrences of the string (- c)
grep -c "show" ${filePath} #17
grep -n "show" ${filePath} 
-n You can list the line number and content of the matching string

// Highlight the match (- - color)
grep --color "list" ${filePath}

// Double conditional match matches the file containing float in the current directory and highlights it
ls *.html | grep --color "float"
summary
  1. Restrictions on single quote strings:

    Any character in single quotation marks will be output as is, and the variable in single quotation marks string is invalid

    A single quotation mark cannot appear in a single quotation mark string (nor after using an escape character for a single quotation mark)

  2. Double quotation mark

    There can be variables in double quotation marks

    Escape characters can appear in double quotation marks

  3. variable

    Variables can only consist of upper and lower case letters, numbers and underscores.

    Variable name cannot start with a number; There must be no spaces around the equal sign of variable assignment

    Variables can store numeric or string types

    String variables can be enclosed in single or double quotation marks

GitHub code reference link: https://github.com/qiaochunmei/shell-study

Keywords: Linux shell

Added by michaewlewis on Fri, 21 Jan 2022 17:00:45 +0200