注:本位为Linux系统的学习笔记,内容来自图书《Beginning Linux Programming》

目录

Get Started

  • Unix: originally developed at Bell Laboratories, designed in the 1970s.
  • Unix-like systems:
    • some available commercially: IBM’s AIX, HP’s HP-UX, and Sun’s Solaris
    • some available for free: FreeBSD and Linux
  • Linux: freely distributed implementation of a UNIX-like kernel.
  • GNU: stands for the recursive GNU’s Not UNIX
  • GPL: GNU General Public License
  • softwares from the GNU project under GPL:
    • GCC: The GNU Compiler Collection, containing the GNU C compiler
    • G++: A C++ compiler, included as part of GCC
    • GDB: A source code-level debugger
    • GNU maker: A version of UNIX make
    • Bison: A parser generator compatible with UNIX yacc
    • bash: A command shell
    • GNU Emacs: A text editor and environment
  • PATH:
    • /bin, /usr/bin, /usr/local/bin
    • /sbin, /usr/sbin (system administration program)
    • /opt (optional operating system components and third-party applications)
  • Header Files
    • provide definitions of constants and declarations for system and library function calls
    • /usr/include
  • Library Files
    • collections of precompiled functions
    • standard system libaries: /lib, /usr/lib
    • .a for traditional, static libraries
    • .so for shared libraries
  • gcc
    • -o write output to
    • -I use header files in subdirectories or nonstandard places
    • -l
    • -L
  • Static libraries
  • Shared Libraries
  • Getting help: man gccinfo gcc

Shell Programming

shell and bash

  • A shell is a program that acts as the interface between you and the Linux system, enabling you to enter commands for the operating system to execute.
  • In linux, the standard shell that is always installed as /bin/sh is called bash (the GNU Bourne-Again SHell), from the GNU suite of tools.

Pipes and Redirection

  • file descriptor:
    • 0: standard input to a program
    • 1: standard output
    • 2: standard error
  • Redirecting Output
    • >: redirect the standard output into a file
    • >>: append the standard output into a file
#分开output和error信息
kill 1234 >killout.txt 2>killerr.txt

#利用>&结合output和error信息
kill 1234 >killouterr.txt 2>&1

#利用/dev/null扔掉输出
kill 1234 >/dev/null 2>&1
  • Redirecting Input
more < killout.txt

但是这看起来并没有什么太大的用处。

  • Pipes
 ps –xo comm | sort | uniq | grep -v sh | more

Shell Programming

Creating a script

  • #: comments
  • #!(in the first line): tell the system which program to be used to execute this file. For example, #! /bin/sh is the default shell program.
  • commands: any Linux commands referenced by you PATH environment variable
  • exit: returns a sensible exit code. 0 denotes success
  • file command: check file type

Making a Script Executable

chmod +x script
  • chmod: change file modes or Access Control Lists
  • chown: change file owner and group
  • chgrp: change group

Shell Syntax

Variables

#所有的变量都被当成字符串,且case-sensitive
#数字类的字符串可以用一些utilities转换
#获取变量内容使用$
salutation=Hello
echo $salutation     ##输出“Hello”
# 有空格时,可以用引号
salutation="Yes Dear"
echo $salutation     ##输出“Yes Dear”

salutation=7+5
echo $salutation      ##输出“7+5”

# 使用read命令让用户对变量赋值
read salutation     ##用户输入“Hi”
echo $salutation      ##输出“Hi”

Quoting

myvar=”Hi there”
echo $myvar        ##输出"Hi there"
echo “$myvar”      ##输出"Hi there"
echo ‘$myvar’      ##输出"$myvar" 
echo \$myvar       ##输出"$myvar" 

Environment Variables

Environment Variable Description
$HOME home目录路径
$PATH 命令搜索的目录路径列表,用冒号分隔
$0 运行的脚本的名字
$# 运行的脚本的参数数目
$$ 运行的脚本的process ID,经常用来生成临时文件,如/tmp/tmpfile_$$

Parameters Variables

Parameters Variables Description
$1, $2 第1,2个参数
$* 所有的参数列表,作为一个变量

Conditions

  • test or [: the shell’s Boolean check
  • condition structure:
## 检查是否存在文件
## test的exit code决定是否继续执行代码
if test -f fred.c
then
## 插入代码
fi

或者

##请注意之间的空格
if [ -f fred.c ]
then
## 插入代码
fi

完整的

if + 条件判断
then
## 插入代码
elif + 条件判断
then
## 插入代码
else
## 插入代码
fi

  • condition types: String ComparisonArithmetic ComparisonFile Conditional
Type Expression Result
String Comparison string1 = string2 字符串相同
  string1 != string2 字符串不同
  -n string 字符串不空(不为null)
  -z string 字符串为空
Arithmetic Comparison expr1 -eq expr2 expression相同e
  expr1 -ne expr2 expression不同
  expr1 -gt expr2 expr1大于expr2
  expr1 -ge expr2 expr1大于等于expr2
  expr1 -lt expr2 expr1小于expr2
  expr1 -le expr2 expr1小于等于expr2
  ! expr expr不是True
File Conditional -d file file是目录
  -f file file是一个常规文件
  -r file file可读
  -s file file的大小非0
  -w file file可写
  -x file file可执行