经过本章学习咱们能够学会如下内容:
linux
一 什么是shell以及shell的好处
shell
二 shell脚本的格式以及变量的使用方法编程
三 test测试语句和if流程控制vim
四 case-for-while语句的使用方法bash
五 中双小括号的使用方法编程语言
六 循坏嵌套使用方法ide
一 什么是shell以及shell的工做方式:oop
shell简单来讲能够当作人与计算机之间的翻译官,他做为用户与linux系统内部的通讯媒介,除了可以支持各类变量与参数外,还能够提供循坏、分之等高级编程语言才有的控制结构特性。
学习
shell的工做方式有二种:测试
交互式:用户每输入一条命令就当即执行。
批处理:用户先编好一个完整的shell脚本,shell会一次执行脚本的多个命令。
二 shell脚本的格式以及变量的使用方法
建立shell脚本的步骤:
第一步:建立一个包含命令和控制结构的shell文件,通常是后缀.sh
第二部:修改文件的权限使它能够执行,通常使用chmod U+x 再加文件名
第三步:执行脚本
方法一: ./加脚本名称(经常使用这一种)
方法二:使用绝对路径
方法三:bash加文件名
举例说明:建立一个脚本,查询一下他的路径和包含的文件内容。
[root@baiyijie jiaoben]# vim example01.sh 建立脚本名称
[root@baiyijie jiaoben]# chmod +x example01.sh加上执行权限
[root@baiyijie jiaoben]# ./example01.sh 运行脚本
our first example
we are currently in the following directory.
/root/jiaoben
this directory contains the following files
example01.sh
[root@baiyijie jiaoben]# cat example01.sh查看脚本内容
#!/bin/bash
#this is to show what a example looks like.
echo "our first example"
echo # this inserts an empty line in output .
echo "we are currently in the following directory."
pwd
echo
echo "this directory contains the following files"
ls
shell变量的使用方法:
用户定义变量:由字母或者下划线开头,由字母或者下划线组成,大小写字母意义不一样,变量长度没有限制。
使用变量值时:要在变量名前加上前缀"$"
举例说明:2VAR就是非法变量
变量赋值:赋值号 = 二边没有空格
举例说明:
[root@localhost ~]# A=aaa变量赋值这种正确的
[root@localhost ~]# A = AAA 这种错误
-bash: A: command not found
[root@localhost ~]# echo $A 使用变量
aaa
[root@localhost ~]# A=`date` 将一个命令的执行结果赋予变量注意加``
[root@localhost ~]# echo $A
Thu Mar 22 10:33:58 CST 2018
[root@localhost ~]# B=$(ls -l) B的变量赋值就是一个ls -l的命令
[root@localhost ~]# echo $B
total 40 -rw-------. 1 root root 1409 Mar 14 22:00 anaconda-ks.cfg -rwxr-xr-x. 1 root root 75 Mar 16 15:26 backup.sh -rwxr-xr-x. 1 root root 115 Mar 19 16:00 baojing.sh -rwxr-xr-x. 1 root root 195 Mar 16 17:00 caipiao.sh drwxr-xr-x. 2 root root 24 Mar 22 10:11 jiaoben drwxr-xr-x. 4 root root 49 Mar 19 20:38 lianxi -rwxr-xr-x. 1 root root 184 Mar 20 15:46 mizhi.sh -rwxr-xr-x. 1 root root 113 Mar 20 15:05 pingfang.sh -rwxr-xr-x. 1 root root 235 Mar 20 21:26 sanjiao.sh -rwxr-xr-x. 1 root root 44 Mar 19 14:17 shifen.sh -rwxr-xr-x. 1 root root 92 Mar 19 15:53 wokao.sh -rwxr-xr-x. 1 root root 112 Mar 20 15:19 xingqitian.sh drwxr-xr-x. 2 root root 6 Mar 18 13:48 zuoye
[root@localhost ~]# A=$B A赋值变量B的内容
[root@localhost ~]# echo $A
total 40 -rw-------. 1 root root 1409 Mar 14 22:00 anaconda-ks.cfg -rwxr-xr-x. 1 root root 75 Mar 16 15:26 backup.sh -rwxr-xr-x. 1 root root 115 Mar 19 16:00 baojing.sh -rwxr-xr-x. 1 root root 195 Mar 16 17:00 caipiao.sh drwxr-xr-x. 2 root root 24 Mar 22 10:11 jiaoben drwxr-xr-x. 4 root root 49 Mar 19 20:38 lianxi -rwxr-xr-x. 1 root root 184 Mar 20 15:46 mizhi.sh -rwxr-xr-x. 1 root root 113 Mar 20 15:05 pingfang.sh -rwxr-xr-x. 1 root root 235 Mar 20 21:26 sanjiao.sh -rwxr-xr-x. 1 root root 44 Mar 19 14:17 shifen.sh -rwxr-xr-x. 1 root root 92 Mar 19 15:53 wokao.sh -rwxr-xr-x. 1 root root 112 Mar 20 15:19 xingqitian.sh drwxr-xr-x. 2 root root 6 Mar 18 13:48 zuoye
[root@localhost ~]# A=date 看这里不加``会有什么后果
[root@localhost ~]# echo $A
date 只输出date
这里要说一下单引号和双引号的区别:
单引号之间的内容原封不动的指定给了变量
双引号取消了空格的做用,特殊符号的含义保留。
列出全部的变量用set
位置变量和特殊变量
位置变量:shell 解释执行用户的命令时,将命令执行的第一个字做为命令名,而其余字做为参数,由出如今命令行上的位置肯定的参数称为位置参数。位置变量:使用$N来表示。
举例说明:
[root@localhost ~]# ./example.sh file1 file2 file3
$0 这个程序的文件名 example.sh
$n 这个程序的第n个参数值。n=1..n
特殊变量:
有些变量一开始执行脚本的时候就会设定,且不能被修改,但咱们不叫它只读的系统变量,而叫它特殊变量,这些变量当一执行程序就有了,如下是一些特殊变量:
$*这个程序的全部参数
$#这个程序参数的个数
$$这个程序的PID
$!执行上一个后台程序的PID
$?执行上一个指令的返回值
举例说明:写一个脚本表示出程序的全部参数进程ID和程序参数的个数。
[root@baiyijie jiaoben]# vim z.sh
[root@baiyijie jiaoben]# chmod +x z.sh
[root@baiyijie jiaoben]# ./z.sh
表示这个程序的全部参数
0 表示这个程序的参数的个数
3486 表示程序进程的ID
3488 执行上一个就太指令的PID
3486 表程序的进程ID
[root@baiyijie jiaoben]# ./z.sh aaa bbb ddd cccc加入一些参数
aaa bbb ddd cccc 表示这个程序的全部参数
4 表示这个程序的参数的个数
3489 表示程序进程的ID
3491 执行上一个就太指令的PID
3489 表程序的进程ID
[root@baiyijie jiaoben]# cat z.sh查看脚本
#!/bin/bash
echo "$* 表示这个程序的全部参数"
echo "$# 表示这个程序的参数的个数"
touch /tmp/a.txt
echo "$$ 表示程序进程的ID"
touch /tmp/b.txt &
echo "$! 执行上一个就太指令的PID"
echo "$$ 表程序的进程ID"
三 test测试语句和if流程控制
&& 表明条件性的AND
|| 表明条件性的OR
test也能够写成[]
数值测试:-gt 是否大于
-ge 是否大于等于
-eq 是否等于
-ne 是否不等于
-lt 是否小于
-le 是否小于等于
IF语法:
if条件
then
语句
fi
扩展; 分号,表示二个命令写在一行,互不影响。
if语法:
if 条件 ;then
命令1
else
命令2
fi
举例说明;写一个脚本,能够判断他是目录,或者是普通文件,或者是设备文件。
[root@localhost ~]# vim fuza.sh
[root@localhost ~]# chmod +x fuza.sh
[root@localhost ~]# ./fuza.sh 执行脚本
./fuza.sh: line 1: i#/bin/bash: No such file or directory
input a file name 让我输入一个文件名
/etc 输入/etc
/etc is a dir 系统判断他是一个目录
[root@localhost ~]# ./fuza.sh
./fuza.sh: line 1: i#/bin/bash: No such file or directory
input a file name
/etc/passwd
/etc/passwd is file
[root@localhost ~]# ./fuza.sh
./fuza.sh: line 1: i#/bin/bash: No such file or directory
input a file name
hhhk
hhhk is an unknow file
[root@localhost ~]# ./fuza.sh
./fuza.sh: line 1: i#/bin/bash: No such file or directory
input a file name
/dev/sda
/dev/sda is a device file 说它是一个设备文件
[root@localhost ~]# cat fuza.sh
i#/bin/bash
echo "input a file name"
read file_name
if [ -d $file_name ] ; then
echo "$file_name is a dir"
elif [ -f $file_name ] ;then
echo "$file_name is file"
elif [ -c $file_name -o -b $file_name ] ; then
echo "$file_name is a device file"
else
echo "$file_name is an unknow file"
fi
四 case-for-while语句的使用方法
case: 流程控制语句,适用于多分支。
格式:
case 变量 in
字符串1)命令列表1
;;
....
字符串n)命令列表n
;;
esac
举例说明:
制做一个菜单,能够选择bcd,分别表明backup copy delete
文字部分
[root@localhost lianxi]# vim case.sh
[root@localhost lianxi]# chmod +x case.sh
[root@localhost lianxi]# ./case.sh 执行脚本
****************************
Please selet you operation:
1 Copy
2 Delete
3 Backup
*****************************
C 我输入C
your seletion is copy 说我选择的是copy
[root@localhost lianxi]# ./case.sh
****************************
Please selet you operation:
1 Copy
2 Delete
3 Backup
*****************************
D
your seletion is delete
[root@localhost lianxi]# ./case.sh
****************************
Please selet you operation:
1 Copy
2 Delete
3 Backup
*****************************
B
your seletion is backup
[root@localhost lianxi]# cat case.sh
#!/bin/bash
echo "****************************"
echo "Please selet you operation:"
echo " 1 Copy"
echo " 2 Delete"
echo " 3 Backup"
echo "*****************************"
read op
case $op in
C)
echo "your seletion is copy"
;;
D)
echo "your seletion is delete"
;;
B)
echo "your seletion is backup"
;;
*) # 参数*, 匹配全部参数。
echo "invalide selection"
;;
esac
whlie 是循坏语句,用在循坏中
格式:
while 条件
do
命令
done
话很少说,继续举例说明:
算出一到十的平方,用脚本写出来。
脚本内容:
文字说明:
[root@localhost ~]# vim pingfang.sh
[root@localhost ~]# chmod +x pingfang.sh
[root@localhost ~]# ./pingfang.sh
1
4
9
16
25
36
49
64
81
100
[root@localhost ~]# cat pingfang.sh
#!/bin/bash
num=1
while [ $num -le 10 ] 当变量小于10的时候执行下面的动做
do
square=`expr $num \* $num` 变量的值相乘。中间要用转移符
echo $square
num=`expr $num + 1` 每次执行加一下
done
五 中双小括号的使用方法
使用(())扩展shell中算数运算的使用方法,使用[]的时候,必须保证运算符与算数之间有空格,四则运算也只能借助expr命令来完成,咱们今天的说的(())结构语句,就是对shell中算数及赋值运算的扩展。
举例说明:
依次输出小于100之内2的幂值,输出的结果应该为:2 4 8 16。
脚本的内容:
文字解释:
[root@localhost ~]# vim mizhi.sh
[root@localhost ~]# chmod +x mizhi.sh
[root@localhost ~]# ./mizhi.sh
the while loop example
value of the variable is : 1
value of the variable is : 2
value of the variable is : 4
value of the variable is : 8
value of the variable is : 16
value of the variable is : 32
value of the variable is : 64
the loop execution is finished
[root@localhost ~]# cat mizhi.sh 查看一下脚本内容
#!/bin/bash
echo "the while loop example"
echo
var1=1
while ((var1<100)) 注意这里能够省去$
do
echo "value of the variable is : $var1"
((var1=var1*2)) 这里也是能够省去$
done
echo
echo "the loop execution is finished"
六 循坏嵌套使用方法
顾名思义就是在一个脚本中使用循坏嵌套来实现更简单的用法。
举例说明;使用特殊符号打印三角形。要求整个程序要有交互,运行时能够自动输入打印的行数和用于描绘三角形的特殊符号。
脚本内容:
文字解释
[root@localhost ~]# vim sanjiao.sh
[root@localhost ~]# chmod +x sanjiao.sh
[root@localhost ~]# ./sanjiao.sh
please enter the line number:6
please enter the char number:*
*
**
***
****
*****
******
[root@localhost ~]# cat sanjiao.sh
#!/bin/bash
read -p "please enter the line number:" line
read -p "please enter the char number:" char
a=1
while [ $a -le $line ]
do
b=1
while [ $b -le $a ] 当变量b的值小于等于变量a的值时,执行do
do
echo -n "$char" 输出
b=`expr $b + 1` 变量b的值加一
done
echo
a=`expr $a + 1`
done
[root@localhost ~]#
写了这么多,你也看出来了。没有什么捷径,捷径就是一天一个脚本,迟早你就会成功。