Shell Programming: Explore the basic concepts and usage of Shell

Table of contents

Introduction to Shell

Shell script

Shell script runs

Shell variables

1. Create variables and assign values

2. Reference variables

3. Modify the value of the variable

4. Read-only variables

5. Delete variables

6. Environment variables

Shell string manipulation

1. Splicing strings

2. String length

3. String interception

Shell array

1. Create an array

2. Access array elements

shell passing parameters

1. Position parameter

2. Special variables

Shell echo command

1. Print text message

2. Display variable value

3. Print multi-line text

4. Output special characters

5. Output to file

6. Append to file


Introduction to Shell

Shell is a program written in C language, which is a bridge for users to use Linux. Shell is both a command language and a programming language.

Shell refers to an application program that provides an interface through which users access the services of the operating system kernel.

Shell script (shell script) is a script program written for the shell. Shell and shell script are two different concepts.

Shell script

Open a text editor (you can use the vi/vim command to create a file), create a new file test.sh, the extension is sh (sh stands for shell), the extension does not affect the execution of the script, just know the name, if you Use php to write shell scripts, just use php as the extension.

#!/bin/bash
echo "Hello World !"

#! Is a conventional mark, which tells the system what interpreter this script needs to execute, that is, which shell to use.

The echo command is used to output text to the window.

Shell script runs

1. As an executable program

chmod +x ./test.sh  #使脚本具有执行权限

./test.sh  #执行脚本

2. As an interpreter parameter

/bin/sh test.sh

/bin/php test.php

Shell variables

Variables are a fundamental way of storing and manipulating data. In shell scripts, you can create, assign, modify, and reference variables.

Only English letters can be used, cannot start with numbers, cannot have spaces in between, can use underscores, cannot use punctuation marks, and avoid using Shell keywords.

1. Create variables and assign values

name="John"
age=25

2. Reference variables

To refer to the value of a variable, you can use the $ symbol.

echo $name
echo "My name is $name"

3. Modify the value of the variable

You can use assignment statements to modify the value of a variable.

age=30

4. Read-only variables

You can use the readonly command to make a variable read-only, which means you cannot modify its value.

readonly age

5. Delete variables

Use the unset command to delete a variable.

unset age

6. Environment variables

When it comes to environment variables, you can think of them as a kind of global variable, which is visible to the entire operating system or process. Environment variables store some configuration information, paths and other important data, which can be accessed and used by different programs and scripts.

Environment variables are special variables that are available throughout a shell session. You can upgrade a variable to an environment variable using the export command.

export MY_VARIABLE="Hello"

Shell string manipulation

1. Splicing strings

Use the concatenation operator . Two strings can be concatenated together.

greeting="Hello"
name="Alice"
message=$greeting" "$name
echo $message

Or use variable references.

message="${greeting} ${name}"

2. String length

Use ${#string} to get the length of the string.

text="Hello, World!"
length=${#text}
echo "字符串长度为:$length"

3. String interception

Using ${text:7:5} will start from the 7th character (counting from 0), intercept 5 characters, and the obtained substring is "World" .

text="Hello, World!"
substring=${text:7:5}
echo "截取的子字符串:$substring"

Shell array

1. Create an array

Arrays can be created using parentheses and separating array elements with spaces within the parentheses.

fruits=("apple" "banana" "cherry")

2. Access array elements

Array elements are accessed by index, which starts counting from 0.

echo ${fruits[0]}  # 输出:apple
echo ${fruits[1]}  # 输出:banana
echo ${fruits[2]}  # 输出:cherry

shell passing parameters

1. Position parameter

# 脚本名:myscript.sh
echo "第一个参数是: $1"
echo "第二个参数是: $2"

implement

./myscript.sh arg1 arg2

output

第一个参数是: arg1
第二个参数是: arg2

2. Special variables

In addition to positional parameters, there are some special variables used to get information about the script itself and its environment, $0 : script name, $# : number of parameters passed to the script, $@ : list of all parameters, $* : all parameters A list of , as a single string, $? : the exit status of the previous command.

# 脚本名:special.sh
echo "脚本名:$0"
echo "参数个数:$#"
echo "参数列表:$@"
echo "参数列表(作为单个字符串):$*"
echo "上一个命令的退出状态:$?"

implement

./special.sh arg1 arg2 arg3

output

脚本名:./special.sh
参数个数:3
参数列表:arg1 arg2 arg3
参数列表(作为单个字符串):arg1 arg2 arg3
上一个命令的退出状态:0

Shell echo command

1. Print text message

echo "Hello, World!"

2. Display variable value

name="Alice"
echo "My name is $name"

3. Print multi-line text

echo "Line 1"
echo "Line 2"

4. Output special characters

\t : represents a tab character (Tab key), \n : represents a newline character;

echo "New\t line\n"

5. Output to file

This will output "Hello, File!" to a file named output.txt , overwriting the contents if the file exists.

echo "Hello, File!" > output.txt

6. Append to file

This will append "More content" to the end of the output.txt file.

echo "More content" >> output.txt

 

Guess you like

Origin blog.csdn.net/m0_67906358/article/details/132414134