I Shell background
1.1 GNU plan
Tips:
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).
Tips:
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
Tips:
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
Important:
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. 4 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) NAME bash - GNU Bourne-Again SHell SYNOPSIS bash [options] [file] COPYRIGHT Bash is Copyright (C) 1989-2005 by the Free Software Foundation, Inc. DESCRIPTION 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.
Tips:
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 PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}" 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
Important:
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
#!/bin/bash #author : scott sun #function: use to count how much commands in your computer. IFS=":" count=0; nonex=0 for directory in $PATH ; do if [ -d "$directory" ] then for command in "$directory"/* ; do if [ -x "$command" ] then count=$[count + 1] else nonex=$[nonex + 1] fi done fi done 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:
myname="sq"
Important:
1. The variable name cannot have spaces
2. English letters, numbers and underscores cannot use bash keyword
Use variables
myname="sq" echo $myname echo ${myname} echo "my name is ${myname}"
2.3 Data Type
#!/bin/bash vint=1 vint2=2 vfloat=1.1 vString="sq" 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$
Important:
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
#!/bin/bash myname="scott.sun" 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"
Important:
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
#!/bin/bash array=( 1 2 3 4 5 ) 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 1 1 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
Important:
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" ] then echo "this file can execute" else echo "this file can't execute" fi
2. switch case
#!/bin/bash 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" ;; esac
2.7 Looping
1. while
#!/bin/bash cnt=1 while(( $cnt<=5 )) do echo $cnt let "cnt++" done
Tips:
1. let is used to execute one or more expressions without the $symbol
2. for
#!/bin/bash for loop in 1 2 3 4 5 do echo "The value is $loop" done
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 ] then echo "File readable" else echo "File unreadable" fi
2.9 Function
#!/bin/bash fun1(){ 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
#!/bin/bash # this is first script . secondScriptName
III Shell script practice
3.1 memorandum
#!/bin/bash # file name: remember # func : remember the note rememberfile="$HOME/.remember_work" if [ $# -eq 0 ] ; then echo "Enter note, end with ^D" cat - >> $rememberfile else echo "$@" >> $rememberfile fi exit 0
#!/bin/bash # remind note rememberfile="$HOME/.remember_work" if [ ! -f $rememberfile ] then echo "$0: .remember not exist." >$2 exit 1 fi if [ $# -eq 0 ] then more $rememberfile else grep -i "$@" $rememberfile | more fi exit 0
3.2 navigation
#!/bin/bash if [ $# -le 0 ] ; then open https://baidu.com elif [ $1 == 'mail' ] ; then open mail.163.com ... fi
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,