shell学习之shell基础知识了解

简介:

一、了解bash shell

/etc/bash是Linux系统预设的shell.bash是GNU计划中重要的工具软件之一,目前也是Linux distributions的标准
shell。

Shell:(壳):交互式环境中我们所用到的一个界面,很多时候也被称作是编译器
shell可分为两大类(基于linux的系统):
Text base:基于文字的
                        sh csh ksh bash tcsh zsh
Graphic base:基于图形界面的
                         KDE、GNOME、Xface

bash工作模式:
    互动模式(interactive mode),需要有输入设备传递shell
    shell scripts模式(non-interactive mode)把执行命令写入到文件中,交由bash去读取和执行


1、查看当前系统上所支持的shell类型

1
2
3
4
5
6
7
[root@lovelace ~] # cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
/bin/ksh

2、检查shell的版本信息

1
2
[root@lovelace ~] # echo $BASH_VERSION
3.2.25(1)-release

 二、bash shell的功能
bash shell具备的功能:
1、兼容于bourne shell(sh)
2、支持许多选项及变量,可自定shell的使用环境。
3、支持历史命令功能
4、具有命令修订的能力,可重新取用或修改之前执行过的命令
5、具有工作控制的能力,可控制前台及后台的程序
6、具有程序设计的能力,支持变量,真假值判断,控制流程,函数,输入出控制,可让你设计程序已解决问题,进行系统的维护管理。
7、具有:短小精悍,可移植性强。

三、shell的种类以及bash的配置文件

站在用户登录的角度来说,shell的种类
登录式shell:
    正常通过某终端登录
    su - username
    su -l username
非登录式shell:
    su username
    图形终端下打开命令窗口
    自动执行的shell脚本


bash配置文件
全局配置:

/etc/profile   

/etc/profile.d/*.sh 

/ets/bashrc
个人配置:

~/.bash_profile

~/.bashrc

profile类功能:
    设定环境变量
    运行命令或脚本

bashrc类功能:
    设定本地变量
    定义命令别名

登录式shell如何读取配置文件
/etc/profile----->/etc/profile.d/*.sh------->~/.bash_profile--->~/.bashrc--->/etc/bashrc

非登录式shell如何读取配置文件
~/.bashrc--->/etc/bashrc---->/etc/profile.d/*.sh 


四、优秀的shell脚本应该遵循的原则

应该包含以下基本信息
1、scripts的功能
2、scripts的版本信息
3、scripts的作者与联络方式
4、scripts的版权宣告方式
5、scripts的历史记录
6、scripts内的特殊指令,使用绝对路径来调用
7、scripts运作时需要的环境变量预先宣告预设定

8、scripts应该包含详细的注释信息,以便于查阅


后记:学习shell,个人感觉有以下几点需要注意:

1、基本功,这个需要我们长时间的积累,碰到问题,才能达到那种顺手拈来的境界。

2、进阶,你需要熟练掌握shell各种语法结构和定义,以及shell语句在什么环境下使用。

3、多看别人的优秀的脚本,(这里的优秀没有绝对的界限,哪怕整个脚本只有一个语句对你有帮助的那就是优秀的).

4、多总结,把收集到的优秀的脚本和语句吸收、整理,尽可能使其成为自己的知识体系中的一部分.

5、优化,可能一个月前你写的脚本很冗余,都是用一大堆命令堆叠起来的,而一个月后你可能使用函数重新编译先前你写的脚本,或许就可以精简到几行,

所以写过的脚本,不要认为可以完成工作就可以了(尽可能的去简化,如果你只是简单的使用,那就可以了),如果你想要写出更完美的脚本,那就尽可能去优化。

 

附上一列(检查当前主机所在网络中的主机存活情况):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash
#Verson:0.1
#Auther:lovelace
#Pragram:This pragram is auto read network address,after that check hosts active or not in your current network,
#BUG: There are much more delay when ping unactive hosts,who can offer good ideas.
#define netwok address
netIP=` ifconfig  | grep  'inet addr:'  grep  Bcast| awk  '{print $2}'  | awk  -F:  '{print $2}'  cut  -d  '.'  -f1-3`
#loop network address and check host active or not
for  in  ` seq  1 255`
do
#Note:ping -c--->count  -w--->deadtime  This two argument can reduce the delay time
ping  -c 2  -w 1 $netIP\.$x &> /dev/null
#  sleep 2
if  [ $? - eq  0 ]; then
#print active host and input file to ip.txt files,
echo  -e  "\033[31m$netIP.$x\033[0m is active" tee  /tmp/ip .txt
fi
done
#statistics the active hosts
runnum=$( cat  /tmp/ip .txt |  wc  -l)
#print the host active sum
echo  -e  "There are \033[33m$runnum hosts\033[0m  live in \033[35m$netIP.0\033[0m network."

ping


本文转自lovelace521 51CTO博客,原文链接:http://blog.51cto.com/lovelace/1211087,如需转载请自行联系原作者

相关文章
|
8月前
|
关系型数据库 MySQL Shell
shell学习(十七) 【mysql脚本备份】
shell学习(十七) 【mysql脚本备份】
65 0
|
3月前
|
监控 Unix Shell
shell脚本编程学习
【10月更文挑战第1天】shell脚本编程
96 12
|
6月前
|
Shell Linux
Shell 脚本编程学习
Shell 脚本编程学习
46 0
|
7月前
|
人工智能 运维 中间件
Linux-shell简单学习
Linux-shell简单学习
42 0
|
8月前
|
存储 Shell 数据安全/隐私保护
ZooKeeper【基础知识 04】控制权限ACL(原生的 Shell 命令)
【4月更文挑战第11天】ZooKeeper【基础知识 04】控制权限ACL(原生的 Shell 命令)
159 7
|
8月前
|
运维 Shell Python
Shell和Python学习教程总结
Shell和Python学习教程总结
|
8月前
|
分布式计算 Hadoop Shell
Hadoop【基础知识 04】【HDFS常用shell命令】(hadoop fs + hadoop dfs + hdfs dfs 使用举例)
【4月更文挑战第4天】Hadoop【基础知识 04】【HDFS常用shell命令】(hadoop fs + hadoop dfs + hdfs dfs 使用举例)
157 5
|
8月前
|
机器学习/深度学习 Shell Linux
shell 学习记录
shell 学习记录
42 0
|
8月前
|
Shell
shell学习(六) 【case多条件分支语句】
shell学习(六) 【case多条件分支语句】
132 1
|
8月前
|
Shell 应用服务中间件 nginx
shell学习(七) 【shell 函数】
shell学习(七) 【shell 函数】
50 1