Shell MIT shell 课程
在MIT的Shell和Scripting课程中,学生将学习如何利用shell工具来提高工作效率。以下是一些建议,展示如何在使用shell时提升效率:
- 熟悉常用的Shell命令:
深入了解cd、ls、mv、cp、rm等基本命令。
学会使用man命令查看命令的手册页,了解命令的详细用法和选项。 - 使用别名(Aliases):
通过为常用命令设置别名,可以减少输入量。例如,alias ll='ls -la’可以将ll命令设置为列出当前目录的详细文件和目录列表。 - 编写脚本(Scripting):
利用shell的脚本功能,将一系列命令组合成一个文件,并通过执行该文件来自动完成一系列任务。
使用函数来模块化代码,提高代码的可读性和可维护性。 - 利用文本处理工具:
grep、awk、sed等文本处理工具可以大大提高处理文本数据的效率。
学习如何结合使用这些工具来处理日志文件、配置文件等。 - 使用自动补全和历史命令:
大多数shell都支持命令自动补全功能,通过按Tab键可以快速完成命令的输入。
利用历史命令功能(通常通过上下箭头键访问),可以快速找到并重新执行之前执行过的命令。 - 利用通配符和正则表达式:
学会使用通配符(如*、?、[])来匹配文件名和目录名。
学习正则表达式的基础知识,以便在文本搜索和处理中更精确地匹配模式。 - 使用管道和重定向:
利用管道(|)将一个命令的输出作为另一个命令的输入,从而实现命令的串联。
使用重定向(>、>>、<)来改变命令的输入输出方向,以便将输出保存到文件或将文件作为输入。 - 学习更高级的shell:
除了bash之外,还有其他一些更高级的shell可供选择,如zsh、fish等。这些shell提供了更多的功能和更好的用户体验。 - 使用版本控制工具:
对于重要的shell脚本,使用版本控制工具(如Git)来跟踪和管理脚本的更改历史。这有助于在出现问题时快速回滚到之前的版本。 - 持续学习和实践:
不断学习和探索新的shell工具和技巧,将其应用到实际工作中。通过实践来加深对shell的理解和提高使用效率。
MIT 6.NULL课程
https://missing.csail.mit.edu/ ,介绍了如何利用工具提升效率
Lecture1. Course overview + the shell
- shell:空格分割输入,
~
is short for “home”,.
表示当前文件夹.
在UNIX系统的遗留问题
- environment variable:
echo $PATH
;vim ~/.zshrc
$PATH
可以作为输入
- connecting programs:
- <和>:rewire the input and output streams; >>可append
cat < hello.txt > hello2.txt
- wire:
ls -l | tail -n1
,`` curl --head --silent baidu.com | grep --ignore-case content-length | cut -f2 -d ' '
- sudo: super user,linux系统可改/sys下面的sysfs
echo 1 | sudo tee /sys/class/leds/input6::scrolllock/brightness
Lecture2. Shell Tools and Scripting
shell scripting
- foo=bar, $foo 注意等号前后不能有space,否则被当成参数
- 单引号和双引号的区别:同样套在$foo上,前者是literal meaning,而" "会替换成变量值
- shell scripting也有if、case、while、for、function特性
- source mcd.sh后即可使用。cd如果在function内部使用,针对的是子shell,不影响外部,因此直接用./mcd.sh不合适
#!/bin/bash
mcd(){
mkdir -p "$1"
cd "$1"
}
- if else
- for特性的实用例子
POLICIES=("FIFO" "LRU" "OPT" "UNOPT" "RAND" "CLOCK")
for policy in "${POLICIES[@]}"
do
for i in 1 2 3 4
do
./paging-policy.py -c -f ./vpn.txt -p "$policy" -C "$i"
done
echo ""
done
- case-esac 语句的使用
while [ "$#" -gt 0 ]; do
case $1 in
-c | --clang)
clang=1
;;
-g | --gcc)
gcc=1
;;
--lto)
lto=1
;;
--thinlto)
thinlto=1
;;
esac
shift
done
set -e
:遇到错误,shell script 直接退出shift
: 含义是$0
不移动,将$1
起始的若干参数干掉
special variables
$0
- Name of the script$1 to \$9
- Arguments to the script. $1 is the first argument and so on.$@
- All the arguments$*
同义,区别是它在“”包围下会当成一个参数
$#
- Number of arguments- 最后一个参数:
${!#}
- 最后一个参数:
$?
- Return code of the previous command$$
- Process Identification number for the current script!!
- Entire last command, including arguments. A common pattern is to execute a command only for it to fail due to missing permissions, then you can quickly execute it with sudo by doing sudo !!$_
- Last argument from the last command. If you are in an interactive shell, you can also quickly get this value by typing Esc followed by .$!
- last backgrounded job- ||和&& operator:机制和 error code 联系,true 和 false 命令返回固定的error code
- linux中,&和&&, |和|| ,&> 与 >的区别
false || sleep 10s
固定error code为零
false || echo "Oops, fail"
# Oops, fail
true || echo "Will not be printed"
#
true && echo "Things went well"
# Things went well
false && echo "Will not be printed"
#
false ; echo "This will always run"
# This will always run
shell中 ${}, ##, %%, :- , :+, ? 的使用
percent=${PERCENT:-50}
Linux-shell中各种替换的辨析
- variable substitution:
$var, ${var}
- command substitution:
for file in $(ls)
,可以用' '
代替$( )
,但后者辨识度更高 - process substitution: 生成返回temporary file,
diff <(ls foo) <(ls bar)
#!/bin/bash
echo "Starting program at $(date)" # Date will be substituted
echo "Running program $0 with $# arguments with pid $$"
for file in $@; do
grep foobar $file > /dev/null 2> /dev/null
# When pattern is not found, grep has exit status 1
# We redirect STDOUT and STDERR to a null register since we do not care about them
if [[ $? -ne 0 ]]; then
echo "File $file does not have any foobar, adding one"
echo "# foobar" >> "$file"
fi
done
- 2>,重定向 stderr
- &> 或 >&,重定向到 stderr
&>word
<=>>word 2>&1
$command > result 2>&1
,STDOUT、STDERR 均重定向到 result
-ne
,更多的查看man test,比如-n 文件存在为真 -z 不存在为真
- “test command”, [[和[的区别:http://mywiki.wooledge.org/BashFAQ/031 ,
[[
是compound command,存在special parsing context,寻找reserved words or control operatorsif [[ -e $file ]] && [[ $var == true]]
--
- to signify the end of command options
grep -- -v file
shell globbing 通配
-
wildcard通配符:?和*
ls *.sh
-
{}:
mv *{.py,.sh} folder
,mv abc{000..120}* folder
-
touch {foo,bar}/{a..h}
-
利用shellcheck检查shell scripts的错误
-
shebangline 进行解释,可以利用env命令
#!/usr/bin/env python
#!/usr/bin/env -S /usr/local/bin/php -n -q -dsafe_mode=0
shell 引号嵌套
https://zhuanlan.zhihu.com/p/146462733
最简单的方法就是单双引号交替
shell函数和scripts的区别:
- Functions have to be in the same language as the shell, while scripts can be written in any language. This is why including a shebang for scripts is important.
- Functions are loaded once when their definition is read. Scripts are loaded every time they are executed. This makes functions slightly faster to load but whenever you change them you will have to reload their definition.
- Functions are executed in the current shell environment whereas scripts execute in their own process. Thus, functions can modify environment variables, e.g. change your current directory, whereas scripts can’t. Scripts will be passed by value environment variables that have been exported using
export
- 比如cd只能在function中影响到外界shell
- As with any programming language functions are a powerful construct to achieve modularity, code reuse and clarity of shell code. Often shell scripts will include their own function definitions.
实用技巧
-
alias ll='ls -aGhlt'
-
marco记录directory,polo前往
#!/bin/bash
marco(){
foo=$(pwd)
export MARCO=$