wangdoc / bash-tutorial Goto Github PK
View Code? Open in Web Editor NEWBash 教程
Home Page: https://wangdoc.com/bash/
Bash 教程
Home Page: https://wangdoc.com/bash/
老师你好,发现了一处笔误。
模式扩展-start-end-扩展中一处示例的echo
本意应该是ls
。
$ echo report[!1–3].txt
report4.txt report5.txt
修改为
$ ls report[!1–3].txt
report4.txt report5.txt
mac新版os使用zsh解析shell命令,数组foo=(a b c d e f) , foo[1] 表示第一个元素,foo[0]输出为空,echo ${foo} 是输出所以元素而不是第一个。
`walt@MacBook-Pro ~ % foo=(a b c d e f)
walt@MacBook-Pro ~ % echo ${foo}
a b c d e f
walt@MacBook-Pro ~ % echo ${foo[0]}
walt@MacBook-Pro ~ % echo ${foo[1]}
a`
在字符串操作章节的子字符串小节中关于length的说明
如果offset为负值,表示从字符串的末尾开始算起。注意,负数前面必须有一个空格, 以防止与${variable:-word}的变量的设置默认值语法混淆。这时,如果还指定length,则length不能小于零。
length其实是可以小于0的,但是不能比offset更小。这个时候的行为和python中字符串切片操作中效果是类似的。
cd $some_directory
if [ "$?" = "0" ]; then
rm *
else
echo "无法切换目录!" 1>&2
exit 1
fi
示例中,cd $some_directory
当变量不存在时,会cd到用户目录。
进入if的话就会误删用户文件,我的文件已被删除😳,
建议添加个提示,或者把 rm *
换成 类似ls
这种安全命令
env或printenv输出的是环境变量,而BASHPID、BASHOPTS、PS1、PS2、RANDOM通常不属于环境变量,不会被printenv输出(其实UID也不会输出,但不知为何USER会),只能被set输出。
https://stackoverflow.com/questions/41931855/why-env-does-not-print-ps1-variable
https://askubuntu.com/questions/960551/why-arent-variables-like-ps1-in-printenv
比如a.sh中有函数a1(),b.sh中有函数b1()。
a.sh的a1()怎么调用b1()
expansion.md文件里面322行是对的
$ echo /bin/{cat,b*}
https://wangdoc.com/bash/expansion.html
网站上显示的是
$ echo /bin/{cat,d*}
b* 和 d*
你好, 偶是小白,不知道咋用啊?直接打开doc中的文件看吗?有没有其他方式?
https://wangdoc.com/bash/ 以及 wangdoc.com/bash/ 均无法打开
阮老师好,大家好:
我对条件判断这篇的几个小建议,但我也是shell新手,不确定以下的建议是否合理。
[
和[[
的差异还是很多的,建议将第2小节开头介绍的句子上面三种形式是等价的,但是第三种形式还支持正则判断,前两种不支持。
修改为
上面三种形式是等价的,主要的差异是前两种是POSIX的标准
test
命令,第三种是bash的扩展语法,它的表达式语法更接近一般的高级语言,甚至支持正则判断
test
例子优化if [ ! \( $INT -ge $MIN_VAL -a $INT -le $MAX_VAL \) ]; then
echo "$INT is outside $MIN_VAL to $MAX_VAL."
else
echo "$INT is in range."
fi
这里通过-a
选项来表达且
已经不被POXIS推荐了, 原文如下:
That is, in cases where maximal portability is of concern, replace:
test expr1 -a expr2
with:
test expr1 && test expr2
所以例子最好也采用标准推荐的写法
if ! ( [ $INT -ge $MIN_VAL ] && [ $INT -le $MAX_VAL ] ); then
echo "$INT is outside $MIN_VAL to $MAX_VAL."
else
echo "$INT is in range."
fi
[
和[[
的全部差异?因为那个网页我用https进不去,http能进去,但是只能看到第一页,点其他链接又转圈圈加载失败了,所以想本地运行一下,感谢
所以又称为命令行环境(commandline,简写为 CLI)
这个应该是 Command Line Interface 的简写,与GUI 对应 Graphics User Interface
set章节
设置-e参数,会导致trap命令失效(参考《trap 命令》一章),-E参数可以纠正这个行为。
对于-E参数的解释不同于官方手册
-E If set, any trap on ERR is inherited by shell functions, command substitutions, and commands executed in a subshell environment. The ERR trap is normally not in‐herited in such cases.
例如,函数中的错误是不会触发err trap,设置-E以后,函数中的错误也可以触发err trap
示例中的err trap是因为myfunc的调用返回值非0,所以触发err trap,如果在myfunc中添加return 0,就不会有err trap
https://wangdoc.com/bash/condition.html
此处测试失败[ -n string ]:如果字符串string的长度大于零,则判断为真
[[ -n string ]] 须要两个括号才行,我在devuan,bash下测试的
比如自动备份脚本,市面上的一键脚本讲解、性能分析。
谢谢
rt,小白不太明白,麻烦指导下,谢谢
Here文档似乎不会进行通配符扩展(毕竟不是命令参数),但会对\转义,以及`command`
子命令扩展。
用搜索 cp 或者 rm 才看到原始文档中有关于文件操作的
Bash Documents中有这两段:
A Unix shell is both a command interpreter and a programming language.
As a command interpreter, the shell provides the user interface to the rich set of GNU utilities.
The programming language features allow these utilities to be combined. Files containing commands can be created, and become commands themselves.
command interpreter <=> user interface
programming language <=> combine these utilities
与本文中:
具体来说,Shell 这个词有多种含义。
首先,Shell 是一个程序,提供一个与用户对话的环境。这个环境只有一个命令提示符,让用户从键盘输入命令,所以又称为>命令行环境(command line interface,简写为 CLI)。Shell 接收到用户输入的命令,将命令送入操作系统执行,并将结果返回给用户。本书中,除非特别指明,Shell 指的就是命令行环境。
其次,Shell 是一个命令解释器,解释用户输入的命令。它支持变量、条件判断、循环操作等语法,所以用户可以用 Shell 命令写出各种小程序,又称为脚本(script)。这些脚本都通过 Shell 的解释执行,而不通过编译。
感觉这两段描述存在出入,是否与原文交错了?
我也刚学,所以有此疑问。
https://wangdoc.com/bash/expansion.html#%E5%A4%A7%E6%8B%AC%E5%8F%B7%E6%89%A9%E5%B1%95
echo {cat,d*}
为什么会额外输出 dawg dg dig dog doug dug
呢?
5.4最后一行
echo "I say:"hello!""
结果应该是I say:"hello!"
文中笔误多了个\
我执行命令的时候,发现 * 可以匹配隐藏文件,如下
ubuntu server 18.04,bash 4.4.20
liyongjun@box:~/tmp$ la
a .a abc
liyongjun@box:~/tmp$ echo .*
. .. .a
centos 7,bash 4.2.46
[centli@VM_0_14_centos tmp]$ la
.a a.txt
[centli@VM_0_14_centos tmp]$ echo .*
. .. .a
Command1 || Command2
上面命令的意思是,如果
Command1
命令运行失败,则继续运行Command2
命令。
引用自 L155-L159
应该是:不管Command1
命令运行失败与否,Command2
命令都会执行。
$ mkdir foo || mkdir bar
上面例子中,只有
mkdir foo
命令执行失败(比如foo
目录已经存在),才会继续执行mkdir bar
命令。如果mkdir foo
命令执行成功,就不会创建bar
目录了
引用自 #L175-L179
应该是:即使mkdir foo
命令执行失败,也会执行继续执行mkdir bar
命令
例子
~/temp/mkdir-test
➜ ls
~/temp/mkdir-test
➜ mkdir foo
~/temp/mkdir-test
➜ ls
foo
~/temp/mkdir-test
➜ mkdir foo || mkdir bar
mkdir: 无法创建目录 “foo”: 文件已存在
~/temp/mkdir-test
➜ ls
bar foo
建议以PDF格式推广,便于本地保存查阅
阮老师,目前国内无法正常访问该站点
希望能够在网道上面看到有关HTTP、TCP/IP等网络协议的教程,这可以填补互联网上中文教程的空缺。
上面代码中,$FILE要放在双引号之中。这样可以防止$FILE为空,因为这时[ -e ]会判断为真。而放在双引号之中,返回的就总是一个空字符串,[ -e "" ]会判断为伪。 这句表达的有问题
如果对数组元素赋值空字符串,不能起到删除元素的效果
a=(x y z)
a[1]=''
declare -p a
echo ${#a[@]} # 还是3
echo ${!a[@]} # 0 1 2
Hi
首先感谢网道提供的优秀文档。
在学习过程中发现了一个小小的问题
https://wangdoc.com/bash/loop.html#select-%E7%BB%93%E6%9E%84
select中选项为"Windows8 Windows7 WindowsXP"
case中选项为""Windows8" | "Windows10" | "WindowsXP")"
导致选择win7时意外退出
希望网道越来越好
比如想要查看文件操作命令rm的使用方法,在网道web页无法找到对应目录。但通过查找,可以在源码处看到相应的文档。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.