[Notes]linux shell编程入门

本文为读《Linux Command Line and Shell Scripting Bible》笔记。

越来越多的对linux不了解阻碍着我玩好玩的事情,看来得补一下linux基础了,听说这本书不错,拿来读读。这纯属个人笔记,不是书的翻译,也不是抄袭。之所以把英文也放在上面,仅仅为了以后方便查阅而已。

现在共享出来。

本文首发:program-dog.blogspot.com

linux的工作机制

linux 内核四个部分

■ System memory management
■ Software program management
■ Hardware management
■ Filesystem management

The Linux system memory map


linux内核把硬件的物理内存,映射成虚拟内存.

The kernel then maintains a table of the
memory pages that indicates which pages are in physical memory, and which pages are swapped out to disk.

内核负责维护一张映射表,指明那些虚拟内存来自哪里。

The kernel keeps track of which memory pages are in use and automatically copies memory pages that have not been accessed for a period of time to the swap space area (called swapping out), even if there’s other memory available. When a program wants to access a memory page that has been swapped out, the kernel must make room for it in physical memory by swapping out a different memory page, and swap in the required page from the swap space. Obviously, this process takes time, and can slow down a running process. The process of swapping out memory pages for running applications continues for as long as the Linux system is running.

内核还会跟踪内存的使用,把使用少的一刀swap区域,如果内存紧张,内核向swap清理?再swap,浪费很长时间?

# 内存映射表在这里
cat /proc/meminfo
...

By default, each process running on the Linux system has its own private memory pages. One
process cannot access memory pages being used by another process. The kernel maintains its own memory areas. For security purposes, no processes can access memory used by the kernel processes.

每一个进程都有自己独自的内存区域,没有进程可以用内核的内存。

但是怎么共享信息呢?

To facilitate data sharing, you can create shared memory pages. Multiple processes can read and write to and from a common shared memory area. The kernel maintains and administers the shared memory areas and allows individual processes access to the shared area.

有一部分内存是专门用来共享的,由内核管理。

通过这个命令查看共享内存区域:

ipcs -m


------ Shared Memory Segments --------
key        shmid      owner      perms      size       nattch     status      
0x00000000 196608     dog        600            4M     2          dest         
0x00000000 229377     dog        600          512K     2          dest         
0x72215959 327682     dog        600          5.5K     1                       
0x00000000 360451     dog        600        320.3K     2   
... 
key地址?
shmid  ?
owner 所有者
perms 权限
size 
mattch
status

Software program management

The Linux operating system calls a running program a process. A process can run in the fore-ground, displaying output on a display, or it can run in background, behind the scenes. Thekernel controls how the Linux system manages all the processes running on the system.

第一个进程是init,其他进程由init启动。

The kernel creates the first process, called the init process, to start all other processes on the system. When the kernel starts, it loads the init process into virtual memory. As the kernel starts each additional process, it gives it a unique area in virtual memory to store the data and code that
the process uses.

init 把自己装载到内存以后,再装载别人到内存。

Some Linux implementations contain a table of processes to start automatically on bootup. On Linux systems this table is usually located in the special file /etc/inittabs .

开机启动的配置一般放在这里/etc/inittabs

The Linux operating system uses an init system that utilizes run levels. A run level can be used to direct the init process to run only certain types of processes, as defined in the /etc/inittabs file. There are five init run levels in the Linux operating system.

那个文件还定义了 run level 。

At run level 1, only the basic system processes are started, along with one console terminal process. This is called single user mode. Single user mode is most often used for emergency filesystem maintenance when something is broken. Obviously, in this mode only one person (usually the
administrator) can log in to the system to manipulate data.

Level 1

这是最基础的level,一般用来修复坏了的文件系统,典型的单用户模式,一般只能root登陆。

Level 3

The standard init run level is 3. At this run level most application software such as network support software is started.

很普通的level,一般运行各种服务。

Another popular run level in Linux is run level 5. This is the run level where the system starts the graphical X Window software, and allows you to log in using a graphical desktop window.

Level 5

图形界面的Level.

可以ps一下:

ps ax
...
  395 ?        Ssl    0:00 /usr/lib/systemd/systemd-timesyncd
  403 ?        Ss     0:06 /usr/bin/dbus-daemon --system --address=systemd: --no
  423 ?        Ss     0:00 /usr/lib/systemd/systemd-logind
  440 ?        Ssl    0:12 /usr/bin/mysqld --pid-file=/run/mysqld/mysqld.pid
  445 ?        Ssl    0:00 /usr/bin/gdm
  455 ?        Ssl    0:09 /usr/bin/NetworkManager --no-daemon
  469 ?        Ssl    0:00 /usr/lib/accountsservice/accounts-daemon
  511 ?        Ssl    0:00 /usr/lib/polkit-1/polkitd --no-debug
  519 ?        Sl     0:00 gdm-session-worker [pam/gdm-launch-environment]
  543 ?        Ss     0:01 /usr/bin/wpa_supplicant -u
  557 ?        Ss     0:00 /usr/lib/systemd/systemd --user
  559 ?        S      0:00 (sd-pam)
  265 ?        S<     0:00 [kpsmoused]
  371 ?        S<     0:00 [kvm-irqfd-clean]
  389 ?        S      0:00 [jbd2/sda11-8]
  390 ?        S<     0:00 [ext4-rsv-conver]

...

看到很多进程,第一行PID,第三行状态 S for sleeping, SW for sleeping and waiting, and R for running.
带括号的程序是不活跃的程序,被swap出去的。

Hardware management

Still another responsibility for the kernel is hardware management. Any device that the Linux system must communicate with needs driver code inserted inside the kernel code. The driver code allows the kernel to pass data back and forth to the device, acting as a middle man between applications and the hardware.

内核是软件和硬件的中间人。

There are two methods used for inserting device driver code in the Linux kernel:
■ Drivers compiled in the kernel
■ Driver modules added to the kernel
Programmers developed the concept of kernel modules to allow you to insert driver code into a running kernel without having to recompile the kernel. Also, a kernel module could be removed from the kernel when the device was finished being used. This greatly simplified and expanded using hardware with Linux.

加载硬件的两种方式,每次都重新编译内核,把驱动做成模块。

The Linux system identifies hardware devices as special files, called device files. There are three different classifications of device files:

■ Character
■ Block
■ Network

Character device files are for devices that can only handle data one character at a time. Most types of modems and terminals are created as character files. Block files are for devices that can handle data in large blocks at a time, such as disk drives.

linux把硬件识别成文件,有三种文件:

■ Character
■ Block
■ Network

Character 每次只能处理一个Character的,而block却可处理大一点的数据,如硬盘。

The network file types are used for devices that use packets to send and receive data. This includes network cards and a special loopback device that allows the Linux system to communicate with itself using common network programming protocols.

而Network则是包的形式通过网卡收发,遵循网络协议。

Linux creates special files, called nodes, for each device on the system. All communication with the device is performed through the device node. Each node has a unique number pair that identifies it to the Linux kernel. The number pair includes a major and a minor device number. Similar devices are grouped into the same major device number. The minor device number is used to identify a specific device within the major device group. This is an example of a few device files on a Linux server:

这些设备可以叫做节点(node),都装载在 /dev/ 下面

ls -al /dev/*  #这下面按照类别分类了。
brw-rw---- 1 root disk 8,  0 May 10 12:31 /dev/sda
brw-rw---- 1 root disk 8,  1 May 10 12:31 /dev/sda1
brw-rw---- 1 root disk 8, 10 May 10 12:31 /dev/sda10
brw-rw---- 1 root disk 8, 11 May 10 12:31 /dev/sda11
brw-rw---- 1 root disk 8,  2 May 10 12:31 /dev/sda2
brw-rw---- 1 root disk 8,  5 May 10 12:34 /dev/sda5
brw-rw---- 1 root disk 8,  6 May 10 12:31 /dev/sda6
brw-rw---- 1 root disk 8,  7 May 10 12:31 /dev/sda7
brw-rw---- 1 root disk 8,  8 May 10 12:31 /dev/sda8
brw-rw---- 1 root disk 8,  9 May 10 12:31 /dev/sda9
...
crw--w----  1 root tty         4,  39 May 10 12:31 tty39
crw--w----  1 root tty         4,   4 May 10 12:31 tty4
crw--w----  1 root tty         4,  40 May 10 12:31 tty40
crw--w----  1 root tty         4,  41 May 10 12:31 tty41

Each device within a major number has its own unique minor device node
number.

第5行就是节点的编号 .不同编号代表设备不是一个。

The first column indicates the permissions for the device file. The first character of the permissions indicates the type of file. Notice that the SCSI hard drive files are all marked as block (b)device, while the COM port device files are marked as character (c) devices.

上面第1列的b代表 block (b)device,c代表character (c) devices.

Filesystem management

The Linux kernel interfaces with each filesystem using the Virtual File System (VFS). This provides a standard interface for the kernel to communicate with any type of filesystem. VFS caches information in memory as each filesystem is mounted and used.

linux 系统支持的文件系统的类型有几十种.它可以把其他文件系统映射成虚拟文件系统(Virtual File System (VFS)).

The GNU coreutils package consists of three parts:
■ Utilities for handling files
■ Utilities for manipulating text
■ Utilities for managing processes

GNU下,很多工具包被发明,主要三个部分:

■ 处理文件的
■ 处理文本的
■ 处理进程的

shells

ash  #A simple, lightweight shell that runs in low-memory environments but has full compatibility with the bash shell
korn  #A programming shell compatible with the Bourne shell but supporting advanced programming features like associative arrays and floating-point arithmetic
tcsh #A shell that incorporates elements from the C programming language into shell scripts
zsh #An advanced shell that incorporates features from bash, tcsh, and korn, providing advanced programming features, shared history files, and themed prompts

The Linux desktop environment

The X Windows system

The X Windows software is a low-level program that works directly with the video card and monitor in the PC, and controls how Linux applications can present fancy windows and graphics on your computer.
The XFree86 software package is the older of the two, and for a long time was the only X Windows package available for Linux. As its name implies, it’s a free open source version of the X Windows software.
Recently, a new package called X.org has come onto the Linux scene. It too provides an open source software implementation of the X Windows system. It is becoming increasingly popular, with many Linux distributions starting to use it instead of the older XFree86 system.

至今为止 ,直接和显示器打交道的有两个软件包:XFree86和X.org后者是后起之秀。

The KDE desktop

The K Desktop Environment (KDE) was first released in 1996 as an open source project to produce a graphical desktop similar to the Microsoft Windows environment. The KDE desktop incorporates all of the features you are probably familiar with if you are a Windows user.

The GNOME desktop

The GNU Network Object Model Environment (GNOME) is another popular Linux desktop environment. First released in 1999, GNOME has become the default desktop environment for many Linux distributions (the most popular being Red Hat Linux).

Other desktops

Linux Distributions

The different Linux distributions are often divided into three categories:
■ Full core Linux distributions
■ Specialized distributions
■ LiveCD test distributions

Core Linux distributions

Slackware
Red Hat
Fedora
Gentoo
Mandriva
openSuSe
Debian

你可能感兴趣的:(linux,linux,shell,shell,bash)