Shell基础

[root@mook ~]# echo $SHELL
/bin/bash
[root@mook ~]# cat /etc/shells 
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
[root@mook ~]# sh
sh-4.1# exit
exit

这里写图片描述
这里写图片描述

[root@mook ~]# echo -e "hell\bo"
helo
[root@mook ~]# echo -e "h\te\tl\nlo"
h   e   l
lo

红色显示:

[root@mook ~]# echo -e "\e[1;31m 按时打算 \e[0m"
 按时打算 

这里写图片描述

脚本执行方式:

这里写图片描述

[root@mook ~]# vi hello.sh
[root@mook ~]# bash hello.sh
 hello moook! 
[root@mook ~]# chmod 755 hello.sh 
[root@mook ~]# ./hello.sh 
 hello moook! 
[root@mook ~]# /root/hello.sh 
 hello moook! 

命令别名:

[root@mook ~]# alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@mook ~]# alias ls='ls --color=never'
[root@mook ~]# alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=never'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

这里写图片描述

[root@mook ~]# vi /root/.bashrc 
[root@mook ~]# source .bashrc 

这里写图片描述

这里写图片描述

历史命令:

这里写图片描述

-c清空缓存中的历史命令。
~/.bash_history保存正常退出logout的之前的命令。
这里写图片描述

这里写图片描述

输出重定向:

这里写图片描述

这里写图片描述

这里写图片描述

注意:命令空格!
丢弃显示结果到黑洞:

[root@mook ~]# ls &>/dev/null

这里写图片描述

[root@mook ~]# wc anaconda-ks.cfg 
  51  121 1208 anaconda-ks.cfg

这里写图片描述

[root@mook ~]# ls
anaconda-ks.cfg  dir.tar.bz2  file      install.log
dir              dir.tar.gz   hello.sh  install.log.syslog
[root@mook ~]# wc < anaconda-ks.cfg 
  51  121 1208
[root@mook ~]# wc << asd
> dffgg
> 
> dfsdfsf
> weer
> 
> dv
> 
> asd
 7  4 25

管道符:

这里写图片描述
查看命令所花时间:

[root@mook ~]# date ; ls ; date

对于“;”,不管中间有没有出错,后续命令继续执行。

[root@mook ~]# ls || echo yes ; echo no
anaconda-ks.cfg  dir.tar.bz2  file      install.log
dir              dir.tar.gz   hello.sh  install.log.syslog
no
[root@mook ~]# ls && echo yes || echo no
anaconda-ks.cfg  dir.tar.bz2  file      install.log
dir              dir.tar.gz   hello.sh  install.log.syslog
yes
[root@mook ~]# ls dsasd && echo yes || echo no
ls: 无法访问dsasd: 没有那个文件或目录
no
[root@mook ~]# ls || echo yes || echo no
anaconda-ks.cfg  dir.tar.bz2  file      install.log
dir              dir.tar.gz   hello.sh  install.log.syslog

这里写图片描述

输出重定向,再用more命令查看文件内容:

[root@mook ~]# ls -l /etc/ > abc
[root@mook ~]# more abc

使用管道符:

[root@mook ~]# ls -l /etc/ | more

判断服务器连接了多少客户端:

[root@mook ~]# netstat -an | grep ESTABLISHED
tcp        0     52 192.168.0.108:22            192.168.0.106:49529         ESTABLISHED 
[root@mook ~]# netstat -an | grep ESTABLISHED | wc -l
1

通配符:

这里写图片描述

[root@mook ~]# ll dir
总用量 0
-rw-r--r--. 1 root root 0 8月   1 00:22 file1
-rw-r--r--. 1 root root 0 8月   1 00:22 file2
-rw-r--r--. 1 root root 0 8月   1 00:22 file3
[root@mook ~]# ll dir*
-rw-r--r--. 1 root root  171 8月   1 01:14 dir.tar.bz2
-rw-r--r--. 1 root root  168 8月   1 01:13 dir.tar.gz

dir:
总用量 0
-rw-r--r--. 1 root root 0 8月   1 00:22 file1
-rw-r--r--. 1 root root 0 8月   1 00:22 file2
-rw-r--r--. 1 root root 0 8月   1 00:22 file3

这里写图片描述

变量:

[root@mook ~]# a = 123
-bash: a: command not found
[root@mook ~]# a=123
[root@mook ~]# echo $a
123
[root@mook ~]# echo '$a'
$a
[root@mook ~]# echo "$a"
123

反引号内的系统命令先执行,再把执行的结果赋给变量:

[root@mook ~]# aa=ls
[root@mook ~]# echo "$aa"
ls
[root@mook ~]# aa=`ls`
[root@mook ~]# echo "$aa"
abc
anaconda-ks.cfg
dir
dir.tar.bz2
dir.tar.gz
file
hello.sh
install.log
install.log.syslog

推荐使用$():

[root@mook ~]# bb=$(ls)
[root@mook ~]# echo $bb
abc anaconda-ks.cfg dir dir.tar.bz2 dir.tar.gz file hello.sh install.log install.log.syslog
[root@mook ~]# echo \$bb
$bb
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值