初识Shell

来源:互联网 发布:社交网络关系图 编辑:程序博客网 时间:2024/05/20 14:17

 

初识shell:


GNU bash
========================================================

Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行。
实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核。不仅如此,Shell有自己的编程语言用
于对命令的编辑,它允许用户编写由shell命令组成的程序。Shell编程语言具有普通编程语言的很多特点,比如它也有
循环结构分支控制结构等,用这种编程语言编写的Shell程序与其他应用程序具有同样的效果。

我们可以使用SHELL实现对Linux系统的大部分管理例如:
1. 文件管理
2. 用户管理
3. 权限管理
4. 磁盘管理
5. 软件管理
6. 网络管理
......

内容提要:
bash shell提示符
shell
语法
bash 特性
Linux获得帮助


一、bash shell提示符:
===================
[root@tianyun ~]# echo $PS1
[\u@\h \W]\$

[root@tianyun ~]# date
2012年 10月 24日 星期三 09:38:54 CST

[root@tianyun ~]# whoami
root

[root@tianyun ~]# useradd jack
[root@tianyun ~]# passwd jack
Changing password for user jack.
New UNIX password:
BAD PASSWORD: it is WAY too short
Retype new UNIX password:
passwd: all authentication tokens updated successfully.


二、shell 语法
=====================
命令 选项 参数
[root@tianyun ~]# ls
[root@tianyun ~]# ls -a
[root@tianyun ~]# ls -a /home

命令:整条shell命令的主体
选项:会影响会微调命令的行为 //通常以 -, --
参数:命令作用的对象


三、bash基本特性
1. 自动补全<tab> //Tab只能补全命令和文件RHEL6
# ls /etc/sysconfig/network-scripts/
# ls /etc/sysconfig/network-scripts/ifcfg-eth0
# cat /etc/sysconfig/network-scripts/ifcfg-eth0
# service network restart
# service sshd restart
# date -s 12:30

2. 快捷键
^C 终止前台运行的程序 //ping 172.16.8.100
^D 退出 等价exit
^L 清屏
^A 光标移到命令行的最前端 //编辑命令
^E 光标移到命令行的后端 //编辑命令
^U 删除光标前所有字符 //编辑命令
^K 删除光标后所有字符 //编辑命令
^R 搜索历史命令,利用关键词
Alt+. 引用上一个命令的最后一个参数,等价于!$
ESC .引用上一个命令的最后一个参数,等价于!$
# ls /etc/sysconfig/network-scripts/ifcfg-eth0
# cat ESC .

3. 历史命令
# history
a. 光标上下键
b. ^R //搜索历史命令(输入一段某条命令的关键字:必须是连续的)
c. !220 //执行历史命令中第220条命令
!字符串 //搜索历史命令中最近一个以xxxx字符开头的命令,例如!ser
!$ //引用上一个命令的最后一个参数
示例1
[root@instructor ~]# ls /root /home
[root@instructor ~]# cd !$
cd /home

示例2
[root@instructor ~]# ls /root /home
[root@instructor ~]# touch !$/file1
touch /home/file1

示例3
[root@instructor ~]# service nfs restart
[root@instructor ~]# ls
[root@instructor ~]# date
[root@instructor ~]# !se

4. 命令别名
# alias //查看系统当前的别名
ll='ls -l --color=tty'
# alias tianyun='cat /etc/sysconfig/network-scripts/ifcfg-eth0' //建立别名(临时的,仅在当前Shell生效)
# unalias tianyun //取消tianyun这个别名

[root@tianyun ~]# type -a ls
ls is aliased to `ls --color=auto'
ls is /bin/ls

[root@tianyun ~]# /bin/ls
[root@tianyun ~]# ls //别名优先
[root@tianyun ~]# \ls //跳过别名

[root@tianyun ~]# cp -rf /etc /tmp
[root@tianyun ~]# cp -rf /etc /tmp
[root@tianyun ~]# \cp -rf /etc /tmp

永久别名:
[root@tianyun ~]# gedit /etc/bashrc //添加如下行
alias tianyun='cat /etc/sysconfig/network-scripts/ifcfg-eth0'


四、Linux获得帮助
1. 命令 --help
# ls --help
用法:ls [选项]... [文件]...

ls 常见选项
-a all,查看目录下的所有文件,包括隐藏文件
-l 长列表显示
-h human 以人性化方式显示出来
-d 只列出目录名,不列出其他内容
-t 按修改时间排序
-S 按文件的Size排序
-r 逆序排列reverse
-i 显示文件的inode号(索引号)

# date --help
Usage: date [OPTION]... [+FORMAT]
or: date [-u|--utc|--universal][MMDDhhmm[[CC]YY][.ss]]

# date
# date +%H
# date +%F
# date 0214080014
# date 0214080014.00

2. man 手册名针对命令帮助,针对配置文件帮助,针对函数帮助)


[root@tianyun ~]# man man
MANUAL SECTIONS
The standard sections of the manual include:

1 User Commands

2 System Calls

3 C Library Functions

4 Devices and Special Files

5 File Formats and Conventions

6 Games et. Al.

7 Miscellanea

8 System Administration tools and Deamons


命令帮助: 章节1,章节8
函数帮助: 章节2,章节3
文件格式: 章节5

一般情况是不需要使用章节号,例如:
# man ls
# man useradd
# man setfacl (/EXAMPLES)

技巧1:按章节查询
/usr/bin/passwd 修改用户口令命令
/etc/passwd 包含用户信息的配置文件

# man -f passwd 列出所有章节中的passwd手册
# man 1 passwd passwd命令的帮助
# man 5 passwd 用户配置文件的帮助

技巧2:在所有章节中查询
# man -a passwd

3. 官方手册
http://docs.redhat.com Enterprise Linux (5/6/7)
http://www.mysql.com docs

4. baidu, google





 

 

 

 

 

 

 

原创粉丝点击