shell-5.bash shell 编程的基本概念

bash shell 编程的基本概念

  • bash shell 的程序结构
  • 内部命令和外部命令
  • bash shell编程所需常用命令及参数
  • 在命令同时执行多个命令

1.bash shell 的程序结构

  • 解释器说明及位置
  • 内部命令、外部命令
  • 函数及过程定义和使用
  • 返回值设置
  • 容错判断
    例子:
[root@master xiaosi]# cat foo1.sh
#!/bin/sh  
xiaosi = 36  #变量
echo "xiaosi  = 36 "  #输出
ifconfig  #bash命令
foo(){     #函数
  echo foo fun 
} 
foo   #调用函数

echo 12   #输出
exit 10   #以输出10作为退出及结果

结果:


[root@master xiaosi]# bash foo1.sh 
foo1.sh: line 2: xiaosi: command not found
xiaosi  = 36 
eth2      Link encap:Ethernet  HWaddr 00:0C:29:4F:62:6E  
          inet addr:192.168.71.100  Bcast:192.168.71.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe4f:626e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:20272 errors:0 dropped:0 overruns:0 frame:0
          TX packets:11913 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2188536 (2.0 MiB)  TX bytes:2033971 (1.9 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:5187 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5187 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:462238 (451.4 KiB)  TX bytes:462238 (451.4 KiB)

foo fun
12

执行Bash Script的方法

  • 设置权限运行(chmod + x filename 上上节有)。
  • 使用解释器解释运行。
  • 使用source或“ . ” 符号运行。

2. 内部命令和外部命令

  • 内部命令(内嵌,执行速度快)
    —bash 自带的功能
  • 外部命令
    — 独立存在于文件系统上的可执行程序
  • 使用type命令查看命令类型
[root@master xiaosi]# type ls
ls is aliased to `ls --color=auto' 
[root@master xiaosi]# type ifconfig
ifconfig is /sbin/ifconfig
[root@master xiaosi]# type cd 
cd is a shell builtin  #内嵌命令
[root@master xiaosi]# type if
if is a shell keyword   #关键字

常见的内部命令说明

查询帮助文档(内部用help,外部用man)

  • help
  • echo
  • printf
  • cd
  • pwd
  • : (代表空)
  • alias(别名)
  • exit
  • history(敲过的命令)
  • time (用来统计一个程序的执行时间)
  • read
  • exec (执行程序)

举几个陌生的例子:

[root@master xiaosi]# alias cdhadoop='cd /usr/local/apps/hadoop-2.4.1/'
[root@master xiaosi]# alias
alias cdhadoop='cd /usr/local/apps/hadoop-2.4.1/'
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@master xiaosi]# alias cdhadoop
alias cdhadoop='cd /usr/local/apps/hadoop-2.4.1/'
[root@master xiaosi]# cdhadoop
[root@master ~]# read A
jing
[root@master ~]# echo $A
jing
[root@master ~]# read A B C
a b c b
[root@master ~]# echo $C
c b  #依次接收变量,多出来的最后一个全接收
[root@master ~]# while : ;do echo A; sleep 1;done
A
A
A
A
A
A
^C
[root@master ~]# 

常用的外部命令说明:

  • which
  • tail(从尾部开始查看,默认倒数十行)
  • find
  • cut(裁剪命令)
  • locate
  • tac
  • tar
  • tr
  • date
  • wc
  • basename
  • grep
  • cat
  • touch
  • dirname
  • tee
  • more
  • mkdir
  • sort
  • xargs
  • head(从顶部开始查看,默认倒数十行)
  • rm
  • uniq’
    例子:
    which(哪里)
[root@master ~]# which ls
alias ls='ls --color=auto'
        /bin/ls

cut

[root@master ~]# echo a:b:c:d |cut -d: -f1  
               #-d: ----d是切分参数,表示以:作为切分,-f1表示第一列,f表示列参数
a
[root@master ~]# echo a:b:c:d |cut -d: -f2
b
[root@master ~]# echo a:b:c:d |cut -d: -f3
c
[root@master ~]# echo a:b:c:d |cut -d: -f4
d
[root@master ~]# echo a:b:c:d |cut -db -f1
a:

tr(替换)

[root@master ~]# echo abcdef [a-z]| tr 'a-z' 'A-Z'
ABCDEF [A-Z]

date

[root@master ~]# date +%F
2019-07-25
[root@master ~]# cp file file.$(date +%F)
[root@master ~]# ls -l file*
-rw-r--r--. 1 root root 77 724 17:19 file
-rw-r--r--. 1 root root 77 725 06:45 file.2019-07-25

wc(统计文件内容)

[root@master ~]# wc /etc/hosts
 3  6 66 /etc/hosts
[root@master ~]# wc -l  /etc/hosts   #统计行号
3 /etc/hosts
[root@master ~]# wc -w /etc/hosts     #统计单词
6 /etc/hosts
[root@master ~]# wc -c /etc/hosts     #统计字符
66 /etc/hosts

basename 与dirname

[root@master xiaosi]# vi foo.out
12838494
3243

534
3
4242

232425435
2311324
13252
4222546
13252
[root@master ~]# basename /etc/sysconfig/network-scripts/ifcfg-eth0 
ifcfg-eth0
[root@master ~]# dirname /etc/sysconfig/network-scripts/ifcfg-eth0          
/etc/sysconfig/network-scripts

sort(把文件排序来查看)

[root@master xiaosi]# sort foo.out


12838494
13252
13252
2311324
232425435
3
3243
4222546
4242
534
[root@master xiaosi]# sort -n foo.out


3
534
3243
4242
13252
13252
2311324
4222546
12838494
232425435
[root@master xiaosi]# sort -rn foo.out
232425435
12838494
4222546
2311324
13252
13252
4242
3243
534
3

uniq(唯一,即是数据合并,计数显示多少次)

[root@master xiaosi]# sort foo.out|uniq -c |sort -n 
      1 12838494
      1 2311324
      1 232425435
      1 3
      1 3243
      1 4222546
      1 4242
      1 534
      2 
      2 13252

在命令同时执行多个命令:

  • 使用“ ; ” 分号作为多个程序的顺序间隔
    • 命令1;命令二
    • 不管命令1执行的状态是否为真,马上执行命令2

[root@master xiaosi]# echo begin ;sort foo.out|uniq -c |sort -n; echo end
begin
1 12838494
1 2311324
1 232425435
1 3
1 3243
1 4222546
1 4242
1 534
2
2 13252
end

  • 使用“&&”(与关系,相当于and)和“||”(或关系,相当于or)符号作为多个程序的执行条件间隔
    • 命令1&&命令2
    • 命令1||命令2
[root@master xiaosi]# grep xiaosi /etc/passw && echo user have found   
grep: /etc/passw: 没有那个文件或目录
[root@master xiaosi]# grep xiaosi /etc/passwd && echo user have found
xiaosi:x:500:500:xiaosi:/home/xiaosi:/bin/bash
user have found
[root@master xiaosi]# grep xiaosi /etc/passwdd || echo user have found  
grep: /etc/passwdd: 没有那个文件或目录
user have found
[root@master xiaosi]# grep xiaosi /etc/passwd || echo user have found 
xiaosi:x:500:500:xiaosi:/home/xiaosi:/bin/bash

注意:举例子以下这条命令相当于:三目表达式(java,python,c之类的:表达式?表达式2:表达式3)

[root@master xiaosi]# grep xiaosi /etc/passwd && echo user have found || echo user hava not found             
xiaosi:x:500:500:xiaosi:/home/xiaosi:/bin/bash
user have found
[root@master xiaosi]# grep xiaosi /etc/passwddddd && echo user have found || echo user hava not found 
grep: /etc/passwddddd: 没有那个文件或目录
user hava not found
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值