[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