shell 脚本练习

####脚本执行方法#####
1.sh script.sh | bash script.sh    ##没有执行权限时

2.path/script.sh | ./script.sh    ##绝对路径,当前目录下

3.source script.sh | . script.sh    ##这种方式会使用source或.号来读如指定shell文件,并会把其他shell中的变量值或函数返回给父shell继续使用


####定义变量####
[root@server ~]# a=hello
[root@server ~]# echo $a
hello
[root@server ~]# b='hello'
[root@server ~]# echo $b
hello
[root@server ~]# c="hello"
[root@server ~]# echo $c
hello
[root@server ~]# a=westos-$a
[root@server ~]# echo $a
westos-hello
[root@server ~]# b='westos-$a'
[root@server ~]# echo $b
westos-$a
[root@server ~]# c="westos-$a"
[root@server ~]# echo $c
westos-westos-hello
[root@server ~]# a=westos hello
bash: hello: command not found...
[root@server ~]# a="westos hello"
[root@server ~]# echo $a
westos hello


注意:建议没有特别要求时,字符串都加双引号,需要原样输出就加单引号


#####特殊变量####
$0:获取脚本文件名,如果执行时包含路径,则输出脚本路径
$n(>0):
$#:
$*:
$@:


$0:
[root@server mnt]# cat westos.sh
#!/bin/bash
echo $0
[root@server mnt]# sh westos.sh
westos.sh
[root@server mnt]# /mnt/westos.sh
/mnt/westos.sh


$n:
[root@server mnt]# cat westos.sh
#!/bin/bash
echo $1 $2

[root@server mnt]# sh westos.sh hello westos
hello westos
[root@server mnt]# sh westos.sh hello redhat
hello redhat

[root@server mnt]# echo \${1..10} > westos.sh
[root@server mnt]# cat westos.sh
$1 $2 $3 $4 $5 $6 $7 $8 $9 $10
[root@server mnt]# sh westos.sh  {1..10}
1 2 3 4 5 6 7 8 9 10
[root@server mnt]# sh westos.sh  {a..z}
a b c d e f g h i a0
[root@server mnt]# sh westos.sh  {a..z}
a b c d e f g h i j


$#:
[root@server mnt]# cat westos.sh ]
echo $1 $2 $3 $4 $5 $6 $7 $8 $9
echo $#
[root@server mnt]# sh westos.sh {1..100}
1 2 3 4 5 6 7 8 9
100

$?:
表示上条命令执行结果的返回值
0表示执行成功
非0表示执行失败


read用法:
[root@server mnt]# read str
westos hello
[root@server mnt]# echo $str
westos hello
[root@server mnt]# read -p "请输入一个整数:" i
请输入一个整数:10


将命令的结果赋值给变量:
[root@server mnt]# CMD=`ls -l`
[root@server mnt]# echo $CMD
total 8 -rwxr-xr-x. 1 root root 492 Dec 22 10:25 test.sh -rwxr-xr-x. 1 root root 40 Dec 22 10:40 westos.sh

[root@server mnt]# CMD=$(ls -l)
[root@server mnt]# echo $CMD
total 8 -rwxr-xr-x. 1 root root 492 Dec 22 10:25 test.sh -rwxr-xr-x. 1 root root 40 Dec 22 10:40 westos.sh


打包日志:
[root@server mnt]# tar zcf log_$(date +%F).tar.gz /var/log/
tar: Removing leading `/' from member names
[root@server mnt]# ls
log_2018-12-22.tar.gz  test.sh  westos.sh

#######变量的数值计算######
1)expr命令
[root@server mnt]# a=123
[root@server mnt]# expr $a + 10
133
[root@server mnt]# expr $a - 10
113
[root@server mnt]# expr $a * 10
expr: syntax error
[root@server mnt]# expr $a \* 10
1230
[root@server mnt]# expr $a / 10
12
[root@server mnt]# expr $a % 10
3

2)$[]和$(())表达式
[root@server mnt]# echo $[a+10]
20
[root@server mnt]# echo $[a-10]
0
[root@server mnt]# echo $[a*10]
100
[root@server mnt]# echo $[a/10]
1
[root@server mnt]# echo $[a%10]
0
[root@server mnt]# echo $((a+10))
20
[root@server mnt]# echo $((a-10))
0


3)let命令(let命令在执行后会保存新的值)
[root@server mnt]# let a+=10
[root@server mnt]# echo $a
20
[root@server mnt]# let a-=10
[root@server mnt]# echo $a
10
[root@server mnt]# let a*=10
[root@server mnt]# echo $a
100
[root@server mnt]# let a/=10
[root@server mnt]# echo $a
10
[root@server mnt]# let a%=10
[root@server mnt]# echo $a
0


4)小数运算工具bc
[root@server mnt]# echo "scale=4;1.23*4.56" | bc
5.6088
[root@server mnt]# echo "scale=2;1.23*4.56" | bc
5.60
[root@server mnt]# echo 1.2+3.4 | bc
4.6
[root@server mnt]# echo 1.23+4.56 | bc
5.79


##计算两个数的加减乘除

#!/bin/bash

read -t 5 -p "请输入两个整数:" a b

echo "a+b=$[a+b]"
echo "a-b=$[a-b]"
echo "a*b=$[a*b]"
echo "a/b=$[a/b]"
echo "a**b=$[a**b]"
echo "a%b=$[a%b]"

#######文本处理######
1)grep,egrep
grep
    -i    ##忽略字母大小写
    -v    ##条件取反
    -c    ##统计匹配行数
    -q    ##静默,无任何输出
    -n    ##显示匹配结果所在的行号


-q:
[root@server mnt]# grep '172.25.254.250' /etc/hosts && echo 'YES' || echo 'NO'
172.25.254.250 content.example.com
YES
[root@server mnt]# grep -q '172.25.254.250' /etc/hosts && echo 'YES' || echo 'NO'
YES

2)基本元字符:^ $
egrep -m10 '/sbin/nologin' /etc/passwd

[root@server mnt]# egrep -c '/sbin/nologin' /etc/passwd
35
[root@server mnt]# egrep -m10 'sbin$' wsp
root sbin
root sbin sbin
[root@server mnt]# cat wsp
root sbin
root sbin root
root sbin sbin


基本元字符:.    ##过滤非空行
[root@server mnt]# egrep '.' wsp
root sbin
root sbin root
root sbin sbin
root
awd
awd
awd


[root@server mnt]# egrep -v '.' wsp    ##过滤空行
[root@server mnt]# egrep  '^$' wsp    ##过滤空行


基本元字符: + ? *
[root@server ~]# egrep 'f+' 1.sh     ##输出包括f,ff,fff....,即至少出现一次
colorful,color
colorfulful?
stuf
stuff
stufff
stuffff
stufawd
we adw dfg awda
wea web wef

[root@server ~]# egrep 'color(ful)?' 1.sh     ##末尾的ful最多出现一次,也可以没有
color color color
colorful,color
color color.
colorfulful?

元字符:{}
[root@server ~]# egrep '(we){3}' 1.sh
rere wewewe
westos wewewewe Shell
[root@server ~]#
[root@server ~]#
[root@server ~]# egrep '(we){2,4}' 1.sh
xcvb wewe asdawd
rere wewewe
westos wewewewe Shell
[root@server ~]#
[root@server ~]# egrep '(we){3,}' 1.sh
rere wewewe
westos wewewewe Shell

[root@server ~]# egrep '(we)[ab]' 1.sh
weawe IPADDR
wea web wef
[root@server ~]# egrep '[A-Z]' 1.sh
weawe IPADDR
westos wewewewe Shell

 

3)cut命令
cut -d    ##指定分隔符
cut -d : -f 1-3 /etc/passwd    ##指定分隔符为:,显示第1到3列
cut -c 1,4 /etc/passwd        ##显示第一和第四个字符


练习:获取主机IP
[root@server ~]# ifconfig eth0 | grep "inet " | awk '{print $2}'
172.25.254.100

[root@server ~]# ifconfig eth0 | grep "inet " | cut -d " " -f 10
172.25.254.100

练习:检测网络
ping -c1 -w1 172.25.254.$1 &> /dev/null && echo 172.25.254.$1 is up || echo 172.25.254.$1 is down


4)sort命令:排序
sort
    -n    ##纯数字排序
    -r    ##倒序
    -u    ##去掉重复数字
    -o    ##输出到指定文件中
    -t    ##指定分隔符
    -k    ##指定要排序的列

[root@server ~]# sort westos

1
12
123
2
3
32
5
51
6
7

[root@server ~]# sort -n westos

1
2
3
5
6
7
12
32
51
123

[root@server ~]# sort -u westos
1
12
123
2
3
32
5
51
6
7

[root@server ~]# sort -t : -k 2 westos
2:0
12:10
2:12
3:2
51:20
5:21
123:22
32:31
5:4
6:4
1:5
51:55
123:66
7:79


[root@server ~]# sort -nt : -k 2 westos
2:0
3:2
5:4
6:4
1:5
12:10
2:12
51:20
5:21
123:22
32:31
51:55
123:66
7:79


[root@server ~]# sort -nt : -k 2 westos -o /mnt/file


5)uniq命令:对重复字符处理
uniq
    -u    ##显示唯一的行
    -d    ##显示重复的行
    -c    ##每行显示一次并统计重复次数
[root@server ~]# sort -n westos | uniq -c
      1 0
      1 1
      2 2
      1 4
      1 6
      1 9
      2 10
      1 20
      1 22
      2 31
      1 55


[root@server ~]# sort -n westos | uniq -d
2
10
31


[root@server ~]# sort -n westos | uniq -u
0
1
4
6
9
20
22
55

######test命令######

test "$a" == "$b" 等同于 [ "$a" == "$b" ]

[ "$a" = "$b" ]        ##等于
[ "$a" != "$b" ]    ##不等于
[ "$a" -eq "$b" ]    ##等于
[ "$a" -ne "$b" ]    ##不等于
[ "$a" -le "$b" ]    ##小于等于
[ "$a" -ge "$b" ]    ##大于等于
[ "$a" -gt "$b" ]    ##大于
[ "$a" -lt "$b" ]    ##小于
[ "$a" -ne "$b" -a "$a" -gt "$b" ]    ##-a必须条件都满足
[ "$a" -ne "$b" -o"$a" -gt "$b" ]    ##-a条件至少满足一个
[ -z "$a" ]        ##是否为空
[ -e "file" ]        ##是否存在
[ -f "file" ]        ##普通文件
[ -b "file" ]        ##块设备
[ -S "file" ]        ##套接字
[ -c "file" ]        ##字符设备
[ -L "file" ]        ##软链接


#!/bin/bash
[ "$1" "/etc/passwd" ] && echo YES || echo NO


##练习
判断输入的数字是否在10以内
1.输入是否为空
2.是否在10以内
3.1<$a<10 --> yes
4.$a<1 $a>10 --> no


#!/bin/bash

[ -z "$1" ] && {
    echo "please input a number!"
    exit 1
}

[ "$1" -gt "0" -a "$1" -lt "10" ] && {
    echo "YES"
}||{
    echo "NO"
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值