Linux学习笔记10

 2023-09-07 阅读 13 评论 0

摘要:Linux学习笔记10 Linux学习笔记10正则表达式源码包约定目录Shell脚本约定目录Shell脚本的创建与执行date命令同步时间Shell脚本预设变量与用户交互数学计算Shell脚本中的选择语句Shell脚本中的循环语句seq命令 正则表达式 http://www.apelearn.com/study_v2/chapter14.html#id

Linux学习笔记10

  • Linux学习笔记10
      • 正则表达式
      • 源码包约定目录
      • Shell脚本约定目录
      • Shell脚本的创建与执行
      • date命令
      • 同步时间
      • Shell脚本预设变量
      • 与用户交互
      • 数学计算
      • Shell脚本中的选择语句
      • Shell脚本中的循环语句
      • seq命令

正则表达式

http://www.apelearn.com/study_v2/chapter14.html#id1

例:遍历查找某一文件中的一段字符

[root@localhost ~]# grep -r 'passwd' /etc/

例:交换一行中两个字符串的位置

[root@localhost ~]# echo "123" | sed 's/\(1\)\(2\)\(3\)/\3\2\1/'
321
[root@localhost ~]# echo "123" | sed 's#\(1\)\(2\)\(3\)#\3\2\1#'
321
[root@localhost ~]#



源码包约定目录

/usr/local/src

Shell脚本约定目录

/usr/local/sbin

Shell脚本的创建与执行

1.使用VIM编写Shell脚本,后缀名约定为.sh

例:编写一个脚本

[root@localhost sbin]# vim 1.sh

脚本文件内容
#! /bin/bash
## This script is the first.
## written by switch.
## 2016-04-04
d=`date +%y:%m:%d`
echo "Hello, this is a shell script."
echo "It's written by switch on $d"

2.执行Shell脚本,方法有:

2.1.sh 脚本文件路径

[root@localhost sbin]# sh /usr/local/sbin/1.sh
Hello, this is a shell script.
It's written by switch on 16:04:04
[root@localhost sbin]#

2.2.给脚本文件加上执行权限,通过脚本路径名访问

[root@localhost sbin]# chmod +x 1.sh
[root@localhost sbin]# ll
总用量 4
-rwxr-xr-x 1 root root 174 4月 4 13:40 1.sh
[root@localhost sbin]# ./1.sh
Hello, this is a shell script.
It's written by switch on 16:04:04
[root@localhost sbin]#

2.3.bash 脚本文件路径,如果需要显示执行过程需要加上-x选项

[root@localhost sbin]# bash -x 1.sh
++ date +%y:%m:%d
+ d=16:04:04
+ echo 'Hello, this is a shell script.'
Hello, this is a shell script.
+ echo 'It'\''s written by switch on 16:04:04'
It's written by switch on 16:04:04
[root@localhost sbin]#

date命令

常用用法:

data +%Y 以四位数字格式打印年份

date +%y 以两位数字格式打印年份

date +%m 月份

date +%d 日期

date +%H 小时

date +%M 分钟

date +%S

date +%w 星期,如果结果显示0 则表示周日

例:

[root@localhost sbin]# date +'%Y-%m-%d %H:%M:%S'
2016-04-04 13:45:50
[root@localhost sbin]#

有时在脚本中会用到一天前的日期:

[root@localhost sbin]# date -d "-1 day" +%d
03

一小时前:

[root@localhost sbin]# date -d "-1 hour" +%H
12

一分钟前:

[root@localhost sbin]# date -d "-1 min" +%M
48

三星期前和三月前:

[root@localhost sbin]# date -d "-3week" +"%Y:%m:%d %w"
2016:03:14 1
[root@localhost sbin]# date -d "-3month" +"%Y:%m:%d %w"
2016:01:04 1
[root@localhost sbin]#

同步时间

ntpdate time.windows.com

Shell脚本预设变量

linux中shell变量$#,$@,$0,$1,$2的含义解释

与用户交互

read -p

例:输入字符串,并打印出来。

[root@localhost sbin]# bash -x 2.sh
+ read -p 'Please input a number: \>' num
Please input a number: \>F1
+ echo 'Your number is F1'
Your number is F1
[root@localhost sbin]#

数学计算

$[数学表达式]

例:简单计算

#! /bin/bash
a=1
b=2
sum=$[$a+$b]
echo "$a+$b=$sum"

执行结果:

[root@localhost sbin]# bash -x 3.sh
+ a=1
+ b=2
+ sum=3
+ echo 1+2=3
1+2=3
[root@localhost sbin]#

Shell脚本中的选择语句

if

基本语法:

if 判断语句;
then command
fi

例:

[root@localhost sbin]# cat if.sh
#! /bin/bash
read -p "Please input your score:" score
if((score<60))
then echo "Your didn't pass the exam."
fi
[root@localhost sbin]# bash -x if.sh
+ read -p 'Please input your score:' score
Please input your score:55
+ (( score<60 ))
+ echo 'Your didn'\''t pass the exam.'
Your didn't pass the exam.
[root@localhost sbin]#

if else

基本语法:

if 判断语句;
then command
else
command
fi

例:

[root@localhost sbin]# cat if_else.sh
#! /bin/bash
read -p "Please input your score:" score
if [ $score -ge 60 ]
then echo "Your pass the exam."
else echo "Your didn't pass the exam."
fi
[root@localhost sbin]# bash -x if_else.sh
+ read -p 'Please input your score:' score
Please input your score:30
+ '[' 30 -ge 60 ']'
+ echo 'Your didn'\''t pass the exam.'
Your didn't pass the exam.
[root@localhost sbin]# bash -x if_else.sh
+ read -p 'Please input your score:' score
Please input your score:70
+ '[' 70 -ge 60 ']'
+ echo 'Your pass the exam.'
Your pass the exam.
[root@localhost sbin]#

if elif else

基本语法:

if 判断语句1;
then command
elif 判断语句2;
then command
else
command
fi

例:

[root@localhost sbin]# cat if_elif_else.sh
#! /bin/bash
read -p "Please input your score:" score
if [ $score -lt 60 ]
then echo "Your didn't pass the exam."
elif [ $score -ge 60 ] && [ $score -lt 85 ]
then echo "Your pass the exam."
else
echo "Your score is vary high!"
fi
[root@localhost sbin]# bash -x if_elif_else.sh
+ read -p 'Please input your score:' score
Please input your score:50
+ '[' 50 -lt 60 ']'
+ echo 'Your didn'\''t pass the exam.'
Your didn't pass the exam.
[root@localhost sbin]# bash -x if_elif_else.sh
+ read -p 'Please input your score:' score
Please input your score:80
+ '[' 80 -lt 60 ']'
+ '[' 80 -ge 60 ']'
+ '[' 80 -lt 85 ']'
+ echo 'Your pass the exam.'
Your pass the exam.
[root@localhost sbin]# bash -x if_elif_else.sh
+ read -p 'Please input your score:' score
Please input your score:90
+ '[' 90 -lt 60 ']'
+ '[' 90 -ge 60 ']'
+ '[' 90 -lt 85 ']'
+ echo 'Your score is vary high!'
Your score is vary high!
[root@localhost sbin]#

以上只是简单的介绍了if语句的结构。在判断数值大小除了可以用 (( )) 的形式外,还可以使用 [ ] 但是就不能使用>, < , = 这样的符号了,要使用 -lt (小于),-gt (大于),-le (小于等于),-ge (大于等于),-eq (等于),-ne (不等于)。

[root@localhost sbin]# a=10; if [ $a -lt 5 ]; then echo ok; fi
[root@localhost sbin]# a=10; if [ $a -gt 5 ]; then echo ok; fi
ok
[root@localhost sbin]# a=10; if [ $a -ge 10 ]; then echo ok; fi
ok
[root@localhost sbin]# a=10; if [ $a -eq 10 ]; then echo ok; fi
ok
[root@localhost sbin]# a=10; if [ $a -ne 10 ]; then echo ok; fi

再看看if中使用 && 和 ||的情况:

[root@localhost sbin]# a=10; if [ $a -lt 1 ] || [ $a -gt 5 ]; then echo ok; fi
ok
[root@localhost sbin]# a=10; if [ $a -gt 1 ] || [ $a -lt 10 ]; then echo ok; fi
ok

shell 脚本中if还经常判断关于文件属性,比如判断是普通文件还是目录,判断文件是否有读写执行权限等。常用的几个选项:

-e :判断文件或目录是否存在

-d :判断是不是目录,并是否存在

-f :判断是否是普通文件,并存在

-r :判断文档是否有读权限

-w :判断是否有写权限

-x :判断是否可执行

-s :判断文件存在且文件大小是否大于0

例:判断是否为文件

[root@localhost sbin]# if [ -e 1.sh ]; then echo 'file'; fi
file
[root@localhost sbin]#

PS:可以通过man test来查看关于判断的帮助文档。

还有一些特殊用法:

-z :判断字符串长是否为0

if后面可以是一个返回结果,如果结果为0,则执行,不为零则不执行。

[root@localhost sbin]# if grep -q 'root' ~/passwd; then echo 'ok'; fi
ok
[root@localhost sbin]#

PS:这里的grep -q获取的是执行是否成功的值。

! 返回结果

PS:就是对上面那种用法的结果取反。

case

基本语法:

case 变量 in
value1)
command
;;
value2)
command
;;
*)
command
;;
esac

PS:value的个数是没有限制的,*代表其他值。

例:判断奇数偶数

[root@localhost sbin]# cat case.sh
#! /bin/bash
read -p "Please input a number:" num
res=$[$num%2]
case $res in
1)
echo "The number is odd."
;;
0)
echo "The number is even."
;;
*)
echo "It's not a number!"
;;
esac
[root@localhost sbin]# bash -x case.sh
+ read -p 'Please input a number:' num
Please input a number:10
+ res=0
+ case $res in
+ echo 'The number is even.'
The number is even.
[root@localhost sbin]# sh case.sh
Please input a number:9
The number is odd.
[root@localhost sbin]#

Shell脚本中的循环语句

for循环

基本语法:

for 变量名 in 循环的条件
do
command
done

PS: “循环的条件” 可以写成一组字符串或者数字(用1个或者多个空格隔开), 也可以是一条命令的执行结果。

例:输出既定的字符串

[root@localhost sbin]# cat for.sh
#! /bin/bash
for i in a b c d e
do
echo $i
done
[root@localhost sbin]# sh for.sh
a
b
c
d
e
[root@localhost sbin]#

例:输出当前文件夹下的所有非隐藏文件的文件名

[root@localhost sbin]# cat for_2.sh
#! /bin/bash
for file in `ls`
do
echo $file
done
[root@localhost sbin]# sh for_2.sh
1.sh
2.sh
3.sh
4.sh
case.sh
for_2.sh
for.sh
if_elif_else.sh
if_else.sh
if.sh
[root@localhost sbin]#

例:1+2+3+…+99+100

[root@localhost sbin]# cat 4.sh
#! /bin/bash
sum=0
for i in `seq 1 100`
do
sum=$[$i+$sum]
done
echo $sum
[root@localhost sbin]# sh 4.sh
5050
[root@localhost sbin]#

PS:脚本中的 seq 1 100 表示从1到100的一个序列。

while循环

基本语法:

while 循环条件
do
command
done

例:1+2+3+…+99+100

[root@localhost sbin]# cat while.sh
#! /bin/bash
sum=0
i=1
while [ $i -le 100 ]
do
sum=$[$sum+$i]
i=$[$i+1]
done
echo $sum
[root@localhost sbin]# sh while.sh
5050
[root@localhost sbin]#

PS:另外可以把循环条件拿一个:替代,这样可以做到死循环。

break : 结束本层循环

continue : 跳过本次循环,进入下一次循环

exit : 直接退出当前Shell脚本

seq命令

seq

基本语法:

seq [-s '分隔符'] 起始数 [增长数] 结束数

例:自增为5,分隔符为’:’,输出1..100的增长序列

[root@localhost sbin]# seq -s ':' 1 +5 100
1:6:11:16:21:26:31:36:41:46:51:56:61:66:71:76:81:86:91:96
[root@localhost sbin]#

Shell脚本中的函数

[root@localhost sbin]# cat fun.sh
#! /bin/bash
function max()
{
if [ $1 -ge $2 ]
then echo $1
else
echo $2
fi
}
max $1 $2
[root@localhost sbin]# bash -x fun.sh 2 3
+ max 2 3
+ '[' 2 -ge 3 ']'
+ echo 3
3
[root@localhost sbin]#

PS:在shell脚本中,函数一定要写在最前面,不能出现在中间或者最后,因为函数是要被调用的,如果还没有出现就被调用,肯定是会出错的。

PS:通过expert 变量可以将变量定义为全局变量,也就是说,在脚本文件的该函数外也能使用该变量。

参考《跟阿铭学Linux》


版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/14997.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息