[Linux] deeply understand Shell usage, from getting started to mastering

Posted by John_wilson on Thu, 20 Jan 2022 01:28:05 +0100

I Shell background

1.1 GNU plan

The GNU Project is a free software, mass collaboration project that Richard Stallman announced on September 27, 1983. Its goal is to give computer users freedom and control in their use of their computers and computing devices by collaboratively developing and publishing software that gives everyone the rights to freely run the software, copy and distribute it, study it, and modify it. GNU software grants these rights in its license.

In 1984, Sturman began the GNU program. The purpose of this program is to create a free and open UNIX operating system (Free Unix).

GNU's not Unix!
GNU is not Unix! So what is GNU? GNU's Not Unix But infinite cycle! Busy~

Bash Shell mentioned in this chapter comes from GNU, and some famous software comes from GNU:

  • Emacs
  • GNU C (GCC)
  • GNU C Library (glibc)
  • Bash shell

In 1991, the Linux kernel appeared, developed outside the GNU project by Linus Torvalds, and in December 1992 it was made available under version 2 of the GNU General Public License. Combined with the operating system utilities already developed by the GNU project, it allowed for the first operating system that was free software, commonly known as Linux.

1.2 Shell

We must communicate the instructions we input with the Kernel through the "Shell" so that the Kernel can control the hardware to work correctly.

We can find that the application is actually in the outermost layer, just like an egg shell, so it is called a shell!

be careful:
Any interface that can operate the application program can be called a shell program.

  • Narrow sense: command line software, bash, etc.
  • Generalized: graphical interface software! Because the graphical interface can also operate various applications to call the core work!

There are many versions of shells, such as Bourne SHell (sh), the default C SHell in Sun, TCSH, zsh and so on. Each Shell has its own characteristics. Bash Shell is an enhanced version of Bourne Shell and developed under the GNU based architecture!

How many Shells are available on your Mac? Use the following command to check:

MacBook-Pro-3:~ sunquan$ cat -n /etc/shells
     1	# List of acceptable shells for chpass(1).
     2	# Ftpd will not allow users to connect who are not using
     3	# one of these shells.
     5	/bin/bash
     6	/bin/csh
     7	/bin/dash
     8	/bin/ksh
     9	/bin/sh
    10	/bin/tcsh
    11	/bin/zsh
MacBook-Pro-3:~ sunquan$ 

1.3 Bash

At present, Bash Shell is used by default in most daily life. We can man bash on the computer

MacBook-Pro-3:~ sunquan$ man bash
BASH(1)                                                                BASH(1)

       bash - GNU Bourne-Again SHell

       bash [options] [file]

       Bash is Copyright (C) 1989-2005 by the Free Software Foundation, Inc.

       Bash  is  an  sh-compatible  command language interpreter that executes
       commands read from the standard input or from a file.  Bash also incor-
       porates useful features from the Korn and C shells (ksh and csh).

       Bash  is  intended  to  be a conformant implementation of the Shell and
       Utilities portion  of  the  IEEE  POSIX  specification  (IEEE  Standard
       1003.1).  Bash can be configured to be POSIX-conformant by default.

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions.

Bash was released as early as 1989, which is used to operate the basic interface of the operating system. What are the advantages of bash:

1. Command history function
The user can switch the commands used in history through the up and down arrow keys in the command terminal. In ~ / bash_ Bash commands used are recorded in history

MacBook-Pro-3:~ sunquan$ tail -n 10 ~/.bash_history 
vim awkDemo2
diff awkDemo awkDemo2
eixt (0) | echo $?
eixt (0) ; echo $?
eixt 0 ; echo $?
eixt 0;echo $?
var = sq ; echo var
var = sq ; echo $var
var=sq;echo $var
exit 0;echo $var

2. Command completion function
Command completion and file completion can be carried out through Tab key

3.alase alias settings

MacBook-Pro-3:~ sunquan$ ls -l
total 32
drwx------@  6 sunquan  staff   192  5 27  2020 Applications
MacBook-Pro-3:~ sunquan$ alias ll='ls -l'
MacBook-Pro-3:~ sunquan$ ll
total 32
drwx------@  6 sunquan  staff   192  5 27  2020 Applications
MacBook-Pro-3:~ sunquan$ 

4. Programmed scripts: (shell scripts)
You can build more functions by writing shell scripts and assembling commands

II Shell script basic syntax

2.1 Getting Start

1. Introduction

shell script is a "program" written by using the functions of the shell. This program uses plain text files. It can also be seen as a batch file or a program language, and because they all use shell and related tool instructions, they can be executed without compilation.

2. Setting up Workspace

MacBook-Pro-3:~ sunquan$ vim ~/.bash_profile
export HISTFILESIZE=1024 #Set the history record bash command size
export PATH=${PATH}:/Users/sunquan/Library/shell #shell workspace
export PATH=${PATH}:/usr/local/bin
export PATH=$PATH:/Users/sunquan/Library/Android/android-ndk-r18b/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
source /Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh
# Setting PATH for Python 3.7
# The original version is saved in .bash_profile.pysave
export PATH
MacBook-Pro-3:~ sunquan$ source ~/.bash_profile

My own shell script is stored in / Users/sunquan/Library/shell and configured in the PATH of the system. When using script commands, the system can automatically find and execute relevant commands in this directory.

3. Hello World

#! /bin/bash
# author: scott sun
# #! Is a convention tag that tells the script what shell environment to execute in
# a program to show "hello world" in your screen
# how to run : switch the dir
# chmod +x helloworld
# ./helloworld; if your add the scripts'dir in your PATH  variable, you can run helloworld directly
echo "Hello World"
exit 0

1. The first line #/ bin/bash declares the shell name used by this script, which must include. When the program runs, you can load the relevant environment configuration files of bash
2. The execution result of an instruction can use $? This variable to observe. Therefore, exit 0 means that a 0 is returned to the system after leaving the script. Then, if echo $? Then you can get the value of 0

4. Motivation Example

 #author : scott sun
 #function: use to count how much commands in your computer.
 count=0; nonex=0
 for directory in $PATH ; do
     if [ -d "$directory" ]
         for command in "$directory"/* ; do
             if [ -x "$command" ]
                 count=$[count + 1]
                 nonex=$[nonex + 1]
 echo "$count commands, and $nonex that weren't executable"
 exit 0

MacBook-Pro-3:shell sunquan$ countShellCommands 
2438 commands, and 14 that weren't executable
MacBook-Pro-3:shell sunquan$

2.2 Variable

Define variables:


1. The variable name cannot have spaces
2. English letters, numbers and underscores cannot use bash keyword

Use variables

echo $myname
echo ${myname}
echo "my name is ${myname}"

2.3 Data Type

 echo "int add int is $[ vint + vint2 ]"
 echo "int add float is $[ vint + vfloat ]"
 echo "int add string $[ vint + vString ]"

MacBook-Pro-3:shell sunquan$ variableDemo 
int add int is 3
/Users/sunquan/Library/shell/variableDemo: line 8: 1.1: syntax error: invalid arithmetic operator (error token is ".1")
int add string 1
MacBook-Pro-3:shell sunquan$

1. The shell does not have an explicit keyword to define the data type
2. The same type can be added, otherwise an error will be reported
3. The integer and string cannot be added

2.4 String

 echo "hello my name is $myname"
 echo 'hello, my name is $myname'
 echo "the length of $myname is ${#myname}"
 echo "substring(1,3) is ${myname:1:3}"
 echo "the first name is "; eval "echo $myname | cut -c1-5"

1. Single quotation marks are output as is, and double quotation marks can display variables
2. ${#string} output string length
3. ${string:start:end} intercepts the string

2.5 Array

 echo $array
 echo ${array[0]}
 echo ${array[5]}
 echo "the length of arr is ${#array[*]}"
 echo "the content of array is ${array[*]}"
 echo "the content of array is ${array[@]}"

MacBook-Pro-3:shell sunquan$ arrayDemo 

the length of arr is 5
the content of array is 1 2 3 4 5
the content of array is 1 2 3 4 5

1. array is defined as above
2. $array outputs the first element and obtains the value through arr[index]. The operation does not output
3. The functions of ${array [*]} and ${array [@]} are the same

2.6 Conditional Statements

1. if else

if [ -x "/path/filename" ]
    echo "this file can execute"
    echo "this file can't execute"

2. switch case

echo "input the num between 1-4"
read num
case $num in
	1) echo "the num is 1"
	2) echo "the num is 2"
	3) echo "the num is 3"
	4) echo "the num is 4"
	*) echo "the num is out of 1-4"

2.7 Looping

1. while

while(( $cnt<=5 ))
	echo $cnt
	let "cnt++"

1. let is used to execute one or more expressions without the $symbol

2. for

for loop in 1 2 3 4 5
	echo "The value is $loop"

3. Infinite cycle

for (( ; ; ))

2.8 basic operation

#  	+	-	*	/	%
#	==	!=	-eq	-ne	-gt	-lt	-ge	-le	
#	! 	- o -a (not or with)
#	-Is the z string length 0
#	-n is the string length 0
#	Is the $string empty
echo $[ 1 + 2 ]
# -r -w -f -d -x -e
# Readable and writable ordinary files 	 Directory 	 Executable 	 Does it exist
if [ -r $file ]
	echo "File readable"
	echo "File unreadable"

2.9 Function

     echo "the name of command is $0"
     echo "the first params is $1"
     echo "the num of params is $#"
     echo "the str of params is $*"
 fun1 1 2 3 4 str
 exit 0

MacBook-Pro-3:shell sunquan$ funcDemo 
the name of command is /Users/sunquan/Library/shell/funcDemo
the first params is 1
the num of params is 5
the str of params is 1 2 3 4 str

2.10 Import

# this is first script
. secondScriptName

III Shell script practice

3.1 memorandum

# file name: remember
# func : remember the note
if [ $# -eq 0 ] ; then
	echo "Enter note, end with ^D"
	cat - >> $rememberfile
	echo "$@" >> $rememberfile
exit 0
# remind note
if [ ! -f $rememberfile ]
	echo "$0: .remember not exist." >$2
	exit 1

if [ $# -eq 0 ]
	more $rememberfile
	grep -i "$@" $rememberfile | more
exit 0

3.2 navigation

if [ $# -le 0 ] ; then 
	open https://baidu.com
elif [ $1 == 'mail' ] ; then
	open mail.163.com

Shell has many uses, just like 4 In the motivation example, there are at least thousands of executable commands in the computer, just like Lego. Use these commands to build blocks in your imagination. You can combine Git, awk, sed, ffmpeg, adb, gradle and other development environments and commands to create your perfect development environment.

Personal profile

Occupation: Alibaba - Wireless Development Expert
WX: sunquan97
Mail: sunquan9301@163.com
Please indicate CSDN + reason for addition,

Topics: Programming Linux shell vim bash