首页 / LINUX / linux的shell编程
linux的shell编程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了linux的shell编程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4911字,纯文字阅读大概需要8分钟。
内容图文
![linux的shell编程](/upload/InfoBanner/zyjiaocheng/1215/0a17e7fe6f0a4c759bc47fe2cab22403.jpg)
1.什么是shell
命令解释器,将abcd翻译成010101发给内核,让机器看懂,内核处理后,返回101010,shell反编译成dcba呈现给终端,让人看懂。
2.shell分类
shell类别 | 易学性 | 可移植性 | 编辑性 | 快捷性 |
Bourne Shell -- sh | 容易 | 好 | 较差 | 较差 |
Korn Shell -- ksh | 较难 | 较好 | 好 | 较好 |
Bourne Again -- Bash | 难 | 较好 | 好 | 好 |
POSIX Shell -- psh | 较难 | 好 | 好 | 较好 |
C Shell -- csh | 较难 | 差 | 较好 | 较好 |
TC Shell -- tcsh | 难 | 差 | 好 | 好 |
linux支持哪几种shell
centos7
ubuntu16.04
3. echo 输出命令
echo -e 输入转义字符
root@ubuntu:/etc/network# echo -e "\\" \
\t 加制表符,需要和-e搭配使用
root@ubuntu:/etc/network# echo -e "a\tb\tc" a b c
\n 换行,需要和-e搭配使用
root@ubuntu:/etc/network# echo -e "a\tb\tc\nd"a b c d
输出颜色 \e[1; 颜色值 abcd \e[0m 30m黑色,31m红色,32m绿色,33m黄色,34m青色,37m白色
4. 脚本执行方法
root@ubuntu:/home/yanyanzhang/shell_study# cat shell01.sh #!/bin/bash echo -e "\e[1;31m hello world \e[0m"
执行方法一:chmod +x shell01.sh 通过路径执行 ./shell01.sh
执行方法二:bash或sh ./shell01.sh 通过命令执行,可以不赋予执行权限
4.Bash的基本功能
history 查看历史命令 history-c 清空历史命令文件 history-w 将当次命令强行写入文件中
输入输出重定向
例如:将输出在屏幕上的内容输出到文件中
> 覆盖
>> 追加,会换行追加
root@ubuntu:/home/yanyanzhang/shell_study# echo "hello world" > ./shell02.txt root@ubuntu:/home/yanyanzhang/shell_study# cat shell02.txt hello world root@ubuntu:/home/yanyanzhang/shell_study# echo "hello world" >> ./shell02.txt root@ubuntu:/home/yanyanzhang/shell_study# cat shell02.txt hello world hello world
xx
root@ubuntu:/home/yanyanzhang/shell_study# pwd > shell02.txt root@ubuntu:/home/yanyanzhang/shell_study# cat shell02.txt /home/yanyanzhang/shell_study root@ubuntu:/home/yanyanzhang/shell_study# pwd >> shell02.txt root@ubuntu:/home/yanyanzhang/shell_study# cat shell02.txt /home/yanyanzhang/shell_study /home/yanyanzhang/shell_study
重定向最典型的应用就是日志
输入报错信息
2>> 追加形式,但是怎么知道命令是错误的呢,因此不是很方便
常用这条命令输出错误和正确信息, 命令 &>> 文件
root@ubuntu:/home/yanyanzhang/shell_study# xxxx &>> shell02.txt root@ubuntu:/home/yanyanzhang/shell_study# cat shell02.txt xxxx: command not found root@ubuntu:/home/yanyanzhang/shell_study# hostname &>> shell02.txt root@ubuntu:/home/yanyanzhang/shell_study# cat shell02.txt xxxx: command not found ubuntu root@ubuntu:/home/yanyanzhang/shell_study#
还有就是正确和错误分开输出到不同文件,这种更加灵活,正确与错误分开记录。
root@ubuntu:/home/yanyanzhang/shell_study# ls >> success.txt 2>> error.txt root@ubuntu:/home/yanyanzhang/shell_study# lserror.txt shell01.sh shell02.txt success.txt root@ubuntu:/home/yanyanzhang/shell_study# cat success.txt error.txt shell01.sh shell02.txt success.txt root@ubuntu:/home/yanyanzhang/shell_study# cat error.txt
多命令执行
顺序执行:; 间隔
root@ubuntu:/home/yanyanzhang/shell_study# ls ; pwderror.txt shell01.sh shell02.txt success.txt /home/yanyanzhang/shell_study root@ubuntu:/home/yanyanzhang/shell_study#
前面正确执行后面才会执行 &&
root@ubuntu:/home/yanyanzhang/shell_study# ls && echo "success"error.txt shell01.sh shell02.txt success.txt success root@ubuntu:/home/yanyanzhang/shell_study# xxx && echo "success" No command ‘xxx‘ found, did you mean: Command ‘xxd‘from package ‘vim-common‘ (main) Command ‘x2x‘from package ‘x2x‘ (universe) Command ‘xdx‘from package ‘xdx‘ (universe) Command ‘xx‘from package ‘fex-utils‘ (universe) xxx: command not found root@ubuntu:/home/yanyanzhang/shell_study#
前面报错后面执行,前面正确后面不执行 ||
root@ubuntu:/home/yanyanzhang/shell_study# sssss || echo "ffff" sssss: command not found ffff root@ubuntu:/home/yanyanzhang/shell_study# ls || echo "ffff" error.txt shell01.sh shell02.txt success.txt
&& 和 || 组合使用,可以知道命令是否正确执行,必须先&& 后 ||
root@ubuntu:/home/yanyanzhang/shell_study# ls && echo "sucess" || echo "fail"error.txt shell01.sh shell02.txt success.txt sucess root@ubuntu:/home/yanyanzhang/shell_study# lsssss && echo "sucess" || echo "fail" lsssss: command not found fail
其实可以单分支取代if
管道符
行提取命令:grep
过滤 grep xxx /xx/xx/test.txt 从test.txt中过滤出xxx后输出
root@ubuntu:/home/yanyanzhang/Desktop# grep a ./haha.py print("haha")
-A 3 将匹配的内容后3行也输出
root@ubuntu:/home/yanyanzhang/Desktop/docker_flask/flask_demo# grep -A 3 app ./start.py app = Flask(__name__) @app.route("/") def index(): return jsonify({"message":"hello flaski"}) -- app.run(port=5001)
-n 输出行号
root@ubuntu:/home/yanyanzhang/Desktop/docker_flask/flask_demo# grep -n app ./start.py 3:app = Flask(__name__) 6:@app.route("/") 14: app.run(port=5001)
-c 找到复合条件字符串的个数
root@ubuntu:/home/yanyanzhang/Desktop/docker_flask/flask_demo# grep -c app ./start.py 3
grep和管道符| 配合使用
root@ubuntu:/home/yanyanzhang/Desktop/docker_flask/flask_demo# netstat -an | grep 80 tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN unix 3 [ ] STREAM CONNECTED 31680 unix 3 [ ] STREAM CONNECTED 33280 unix 3 [ ] STREAM CONNECTED 36080 unix 3 [ ] STREAM CONNECTED 33180 /run/systemd/journal/stdout unix 3 [ ] STREAM CONNECTED 28032 unix 3 [ ] STREAM CONNECTED 32803 unix 2 [ ] DGRAM 60580
通配符
. * ? []
其他特殊符号
单引号 ‘‘ :所有的特殊符号都没有含义,如$,`等都没有含义。
双引号 "" :所有的特殊符号都没有含义,除了 $, `,\例外,$调用变量值,`引用命令,\转义符
反引号 `` :反引号包括起来的是系统命令,在Bash中会先去执行它,和$()作用一样,不过推荐使用$(),因为反引容易看错。
# TODO
原文:https://www.cnblogs.com/meloncodezhang/p/14726734.html
内容总结
以上是互联网集市为您收集整理的linux的shell编程全部内容,希望文章能够帮你解决linux的shell编程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。