Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Chapter 1 basic knowledge of Shell

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1.1 introduction

Shell is a scripting language written in C language. It is a bridge between users and Linux. The user inputs commands to Shell, Shell passes the corresponding operations to the kernel (Kernel), and the kernel outputs the processing results to users.

The following is a schematic diagram of the process flow:

Since Shell works on top of the Linux kernel, it is also necessary for us to know about Linux.

Linux is a free trial and free transmission Unix-like operating system. It is a multi-user, multi-task, multi-threaded and multi-CPU operating system based on POSIX and UNIX.

On September 27th, 1983, Richard Stallman (Richard Matthew Stallman) launched the GNU project, which aims to create a completely free operating system. In order to ensure that GNU software can be freely used, copied, modified and distributed, all GNU software has an agreement that authorizes all rights to anyone without allowing others to add any restrictions, the GNU General Public license (GNU General Plubic License,GPL), which means that it cannot be used for commercial purposes.

GNU is a recursive abbreviation for "GNU is Not Unix". UNIX is the name of a widely used commercial operating system.

In 1085, Richard Stallman founded the Free Software Foundation (Free Software Foundation,FSF) to provide technical, legal, and financial support for the GNU project.

In 1990, GNU planned to develop major projects including Emacs (text editor), GCC (collection of GUN Compiler Collection,GNU compilers), Bash and so on. GCC is a set of programming language compilers developed by GNU. There are also some libraries and tools for developing UNIX systems.

In 1991, Linuxs Torvalds (Linus-Torvalds) developed a UNIX-compatible Linux operating system kernel and released it under GPL terms.

In 1992, Linux combined with other GUN software, the completely free GUN/Linux operating system was officially born, referred to as Linux.

In January 1995, Bob Young founded ACC Company and developed the commercial version of RedHat Linux with GNU/Linux as the core.

The basic idea of Linux has two points: first, everything is a file; second, every software has a definite purpose. It is very similar to Unix's thought.

1.2 Shell is basically divided into two categories.

1.2.1 graphical interface Shell (GUI Shell)

GUI builds a fully functional, easy to operate and user-friendly desktop environment for Unix or Unix-like operating systems.

Mainstream desktop environments include KDE,Gnome and so on.

1.2.2 Command Line Interface Shell (CLI Shell)

CLI is an interface for typing executable instructions at the user prompt, and the user inputs instructions through the keyboard to complete a series of operations.

The mainstream CLI implementation on Linux systems is Bash, which is the default Shell for many Linux distributions. There are also many Shell used on Unix, such as tcsh, csh, ash, bsh, ksh, and so on.

1.3The first Shell script

This tutorial focuses on the default Bash Shell for most Linux distributions. Linux is the CentOS operating system under RedHat, which is completely free. Unlike the commercial version of RHEL (Red Hat Enterprise Linux), which comes from the same source code, CentOS does not include closed source software and after-sales support.

Open test.sh with vi and write:

# vi test.shalloxambinram bashecho "Hello world!"

The first line sets the running environment, and the second line prints Hello world!

Once written, start execution, and there are three ways to execute the Shell script:

Method 1: execute # bash test.shHello world directly with the bash interpreter! Method 2: add executable permissions # ll test.sh-rw-r--r--. 1 root root 32 Aug 18 01:07 test.sh# chmod + x test.sh#. / test.sh-bash:. / test.sh: Permission denied# chmod + x test.sh#. / test.sh#. / in the current directory Hello world! Method 3:source command execution, with the current default Shell execution # source test.shHello world!

1.4 Shell variable

1.4.1 system variabl

Execute env or set directly at the command prompt to view system or environment variables. Env displays user environment variables, and set displays Shell predefined variables and user variables. Can be exported as user variables through export.

Some system variables that are commonly used when writing Shell scripts:

$SHELL default Shell$HOME current user home directory $IFS default internal domain separator $LANG default language $path default executor path $PWD current directory $UID user ID$USER current user $HISTSIZE history command size, you can set the command execution time through the HISTTIMEFORMAT variable

1.4.2 ordinary variables and temporary environment variables

Normal variable definition: VAR=value

Temporary environment variable definition: export VAR=value

Variable reference: $VAR

Here's a look at the difference between them:

The scope of the environment variables of the Shell process is the Shell process, and when export is imported into the system variable, the scope is the Shell process and its shell child processes.

The first column of the ps axjf output is PPID (parent process ID) and the second column is PID (child process ID)

When SSH connects to Shell, the current terminal PPID (- bash) is the PID (root@pts/0) of the sshd daemon, so the PPID of all processes under the current terminal is the PID of-bash, such as executing commands and running scripts.

So when the variable set under-bash is valid only under the-bash process, while the child process bash under-bash is invalid, it is only valid after export.

Further explanation: reconnect the SSH, remove the variables defined above and test

Therefore, the variable defined by the current shell must be export, otherwise it will not be referenced when writing the script.

It is also important to note that after exiting the terminal, all user-defined variables are cleared.

The variables defined under / etc/profile are based on this principle, and later chapters will explain the Linux common variable files.

1.4.3 position variable

A position variable refers to the nth argument followed by a function or script.

$1Mustn. Note that you need to use curly braces from the 10th, for example, ${10}.

Shift can control position variables, such as:

#! / bin/bashecho "1: $1" shiftecho "2: $2" shiftecho "3: $3" # bash test.sh a b C1: a2: c3:

Each time the shift command is executed, the number of location variables is reduced by one, and the value of the variable is advanced by one bit. Shift n, which can be set to move forward n bits.

1.4.4 Special variables

$0 script name $? Returns whether the previous command was executed successfully. 0: success, non-0: failure $# Total positional parameters $* all positional parameters are treated as a string $@ each positional parameter is treated as a separate string $$current process PID

1.5 variable reference

1.5.1 Custom variables and references

# VAR=123# echo $VAR123

All variable references in Shell use the $symbol, followed by the variable name.

Sometimes individual special characters affect normal references, so you need to use ${VAR}, for example:

# VAR=123# echo $VAR123# echo $VAR_ # Shell allows VAR_ to be a variable name, so this reference considers this to be a valid variable name, so it returns an empty # echo ${VAR} 123

Sometimes the variable name gets in the way of other strings and can be mistaken for the entire variable:

# echo $VAR456# echo ${VAR} 456123456

1.5.2 use the command result as the value of a variable

# VAR= `echo 123` # echo $VAR123# VAR=$ (echo 123) # echo $VAR123

The reverse apostrophe here is equivalent to $() and is used to execute the Shell command.

Blog address: http://lizhenliang.blog.51cto.com

QQ group: 323779636 (Shell/Python operation and maintenance development group)

1.6 double quotes and single quotation marks

When a variable is assigned, if the value has a space, Shell interprets the string after the space as a command:

# VAR=1 2 3-bash: 2: command not found# VAR= "1 2 3" # echo $VAR1 2 "VAR='1 2 3 # echo $VAR1 2 3

I can't see the difference, so let me give you another explanation:

# nailed VAR= "1 2$ N" # echo $VAR1 2 VAR='1 2$ nicked # echo $VAR1 2$ N

Single quotation marks tell Shell to ignore special characters, while double quotes explain the original meaning of special symbols, such as $,!.

1.7 comment

The Shell comment is also simple, as long as you precede each line with a # sign, which means that Shell ignores the interpretation.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report