linux学习之路---Shell基础
一.Shell概念
Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。
Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。
Shell 编程跟 java、php 编程一样,只要有一个能编写代码的文本编辑器和一个能解释执行的脚本解释器就可以了。
Linux 的 Shell 种类众多,常见的有:
Bourne Shell(/usr/bin/sh或/bin/sh)
Bourne Again Shell(/bin/bash)
C Shell(/usr/bin/csh)
K Shell(/usr/bin/ksh)
Shell for Root(/sbin/sh)
……
二.第一个shell脚本
例子:
1.创建一个文件 vim hello.sh
2.在文件里写入
#!/bin/bash
echo "你好"
echo "你好"
3.执行脚本
[[email protected] ~]# bash hello.sh
你好
三.别名与常用快捷键
[[email protected] ~]# 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 vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
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 vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
常用快捷键:
四.历史命令
五.输出重定向
实例1:
[[email protected] ~]# lsss &>> test.log#正确错误同时可以写入
[[email protected] ~]# cat test.log
a
anaconda-ks.cfg
b
hello.sh
install.log
install.log.syslog
test.log
-bash: lsss: command not found
实例二:
[[email protected] ~]# lss >>true.log 2>>error.log #将正确的错误的分别写入不同的文件
[[email protected] ~]# cat true.log
[[email protected] ~]# cat error.log
-bash: lss: command not found
[[email protected] ~]# cat true.log
[[email protected] ~]# cat error.log
-bash: lss: command not found
注意:正确的>> 后是空格,错误2>>后面无空格
实例1:
输入完毕按ctrl+d
实例2:
[[email protected] ~]# wc error.log
1 5 30 error.log
1 5 30 error.log
统计error.log的字符的个数
六.管道符
[[email protected] ~]# ls && echo yes || echo no
a anaconda-ks.cfg error.log hello.sh install.log install.log.syslog true.log
yes
当命令正确时候输出yes ,当命令错的时候输出no
案例:
[[email protected] ~]# netstat -an | grep ESTABLISHED | wc -l
查询服务器登录的ip的个数
七.通配符
例子:
单引号与双引号:
[[email protected] ~]# name=abc
[[email protected] ~]# echo '$name'
$name
[[email protected] ~]# echo "$name"
abc
[[email protected] ~]# echo '$name'
$name
[[email protected] ~]# echo "$name"
abc
反引号与$()
[[email protected] ~]# name=$(ls)
[[email protected] ~]# echo $name
a anaconda-ks.cfg error.log hello.sh install.log install.log.syslog true.log
转义符
[[email protected] ~]# echo \$name
$name