初学shell

一、shell简介

shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。

shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。

二、shell基本常识

  1. shell脚本名称命名一般为英文的大写、小写;
  2. 不能使用特殊的符号、空格来命名;
  3. shell脚本后缀以.sh结尾;
  4. shell脚本内容首行需以#/bin/bash开头;
  5. shell脚本中变量名称尽量使用大写字母,字母之间不能使用“-”,可以使用“_;
  6. shell脚本变量名称不能以数字、特殊符号开头。
  7. 环境变量    export name//使变量name变为环境变量

   unset  name//取消环境变量的设定

三、shell脚本

  1. 在终端使用vim”命令进入文本编辑器,新建一个文件tssh.sh

  chmod +x ./test.sh  #是脚本具有执行权限

      ./test.sh          #执行脚本

  /bin/sh test.sh     #直接运行解释器

  1. 算数运算符  + 加法  - 减法  * 乘法  / 除法  % 取余  = 赋值  == 相等  != 不相等
  2. 流程控制

① if

      if condition

      then

            程序段

   f1

if else

       if condition

       Then

            程序段

       elif condition2

       Then

       程序段

else

程序段

f1

while

while:

do

程序段

done

四、文本处理工具

  1. awk      格式 awk  ‘pattern  +     {action}’   file

awk   ‘NR==3,NR==5{print}’      jfedu.txt

   awk   ‘NR==3,NR==5{print $0}’    jfedu.txt

打印文件jfedu中的第三行至第五行,NR表示打印行,$0表示文本所有域。

awk  length$0>80(print NR)       jfedu.txt   打印文件jfedu文件中长度大于80的行号

ifconfig eth0|grep  “bcast”| awk ’{print”ip_”$2}’      awk添加自定义符号。

   2.find         格式 find path  -option  [  -print]  [-exec  -ok  command]  {}\;

-name filename: 查找名为filename的文件。

-perm: 按执行权限来查找。

-nouser:查找无有效属主文件

-type b/d/c/p/l/f:查找块设备、目录、字符设备、管道、符号链接、普通文件。

-user username:按文件属主来查找

-user username:按组来查找

   3.sed       格式  sed  [ -Options]   [‘Commands’]   filename;

      ① x:指定行号

xy: 指定从xy的行号范围

r: 从另一个文件中读文件

w:从文本写入到一个文件

y:变换字符

p:打印匹配行

=:打印文件行号

4.grep      格式  grep  -[acinv]  ‘word’  filename

-a:以文本文件方式搜索。

-c:计算找到的符合行的次数。

-i: 忽略大小写。

-n: 顺便输出行号。

-!: 逻辑运算符。

五、小程序

   while循环1-100的和

猜你喜欢

转载自www.cnblogs.com/1999LL/p/11871365.html