【Linux】常用基本指令(二)

shell脚本学习

文件类型为.sh,可以用vi编辑器打开,在开头加入注释#!/bin/bash告诉系统该shell脚本使用bash解释

变量

变量名定义时要注意:

  • 变量名和等号之间不可以有空格,这可能和你熟悉的所有编程语言都不一样。
  • 变量命名只能使用英文字母、数字和下划线,首个字符不能以数字开头
  • 中间不能有空格,可以使用下划线_代替
  • 不能使用标点符号
  • 不能使用bash中的关键字(可以使用help查看保留关键字)

使用一个定义过的变量,只需要在变量名前加$符号即可,变量前需要一个空格,例如(【】内是一个完整的变量)

【 ${变量名}】

变量名外的花括号是可选的,加不加均可,花括号的目的在于帮助解释器识别变量的边界,例如

for skill in Ada Coffe Action Java; do
    echo "I am good at ${skill}Script"
done

如果不给skill变量加花括号,写成

echo "I am good at $skillScript"

解释器这回把$skillScript当成一个变量(值为空),代码执行的结果就和我们所期望的不一致了


下面是我写的一个脚本文件

[hadoop@master test]$ vi test01.sh  

#!bin/bash
a=10
b=20
c=10
echo "a=10,b=20,c=10"
echo "a -eq b等于为真" 
[ $a -eq $b ];echo $?
echo "a -eq c  " 
[ $a -eq $c ];echo $?
echo "a -ne b不等于为真" 
[ $a -ne $b ];echo $?
echo "a -gt c大于为真" 
[ $a -gt $c ];echo $?
echo "a -ge c大于等于为真" 
[ $a -ge $c ];echo $?

错误点总结:

  • echo和要打印输出的内容之间要有空格
  • []内为要执行的计算,[和]要与内部用空格分开
  • 常用的计算符号有等于-eq 不等于-ne 大于-gt 小于-lt 大于等于-ge 小于等于-le
  • $?表示输出[]内结果是否成立,成立输出0,否则不成立

执行结果:

[hadoop@master test]$ bash test01.sh
a=10,b=20,c=10
a -eq b等于为真
1
a -eq c  
0
a -ne b不等于为真
0
a -gt c大于为真
1
a -ge c大于等于为真
0

字符串判断

-z 字串     字串长度为null则为真        

[ -z $c ] ; echo $?


-n 字串     字串长度不为null则为真

文件测试

  • -e 文件名 如果文件存在则为真        
  • -r 文件名 如果文件存在且可读则为真
  • -w 文件名 如果文件存在且可写则为真
  • -x 文件名 如果文件存在且可执行则为真
  • -s 文件名 如果文件存在且至少有一个字符则为真
  • -d 文件名 如果文件存在且为目录则为真
  • -f 文件名 如果文件存在且为普通文件则为真
  • -c 文件名 如果文件存在且为字符型特殊文件则为真
  • -b 文件名 如果文件存在且为特殊文件则为真

测试

[hadoop@master test]$ ll
total 12
-rwxrwxr-x 1 hadoop hadoop  61 Jul 20 09:02 shellTest.sh
-rw-rw-r-- 1 hadoop hadoop 299 Jul 20 11:48 test01.sh
-rw-rw-r-- 1 hadoop hadoop  13 Jul 19 14:10 test.txt
[hadoop@master test]$ [ -x test01.sh ];echo $? 
1
[hadoop@master test]$ [ -x shellTest.sh ];echo $?            
0
[hadoop@master test]$ [ -c shellTest.sh ];echo $?  
1
[hadoop@master test]$ [ -f shellTest.sh ];echo $?  
0
[hadoop@master test]$ [ -b shellTest.sh ];echo $?  
1
[hadoop@master test]$ [ -d shellTest.sh ];echo $?  
1
[hadoop@master test]$ [ -s shellTest.sh ];echo $?  
0

流程控制

for循环

  • 语法一

for i in (xx)
do
循环体……
done

[hadoop@master test]$ vi test02.sh

#!bin/bash
#使用for循环打印1-10
for i in  1 2 3 4 5 6 7 8 9 10
do
echo $i
done 
"test02.sh" [New] 6L, 86C written    

                      
[hadoop@master test]$ bash test02.sh
1
2
3
4
5
6
7
8
9
10
  • 语法二(类似java里的for循环,条件用两个括号括起来)

for((条件判断))
do
循环体……
done

[hadoop@master test]$ vi test03.sh  

#!bin/bash
for((i=1;i<11;i++))
do
echo $i
done
"test03.sh" 5L, 47C written  

                              
[hadoop@master test]$ bash test03.sh
1
2
3
4
5
6
7
8
9
10

这里说一个注意到的关于权限的问题

没有x(执行)权限的sh脚本,可以用bash 文件名执行,但不可以用./ 文件名执行

但是赋予了x权限后,便可以用./ 文件名的方式执行了

再举一个for循环的应用

打印九九乘法表

[hadoop@master test]$ vi test04.sh  

#!bin/bash
#打印乘法表
for ((i=1;i<10;i++))
do
for ((j=1;j<=i;j++))
do
echo -ne "$i*$j=$((i*j))\t"
done
echo " "
done
"test04.sh" 11L, 124C written   

                           
[hadoop@master test]$ bash test04.sh
1*1=1    
2*1=2   2*2=4    
3*1=3   3*2=6   3*3=9    
4*1=4   4*2=8   4*3=12  4*4=16   
5*1=5   5*2=10  5*3=15  5*4=20  5*5=25   
6*1=6   6*2=12  6*3=18  6*4=24  6*5=30  6*6=36   
7*1=7   7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49   
8*1=8   8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64   
9*1=9   9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81  

其中循环语句

echo -ne "$i*$j=$((i*j))\t"

等同于

echo -n -e "$i*$j=$((i*j))\t"

这里的-ne不是计算中不等于的意思,而是echo的参数

-e 每次遇到\t时转义

-n 每次的输出不换行


while循环

  • 语法一

while [ 条件 ]
        do
        命令序列
        done

[hadoop@master test]$ vi test05.sh  

#!bin/bash
count=1
while [ $count -le 10 ]
do
echo $count
count=$((count+1))
done
echo $?  
"test05.sh" 8L, 93C written


[hadoop@master test]$ bash test05.sh
1
2
3
4
5
6
7
8
9
10
0
  • 语法二

while read  line
        do
        命令序列
        done


if循环

if condition
then
    command1 
    command2
    ...
    commandN 
fi

获取输入:

read -p "打印内容" i(i为保存你输入内容的变量)

一个比大小的例子:

#!/bin/bash
read -p "please input the first:" a
read -p "please input the second:" b
if [ $a -eq $b ]
then
        echo  "="
elif [ $a -lt $b ]
then
        echo "<"
elif [ $a -gt $b ]
then
        echo ">"
fi
"test06.sh" [New] 14L, 211C written 


[hadoop@master test]$ bash test06.sh
please input the first:4
please input the second:5
<

case控制语句

case 对象 in

引号表示对象的取值,加反括号表示结束,*号表示其他,类似default

每一个取值结束后用两个连续的分号分割,类似于java里switch的break;

[hadoop@master test]$ vi casetest.sh

#!/bin/bash
read -p "请输入[y|n]:" i
case $i in
"y")
echo "yes!"
;;
"n")
echo "no!"
;;
*)
echo "y or n!"
esac
"casetest.sh" [New] 12L, 114C written 

           
[hadoop@master test]$ bash casetest.sh 
请输入[y|n]:y
yes!
[hadoop@master test]$ bash casetest.sh 
请输入[y|n]:n
no!
[hadoop@master test]$ bash casetest.sh 
请输入[y|n]:s
y or n!

定时任务

date 命令显示当前时间

date -s "2018-07-20 9:47:52" 用root权限修改临时时间,重启后会失效

#查看atd服务是否开启
[root@master test]# service atd status
atd (pid  1515) is running...

#停止服务
[root@master test]# service atd stop
Stopping atd:                                              [  OK  ]
[root@master test]# service atd status
atd is stopped
#启动服务
[root@master test]# service atd start
Starting atd:                                              [  OK  ]
[root@master test]# service atd status
atd (pid  2347) is running...

at [启动时间]

ctrl+d结束

[hadoop@master test]$ at "9:58"
at> touch at.txt
[hadoop@master test]$ at -l
3       2018-07-20 09:58 a hadoop

参数:

-l 查看用户计划任务

-d 删除用户计划任务

-c 查看at计划任务的具体内容


crontab 周期性执行计划任务

用法

crontab [-u 用户] [-l|-r|-e]

参数:    
-u:
指定某个用户,不加-u选项则为当前用户
-e:制定计划任务
-l:列出计划任务
-r :删除计划任务

注意:也要先确定crond服务是否启动

设置格式

分(0-59) 时(00-23) 日(1-31) 月(1-12) 周(0-7) 命令
可以使用横杠(-)来表示一段连续的时间
使用(,)表示若干不连续的时间
使用星号(*)表示所有的时间
使用除号(/)表示间隔时间

每周日凌晨12:00将passwd文件拷贝到datas中

0 0  * *  7 cp /etc/passwd /opt/datas

每一分钟往at.txt中插入一个正字

[hadoop@master test]$ crontab -e
no crontab for hadoop - using an empty one

*/1 * * * * echo "正" >>at.txt
[hadoop@master test]$ crontab -l
*/1 * * * * echo "正" >>at.txt

猜你喜欢

转载自blog.csdn.net/qq_36817460/article/details/81131673