1
0
Fork 0
planet/沈浪熊猫儿/Bash脚本基础.tm

250 lines
6.1 KiB
Plaintext
Raw Normal View History

2022-08-29 19:24:27 +08:00
<TeXmacs|2.1.3>
2022-05-15 15:35:32 +08:00
<style|<tuple|article|chinese|old-dots|old-lengths>>
<\body>
2022-08-29 19:24:27 +08:00
<doc-data|<doc-title|Bash脚本基础>|<doc-author|<author-data|<author-name|沈达>>>>
2022-05-15 15:35:32 +08:00
2022-08-29 19:24:27 +08:00
<section|赋值与替换>
2022-05-15 15:35:32 +08:00
<\session|shell|default>
<\input|Shell] >
a=375
</input>
<\input|Shell] >
hello=$a
</input>
<\folded-io|Shell] >
echo $hello
<|folded-io>
375
</folded-io>
<\input|Shell] >
\;
</input>
</session>
2022-08-29 19:24:27 +08:00
<section|变量>
2022-05-15 15:35:32 +08:00
<\description>
2022-08-29 19:24:27 +08:00
<item*|内置变量>比如<verbatim|$HOME>或者<verbatim|$PWD>,更多例子可以参考
2022-05-15 15:35:32 +08:00
<shell|man 7 environ>
2022-08-29 19:24:27 +08:00
<item*|位置参数><verbatim|$0>是basename<verbatim|$@>是basename之后的所有参数
2022-05-15 15:35:32 +08:00
<tree|<verbatim|$@>|<verbatim|$1>|<verbatim|$2>|<verbatim|$3>|<verbatim|$4>|<text-dots>>
2022-08-29 19:24:27 +08:00
<item*|特殊参数><verbatim|$?>是命令、函数或者脚本本身的返回状态一般用0表示一切正常
2022-05-15 15:35:32 +08:00
</description>
2022-08-29 19:24:27 +08:00
<section|分支>
2022-05-15 15:35:32 +08:00
<\shell-code>
if [ condition1 ];then
\ \ \ \ command_series1
elif [ condition2 ];then
\ \ \ \ command_series2
else
\ \ \ \ default_command_series3
fi
</shell-code>
2022-08-29 19:24:27 +08:00
<section|循环>
2022-05-15 15:35:32 +08:00
<\description>
2022-08-29 19:24:27 +08:00
<item*|循环与Python语法相近>
2022-05-15 15:35:32 +08:00
<\shell-code>
for arg in `seq 10`
do
\ \ \ \ echo $arg
done
</shell-code>
2022-08-29 19:24:27 +08:00
<item*|循环与C语言for语法相近>
2022-05-15 15:35:32 +08:00
<\shell-code>
for ((a=1; a\<less\>=LIMIT; a++))
do
\ \ \ \ echo -n "$a "
done
</shell-code>
2022-08-29 19:24:27 +08:00
<item*|循环: 与C语言while语法相近>
2022-05-15 15:35:32 +08:00
<\shell-code>
a=1
while ((a\<less\>=LIMIT))
do
\ \ \ \ echo -n "$a "
\ \ \ \ ((a += 1))
done
</shell-code>
</description>
<section|IO>
<\shell-code>
command \<less\> input-file \<gtr\> output-file \ \ \ \ #
2022-08-29 19:24:27 +08:00
读取然后覆盖
2022-05-15 15:35:32 +08:00
command \<gtr\>\<gtr\> output-file \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ #
2022-08-29 19:24:27 +08:00
追加
2022-05-15 15:35:32 +08:00
</shell-code>
2022-08-29 19:24:27 +08:00
<section|函数>
2022-05-15 15:35:32 +08:00
<\shell-code>
2022-08-29 19:24:27 +08:00
# 如何定义函数1
2022-05-15 15:35:32 +08:00
function fun_name(){
\ \ \ \ command...
}
2022-08-29 19:24:27 +08:00
# 如何定义函数2
2022-05-15 15:35:32 +08:00
fun_name(){ # arg1 arg2 arg3
\ \ \ \ command...
}
\;
2022-08-29 19:24:27 +08:00
# 函数的应用
2022-05-15 15:35:32 +08:00
fun_name $arg1 $arg2 $arg3
\;
\;
2022-08-29 19:24:27 +08:00
# 引用解除
2022-05-15 15:35:32 +08:00
fun_name(){ # arg1
\ \ \ \ eval "$1=hello"
}
fun_name arg1
\;
2022-08-29 19:24:27 +08:00
# 上述代码片段等价于\
2022-05-15 15:35:32 +08:00
arg1=hello
</shell-code>
2022-08-29 19:24:27 +08:00
<section|调试>
2022-05-15 15:35:32 +08:00
<\itemize>
2022-08-29 19:24:27 +08:00
<item>用好sh命令
2022-05-15 15:35:32 +08:00
<\description>
2022-08-29 19:24:27 +08:00
<item*|sh -n script>检查脚本语法
2022-05-15 15:35:32 +08:00
2022-08-29 19:24:27 +08:00
<item*|sh -v script>在执行前将所有命令打印出来
2022-05-15 15:35:32 +08:00
2022-08-29 19:24:27 +08:00
<item*|sh -x script>在执行前将所有命令到打印到错误输出,并在命令前面加上<shell|+>前缀
2022-05-15 15:35:32 +08:00
</description>
2022-08-29 19:24:27 +08:00
<item>利用<shell|echo>
2022-05-15 15:35:32 +08:00
2022-08-29 19:24:27 +08:00
<item>利用<shell|trap>
2022-05-15 15:35:32 +08:00
</itemize>
2022-08-29 19:24:27 +08:00
<section|并行>
2022-05-15 15:35:32 +08:00
2022-08-29 19:24:27 +08:00
利用GNU Parallel<\footnote>
2022-05-15 15:35:32 +08:00
<slink|https://www.gnu.org/software/parallel/>
</footnote>
2022-08-29 19:24:27 +08:00
<section|代码风格>
2022-05-15 15:35:32 +08:00
2022-08-29 19:24:27 +08:00
利用ShellCheck<\footnote>
2022-05-15 15:35:32 +08:00
<slink|https://github.com/koalaman/shellcheck>
</footnote>
</body>
<\references>
<\collection>
<associate|auto-1|<tuple|1|?>>
<associate|auto-2|<tuple|2|?>>
<associate|auto-3|<tuple|3|?>>
<associate|auto-4|<tuple|4|?>>
<associate|auto-5|<tuple|5|?>>
<associate|auto-6|<tuple|6|?>>
<associate|auto-7|<tuple|7|?>>
<associate|auto-8|<tuple|8|?>>
<associate|auto-9|<tuple|9|?>>
<associate|footnote-1|<tuple|1|?>>
<associate|footnote-2|<tuple|2|?>>
<associate|footnr-1|<tuple|1|?>>
<associate|footnr-2|<tuple|2|?>>
</collection>
</references>
<\auxiliary>
<\collection>
<\associate|toc>
2022-08-29 19:24:27 +08:00
<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|1<space|2spc>赋值与替换>
2022-05-15 15:35:32 +08:00
<datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
<no-break><pageref|auto-1><vspace|0.5fn>
2022-08-29 19:24:27 +08:00
<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|2<space|2spc>变量>
2022-05-15 15:35:32 +08:00
<datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
<no-break><pageref|auto-2><vspace|0.5fn>
2022-08-29 19:24:27 +08:00
<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|3<space|2spc>分支>
2022-05-15 15:35:32 +08:00
<datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
<no-break><pageref|auto-3><vspace|0.5fn>
2022-08-29 19:24:27 +08:00
<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|4<space|2spc>循环>
2022-05-15 15:35:32 +08:00
<datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
<no-break><pageref|auto-4><vspace|0.5fn>
<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|5<space|2spc>IO>
<datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
<no-break><pageref|auto-5><vspace|0.5fn>
2022-08-29 19:24:27 +08:00
<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|6<space|2spc>函数>
2022-05-15 15:35:32 +08:00
<datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
<no-break><pageref|auto-6><vspace|0.5fn>
2022-08-29 19:24:27 +08:00
<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|7<space|2spc>调试>
2022-05-15 15:35:32 +08:00
<datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
<no-break><pageref|auto-7><vspace|0.5fn>
2022-08-29 19:24:27 +08:00
<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|8<space|2spc>并行>
2022-05-15 15:35:32 +08:00
<datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
<no-break><pageref|auto-8><vspace|0.5fn>
2022-08-29 19:24:27 +08:00
<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|9<space|2spc>代码风格>
2022-05-15 15:35:32 +08:00
<datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
<no-break><pageref|auto-9><vspace|0.5fn>
</associate>
</collection>
</auxiliary>