当前位置:网站首页>shell编程01_Shell基础
shell编程01_Shell基础
2022-07-02 06:48:00 【sinat_36070482】
达内教育
最新版Linux Shell脚本全套,终于讲明白了!
丁明一老师
shell编程01_Shell基础

shell命令 需要shell命令解释器
常见的shell解释器
/bin/bash
/bin/sh
/bin/csh
/bin/tcsh
解释器:将用户的指令翻译为内核可以识别的指令
通过usermod、chsh可以更改登录shell
创建临时的用户的时候,如果我们没有指定指定解释器的时候,
默认的解释器为/bin/bash
[email protected]:/home/ubuntu/songweibin/shell# useradd test1
[email protected]:/home/ubuntu/songweibin/shell# grep test1 /etc/passwd
test1:x:1001:1001::/home/test1:/bin/sh
usermod chsh 来修改解释器
通过 /etc/shells
可以看到支持的shell解释器
[email protected]:/home/ubuntu/songweibin/shell# cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash我们也可以安装更多的shell解释器
[email protected]:/home/ubuntu/songweibin/shell# apt-get install tcsh
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-5.11.0-27-generic linux-hwe-5.11-headers-5.11.0-27
linux-image-5.11.0-27-generic linux-modules-5.11.0-27-generic
linux-modules-extra-5.11.0-27-generic
Use 'apt autoremove' to remove them.
The following NEW packages will be installed:
tcsh
0 upgraded, 1 newly installed, 0 to remove and 90 not upgraded.
Need to get 427 kB of archives.
After this operation, 1,363 kB of additional disk space will be used.
Get:1 http://cn.archive.ubuntu.com/ubuntu focal/universe amd64 tcsh amd64 6.21.00-1 [427 kB]
Fetched 427 kB in 7s (61.1 kB/s)
Selecting previously unselected package tcsh.
(Reading database ... 227919 files and directories currently installed.)
Preparing to unpack .../tcsh_6.21.00-1_amd64.deb ...
Unpacking tcsh (6.21.00-1) ...
Setting up tcsh (6.21.00-1) ...
update-alternatives: using /bin/tcsh to provide /bin/csh (csh) in auto mode
Processing triggers for man-db (2.9.1-1) ...
再次查看支持的shell解释器
[email protected]:/home/ubuntu/songweibin/shell# cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/bin/tcsh
/usr/bin/tcsh
test1 用户默认shell解释器为/bin/bash
修改默认的shell解释器方法
usermod -s /bin/tcsh test
[email protected]:/home/ubuntu/songweibin/shell# usermod -s /bin/tcsh test1
[email protected]:/home/ubuntu/songweibin/shell# grep test1 /etc/passwd
test1:x:1001:1001::/home/test1:/bin/tcsh
可以使用chsh -s /bin/bash test1 修改用户默认shell解释器
chsh:change shell的意思
[email protected]:/home/ubuntu/songweibin/shell# chsh -s /bin/bash test1
[email protected]:/home/ubuntu/songweibin/shell# grep test1 /etc/passwd
test1:x:1001:1001::/home/test1:/bin/bash
为什么默认支持/bin/bash 呢?
因为Bash 很多功能和特性
快捷键 Tab键补齐
快捷键:
Ctrl + a 命令行的最前面
Ctrl + e 命令行的最后面
Ctrl + C 当前执行的命令撤销
Ctrl + l 清屏
Tab键补齐:命令补齐 选项都可以补齐
命令历史(history)
通过上下键翻阅历史
命令的别名(aliase)
ll= ls-l
标准输入与输出的重定向(>、 >>、2>、2>>、&>、 &>>)
> : 正确的信息重定向
2>: 错误的信息重定向
&>: 既要正确的信息又要错误的信息
[email protected]:/home/ubuntu/songweibin# ls
42fputcandwrite 84fatherandsoncommunication fl
82testpipe 85brotherscommunication shell
83mypipe 87fileforprocesscommunication test
[email protected]:/home/ubuntu/songweibin# ls > a.txt
[email protected]:/home/ubuntu/songweibin# cat a.txt
42fputcandwrite
82testpipe
83mypipe
84fatherandsoncommunication
85brotherscommunication
87fileforprocesscommunication
a.txt
fl
shell
test为什么还有a.txt自己呢?
ls > a.txt 命令是都是先创建 a.txt文件
然后将ls 的结果输入到a.txt中
如果使用两个大于号的时候,是表示追加
是在原有内容的基础上追加内容
[email protected]:/home/ubuntu/songweibin# ls >>a.txt
[email protected]:/home/ubuntu/songweibin# cat a.txt
42fputcandwrite
82testpipe
83mypipe
84fatherandsoncommunication
85brotherscommunication
87fileforprocesscommunication
a.txt
fl
shell
test
42fputcandwrite
82testpipe
83mypipe
84fatherandsoncommunication
85brotherscommunication
87fileforprocesscommunication
a.txt
fl
shell
test
[email protected]:/home/ubuntu/songweibin#
使用2>将错误信息导出到指定文件
[email protected]:/home/ubuntu/songweibin# ls song.txt
ls: cannot access 'song.txt': No such file or directory
[email protected]:/home/ubuntu/songweibin# 使用>只能输出正确的信息。
[email protected]:/home/ubuntu/songweibin# ls song.txt > a.txt
ls: cannot access 'song.txt': No such file or directory
[email protected]:/home/ubuntu/songweibin# cat a.txt使用2>将错误信息导出到指定文件
[email protected]:/home/ubuntu/songweibin# ls song.txt 2> a.txt
[email protected]:/home/ubuntu/songweibin# cat a.txt
ls: cannot access 'song.txt': No such file or directory也可以使用&> (也是重新创建文件,会覆盖)可以同时输出正确和错误的信息
单>: 都是重新创建输出的文件,所以会覆盖
双>:都是追加
管道(|)
可以将第一条命令的输出结果作为第二条命令的输入
搜索安装过的软件:
[email protected]:/home/ubuntu/songweibin# apt-cache search all|grep tcsh
tcsh - TENEX C Shell, an enhanced version of Berkeley csh
[email protected]:/home/ubuntu/songweibin# apt-get install ksh
添加新的解释器
[email protected]:/home/ubuntu/songweibin# cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/bin/tcsh
/usr/bin/tcsh
/usr/bin/ksh2020
/usr/bin/rksh2020
通过ksh 直接使用ksh解释器
ksh中只能通过clear 来清屏
无法通过Ctrl+l
ksh 无法使用方向键 所以默认使用/bin/bash
# ^[[C^[[C^[[C^[[C^[[C^[[D^[[D^[[D^[[D^[[A^[[A^[[A^[[B^[[B
ksh 通过exit返回上次默认的解释器
# exit
[email protected]:/home/ubuntu/songweibin# Shell执行命令的方式
1.交互式(命令行)
人工干预
逐条解释执行 效率低
2.非交互式(脚本)
需要提前设计
批量执行 效率高
边栏推荐
- Blender多鏡頭(多機比特)切換
- Importing tables from sqoop
- Is this code PHP MySQL redundant?
- js setTimeout()与面试题
- 【Visual Studio】每次打开一个Unity3D的脚本,都会自动重新打开一个新的VS2017
- Configuration programmée du générateur de plantes du moteur illusoire UE - - Comment générer rapidement une grande forêt
- Windows环境MySQL8忘记密码文件解决方案
- Postman--使用
- Vscode auto format
- 01-spooldir
猜你喜欢

数据库字典Navicat自动生成版本

Flink submitter

Commutateur Multi - lentilles Blender

Blender stone carving

Flink calculates topn hot list in real time

Unreal material editor foundation - how to connect a basic material

How to judge the quality of primary market projects when the market is depressed?

01安装虚拟机

Determine whether there are duplicate elements in the array

Remember the use of add method once
随机推荐
This article takes you to learn in detail what is fiber to home FTTH
[200 Shengxin literatures] 96 joint biomarkers of immune checkpoint inhibitor response in advanced solid tumors
[MySQL] an exception occurs when connecting to MySQL: connection must be valid and open
Transport Optimization abstraction
Blender摄像机环绕运动、动画渲染、视频合成
互联网快讯:腾讯会议应用市场正式上线;Soul赴港递交上市申请书
【Visual Studio】每次打开一个Unity3D的脚本,都会自动重新打开一个新的VS2017
Webui automated learning
[Lua] summary of common knowledge points (including common interview sites)
[200 Shengxin literatures] 95 multiomics exploration TNBC
传输优化抽象
【Unity3D】嵌套使用Layout Group制作拥有动态子物体高度的Scroll View
Importing tables from sqoop
Project practice, redis cluster technology learning (16)
14.信号量的代码实现
2.14 is it Valentine's day or Valentine's day when the mainstream market continues to fluctuate and wait for changes?
pytest--之测试报告allure配置
[unity3d] cannot correctly obtain the attribute value of recttransform, resulting in calculation error
【避坑指南】Unity3D项目接入腾讯Bugly工具时遇到的坑
Lunix reallocates root and home space memory