什么是Shell
Shell是用C语言编写的应用程序,用户可以通过该应用程序提供的界面访问操作系统内核的服务。
界面的分类:命令行界面和图形化操作界面。
什么是Shell脚本
Shell脚本是由Linux指令组成的可执行文件。
写一个简单的Shell脚本
代码如下:
#!/bin/bash
#使用echo输出一行字符串
echo "hello Linux"

练习使用Shell变量
代码如下:
#!/bin/bash
MySystem="CentOS"
echo "hello $MySystem"

练习使用Shell的命令行参数
代码如下:
#!/bin/bash
Sum=`expr $1 + $2`
echo "$1 + $2 = $Sum"

练习使用Shell的命令替换
代码如下:
#!/bin/sh
FilePath=`pwd`
echo "Current Path is: $FilePath"

练习使用Shell的变量替换
#!/bin/bash
Name="zhangsan"
Age=23
#Salary=12000
echo "姓名: $Name; 年龄: $Age; 工资: ${Salary-"空"}"

练习使用Shell的算术运算符
代码如下:
#!/bin/bash
Num1=`expr $1 + $2`
Num2=`expr $1 - $2`
Num3=`expr $1 \* $2`
Num4=`expr $1 / $2`
Num5=`expr $1 % $2`
echo "$1 + $2 = $Num1"
echo "$1 - $2 = $Num2"
echo "$1 * $2 = $Num3"
echo "$1 / $2 = $Num4"
echo "$1 % $2 = $Num5"

练习使用Shell的关系运算符和布尔运算符(分支结构)
代码如下:
#!/bin/bash
echo -n "请输入成绩:"
read Score
if [ $Score -ge 0 -a $Score -lt 60 ]
then
echo "不及格"
elif [ $Score -ge 60 -a $Score -lt 80 ]
then
echo "通过"
elif [ $Score -ge 80 -a $Score -le 100 ]
then
echo "优秀"
else
echo "输入的成绩错误!"
fi

练习字符串的比较运算
代码如下:
#!/bin/bash
Str1="hello world"
Str2="hello world"
if [ "$Str1" = "$Str2" ]
then
echo "true"
else
echo "false"
fi

练习使用Shell的文件测试运算符
代码如下:
#!/bin/bash
#echo $HOME
Files=$HOME/*
#文件个数
FileCount=0
#文件夹个数
FolderCount=0
for File in $Files
do
echo $File
if test -f $File
then
FileCount=`expr $FileCount + 1`
fi
if test -d $File
then
FolderCount=`expr $FolderCount + 1`
fi
done
echo "文件夹个数:$FolderCount"
echo "普通文件个数:$FileCount"

练习Shell的字符串的操作
代码如下:
#!/bin/bash
Str="Ubuntu"
Str2="hello SQL Server"
echo "${Str}的字符个数:${#Str}"
echo ${Str2:6:3}

练习case-esac结构(类似switch-case结构)
代码如下:
#!/bin/bash
#echo $HOME
Files=$HOME/*
Option=$1
case $Option in
-f) for File in $Files
do
if test -f $File
then
echo $File
fi
done
;;
-d) for File in $Files
do
if test -d $File
then
echo $File
fi
done
;;
*) echo "`basename $0`:usage:[-f] | [-d]"
echo "[-f]: 显示用户家目录下的所有普通文件"
echo "[-d]: 显示用户家目录下的所有子文件夹"
exit 1
;;
esac

设计一个猜拳游戏的Shell脚本(循环结构)
代码如下:
#!/bin/bash
echo "-----猜拳游戏-----"
while [ "1" = "1" ]
do
PC=$((RANDOM%3))
echo -n "请输入您的选择(1——石头 2——剪刀 3——布):"
read Player
if [ ${Player} -ne 1 -a ${Player} -ne 2 -a ${Player} -ne 3 ]
then
echo "您的选择无效!"
continue
fi
case $Player in
1) echo "您的选择是:<石头>"
;;
2) echo "您的选择是:<剪刀>"
;;
3) echo "您的选择是:<布>"
;;
*) ;;
esac
case $PC in
1) echo "我的选择是:<石头>"
;;
2) echo "我的选择是:<剪刀>"
;;
3) echo "我的选择是:<布>"
;;
*) ;;
esac
if test ${Player} -eq ${PC}
then
echo "平局"
elif [[ (${Player} == 1 && ${PC} == 2) || (${Player} == 2 && ${PC} -eq 3) || (${Player} == 3 && ${PC} == 1) ]]
then
echo "恭喜!赢了!"
else
echo "很遗憾!您输了!"
fi
echo -n "再来一局?(y/n)"
read YesOrNo
if test $YesOrNo = "y" -o $YesOrNo = "Y"
then
continue
else
break
fi
done
echo "游戏结束!"

练习输出重定向
Linux指令如下:
ping -c 4 192.168.1.7 > my.log

定时执行Shell脚本
* * * * *
分 时 日 月 星期几
Shell脚本文件test.sh的代码如下:
#!/bin/bash
Str=`ls /home/hadoop/logs`
if [ "$Str" != "" ]
then
cd /home/hadoop/logs
rm -rf *
if test $? -eq 0
then
echo "删除成功!"
else
echo "删除失败!"
fi
else
echo "没有文件!"
fi
设置定时任务:
1)使用crontab打开编辑器,添加任务,保存退出
2)使用crontab查看定时任务
[root@lsp ~]# crontab -e
no crontab for root - using an empty one
*/5 * * * * /home/hadoop/scripts/test.sh
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"/tmp/crontab.XXXXbeJJY9" 1L, 41C written
crontab: installing new crontab
[root@lsp ~]# crontab -l
*/5 * * * * /home/hadoop/scripts/test.sh
[root@lsp ~]#

网友评论