×

开发工具:收藏一些 Linux Shell 的入门笔记

作者:Terry2023.07.04来源:Web前端之家浏览:4019评论:0
关键词:Shell

今天把之前收藏的一些 Linux Shell 的入门笔记分享下,希望能够帮助到大家。

Shell 知识体系

  • (结构和功能)

    • 存储:mount, unmount (略)

    • 网络:ping, traceroute, wget, ssh

    • 归档压缩:gzip, tar, rsync

    • 正则表达式:grep

    • 文本处理:cat, sort, uniq, diff, sed

    • 格式化输出:fmt, printf

    • 打印机:(略)

    • 编译:make

    • $$, $?, $_

    • $PWD, $SHELL, $USER, $HOME, $HOSTNAME

    • $PATH:环境变量

    • $PS1:提示符 Prompt

    • $PS4:追踪调试 set -x, set +x

    • $RANDOM

    • echo, exit

    • 路径:ls, cd, pwd

    • 文件:

    • 使用命令:type, which, man, info, whatis, help, --help, alias

    • 权限:id, chmod, chown, chgrp, umask, su, sudo, passwd

    • 进程:ps, top, jobs, bg, fg, kill, killall, shutdown

    • Shell 环境:printenv, set, export, source

    • 文本编辑:vi, nano

    • Misc:trap, wait, sleep

    • file, less, cat

    • cp, mv, mkdir, rm, ln

    • locate, find, touch, stat

    • /, /bin, /usr/bin, /dev, /tmp, /etc, /opt

    • Linux 文件系统层次标准(基础目录结构)

    • 一些内建命令 Builtin Commands command -options arguments

    • 一些内建变量 Shell Varibles

    • (更多内建命令)

  • 键盘操作

    • less、man 等控制:h, ?, q, /pattern, n, N

    • 命令行光标:Ctrl-u, Ctrl-y

  • (命令行书写)

    • reset:\033[0m

    • 4-bit black:\033[0;30m

    • foreground rgb:\x1b[38;2;⟨r⟩;⟨g⟩;⟨b⟩m

    • background rgb:\x1b[48;2;⟨r⟩;⟨g⟩;⟨b⟩m

    • 移动光标并清除上一行:echo "\033[1F\033[2K\c"

    • 输出重定向:echo 123 > a.log, >>, 2>, 2>&1, &>

    • 管道 Pipelines:ls /usr/bin | wxc -l

    • 输入重定向:python hello.py < foo.txt

    • xagrs:把标准输入转化成参数列表

    • Here Documents:<<, <<-, _EOF_

    • Here Strings:<<<, read -r -a Words <<< "This is a string of words."

    • 路径展开:echo *.txt, ls ~username

    • 算数表达式展开 Arithmetic Expansion: echo $(( (2+3)*4 ))

    • 花括号展开 Brace Expansion:echo file_{A{1,2},B{1,2}}, echo {0..z}

    • 参数展开 Parameter Expansion:

    • 命令替换 Command Substitution:ls -l $(which env)

    • 引号,转义 Escape:echo "Hello $USER", echo '$USER', echo "\$5.00"

    • history:!!, !number

    • echo $USER, echo $PWD, echo ${USER}

    • 值检查和替换:echo ${var:-fallbackVal}, echo ${var:=defaultVal}, echo ${var:+successVal}

    • 值检查和报错:echo ${var:?errMsg}

    • 子串:echo ${parameter:offset:length}

    • 替换:echo ${parameter/pattern/replacer}

    • 长度:${#foo}

    • (更多展开和处理 ${} 略)

    • 相对路径:./, ../

    • 绝对路径:/usr/bin/env

    • cd -, cd ~username

    • 通配符 Wildcards:*.txt, Data???, [abc], [!abc], [![:upper:]]

    • 路径

    • 展开 Expansion

    • 标准输入输出,重定向 Redirection

    • 控制编码 ANSI escape code

  • (执行)

    • -rwxr-xr-x

    • chmod +x ./file, chmod 755 ./file

    • 权限 Permissions

    • 进程操控 Processes (略)

  • 包管理

    • Debian:apt, dpkg

    • 红帽:dnf, yum, rpm

  • Shell Script

    • 字符串

    • 数字

    • 数组

    • declare, unset

    • (在展开中做各种处理)

    • +, -, *, /, **, %

    • =, +=, ++, --

    • ˜, <<, >>, &, |, ^

    • <, >, ==, !=, &&, ||

    • expr1?expr2:expr3

    • 数基/进制:$((033)), $((0x1b)), $((2#11011))

    • (各种运算符)

    • 计算器:bc <<< "2+3"

    • arr[1]=val, arr=(foo bar)

    • ${arr[@]}

    • (数组展开类似字符串展开)

    • 条件表达式 Conditional Expressions

    • if, while, continue, break, until

    • case, ;;, ;;&

    • for, done

    • test expression, [ expression ]

    • 文件表达式、字符串表达式、整型表达式

    • 字符串的扩展:[[ str =~ regex ]], [[ $FILE == foo.* ]]

    • 整数的扩展:(( INT < 0 ))

    • 逻辑操作符:&&, ||, !

    • function name {}, name () {}

    • local

    • 参数

    • 退出状态 Exit Status:0-255

    • $0, $1, $2, ${99}

    • 参数组:$#, shift, $@, $*

    • dotfiles, rc files

    • shebang:#!/usr/bin/env bash

    • source, .:在当前 shell 环境执行脚本

    • #:注释

    • : "":利用 true 写多行注释

    • misc

    • 函数

    • 控制流 Control Flow

    • read, IFS Internal Field Separator

    • 变量

  • Misc

    • read foo < <(echo "bar")

    • echo "bar" > >(read; echo "foo, $REPLY")

    • 子 Shell Subshells

    • 子进程替换 Process Substitution

代码片段

proxy

自用,有时需要切换网络连通情况,来回修改 export ALL_PROXY,所以写了一个脚本来简化流程。

因为一些 subshell 的问题,似乎不太能像其他高级语言一样只 export 一部分变量,
于是目前额外暴露出了 _proxy_{set,unset} 两个函数,搜了一圈似乎没什么解决方法,就这样吧…

# https://everything.curl.dev/usingcurl/proxies/env# * ----------------------------------------------------------------_proxy_set() {
  export ALL_PROXY=$1
  export http_proxy=$1
  export https_proxy=$1
  export HTTP_PROXY=$1
  export HTTPS_PROXY=$1}# * ----------------------------------------------------------------_proxy_unset() {
  unset ALL_PROXY  unset http_proxy  unset https_proxy  unset HTTP_PROXY  unset HTTPS_PROXY}# * ----------------------------------------------------------------proxy() {
  local localproxy='http://127.0.0.1:1080'
  local deadproxy="http://a.deadlink/"

  case "$1" in
  boot | on)
    _proxy_set $localproxy
    ;;
  dead)
    _proxy_set $deadproxy
    ;;
  off)
    _proxy_unset    ;;
  esac

  if [[ $1 == 'boot' ]]; then
    echo 'Proxy on'
  elif [[ -n $ALL_PROXY ]]; then
    echo "Proxy: $ALL_PROXY"
  else
    echo "Proxy off"
  fi}# * ----------------------------------------------------------------proxy boot

ginit

自用,给当前目录快速初始化 git。

#!/bin/bash# * ----------------------------------------------------------------if [[ $(git rev-parse --is-inside-work-tree) ]]; then
  echo "Already in a git repository"
  exit 1fi# * ----------------------------------------------------------------git_commit_init() {
  touch .gitignore  git add .gitignore  git commit -m "feat(init): initial commit"
  git tag init}# * ----------------------------------------------------------------git initif [ -f .gitignore ]; then
  TMP_FILE=$(mktemp)
  mv .gitignore $TMP_FILE

  git_commit_init  mv $TMP_FILE .gitignoreelse
  git_commit_initfi


您的支持是我们创作的动力!
温馨提示:本文作者系Terry ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://jiangweishan.com/article/Shell20230704a1.html

网友评论文明上网理性发言 已有0人参与

发表评论: