小白学shell

--------------------------shell script部分------------------------
一、什么是shell
shell是c程序写一门脚本语言,它主要承担是用户和linux系统使用的桥梁。
shell编程指两方面,一个是对shell程序进行编程。一个是使用shell进行编程。

二、shell有那些解释器?
/bin/bash
/bin/sh
/bin/tcsh
/bin/ksh (需要安装)

三、shell运行的环境、运行方式.
shell运行的环境:
shell需要linux 内核的系统都可以。(centos、ubtun、debin、redhat等)
shell承担解释器的作用。

运行方式:
第一种运行方式:
(1)、在文件中先shell脚本
vi first.sh
echo “this is my first shell scrip”
(2)、执行时指定解释器:
/bin/bash ./first.sh

第二种运行方式:
(1)、在脚本中第一句话指定解释器:
#!/bin/bash
(2)、在脚本第一句话下面写脚本语句
#!/bin/bash
echo “this is my first shell scrip”
(3)、给脚本添加执行权限:
chmod a+x first.sh
(4)、执行脚本
./first.sh

四、shell编程
(1)、shell的变量定义和使用
一般情况下:最好根据java命名规则为好
注意:shell变量名不能为:
shell关键词
不能是纯数字等或者特殊符号
首个字符必须为字母(a-z,A-Z)。

定义变量格式:
变量名=值(中间不能有空格)
#!/bin/bash
name=“xiaopan”
echo “this is my first shell scrip: n a m e " e c h o " t h i s i s m y f i r s t s h e l l s c r i p : name" echo "this is my first shell scrip: name"echo"thisismyfirstshellscrip:{name}”

只读变量
name=“xiaopan”
readonly name
name=“xiaoB” //不能再次赋值

删除变量
unset name
可以执行#注释

注意:
单引号字符串的限制:
单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的;
echo 'this is my first shell scrip: n a m e ′ 输 出 结 果 : t h i s i s m y f i r s t s h e l l s c r i p : {name}' 输出结果: this is my first shell scrip: namethisismyfirstshellscrip:{name}
单引号字串中不能出现单引号(对单引号使用转义符后也不行)。

双引号的优点:
双引号里可以有变量
双引号里可以出现转义字符

获取字符串的长度和提取部分字符串
str=“wanglei”
#长度:
echo “KaTeX parse error: Expected '}', got '#' at position 2: {#̲str}" #提取部分字符串 …{str:0:4}”

(2)、shell的数组
Shell 数组:
bash支持一维数组(不支持多维数组),并且没有限定数组的大小。
类似与C语言,数组元素的下标由0开始编号。获取数组中的元素要利用下标,
下标可以是整数或算术表达式,其值应大于或等于0。
(1)、定义数组
stuName=(“xiaoA” “xiaoB” “xiaoC” “xiaoD”)
或者:
stuName=(
“xiaoA”
“xiaoB”
“xiaoC”
“xiaoD” )

(2)、获取素组元素:
${stuName[0]}
${stuName[2]}

(3)获取素组的长度:
${#stuName[*]}
${#stuName[@]}

KaTeX parse error: Expected '}', got '#' at position 2: {#̲stuName}和{#stuName[0]}表示获取第一个元素的长度

=========================================================
(3)、shell运算符操作
a、算数运算符
运算符操作:
expr 是一款表达式计算工具,使用它能完成表达式的求值操作。注意好下面的空格。。。。

  • echo expr 1 + 2
  • echo expr $1 + $2
  • echo expr $1 \* $2
    / echo expr $1 / $2
    % echo expr $1 % $2

案例:
vi test.sh
#!/bin/bash
echo expr 1 + 2
echo expr $1 - $2
echo expr $1 \* $2
echo expr $1 / $2
echo expr $1 % $2

运行:./test.sh 10 20
3
-10
200
0
10

b、关系运算符: 【关系运算符只支持数字,不支持字符串,除非字符串的值是数字。】

-eq 检测两个数是否相等,相等返回 true。 [ $a -eq $b ] 返回 false。 equal
-ne 检测两个数是否相等,不相等返回 true。 [ $a -ne $b ] 返回 true。 not equal
-gt 检测左边的数是否大于右边的,如果是,则返回 true。 [ $a -gt $b ] 返回 false。 greater than
-lt 检测左边的数是否小于右边的,如果是,则返回 true。 [ $a -lt $b ] 返回 true。 less than
-ge 检测左边的数是否大于等于右边的,如果是,则返回 true [ $a -ge $b ] 返回 false。 greater equal
-le 检测左边的数是否小于等于右边的,如果是,则返回 true [ $a -le $b ] 返回 true。 less equal

案例:
#!/bin/bash
#$1、$2表示接收命令传的参数
n=$1
m=$2

if [ $n -eq m ] t h e n e c h o " m ] then echo " m]thenecho"n is equal m " e l s e e c h o " m" else echo " m"elseecho"n is not equal $m"
fi

if [ $n -ne m ] t h e n e c h o " m ] then echo " m]thenecho"n is not equal m " e l s e e c h o " m" else echo " m"elseecho"n is equal $m"
fi

if [ $n -gt m ] t h e n e c h o " m ] then echo " m]thenecho"n is greater than m " e l s e e c h o " m" else echo " m"elseecho"n is not greater than $m"
fi

if [ $n -lt m ] t h e n e c h o " m ] then echo " m]thenecho"n is less than m " e l s e e c h o " m" else echo " m"elseecho"n is not less than $m"
fi

if [ $n -ge m ] t h e n e c h o " m ] then echo " m]thenecho"n is greater equal m " e l s e e c h o " m" else echo " m"elseecho"n is less than $m"
fi

if [ $n -le m ] t h e n e c h o " m ] then echo " m]thenecho"n is less equal m " e l s e e c h o " m" else echo " m"elseecho"n is greater than $m"
fi

执行:
chmod a+x ./test01.sh
./test01.sh 100 200

c、布尔运算符、逻辑运算符
! 非运算,表达式为 true 则返回 false,否则返回 true。 [ ! false ] 返回 true。
-o(||) 或运算,有一个表达式为 true 则返回 true。 [ $a -lt 20 -o $b -gt 100 ] 返回 true。
-a(&&) 与运算,两个表达式都为 true 才返回 true。 [ $a -lt 20 -a $b -gt 100 ] 返回 false。

vi test02.sh

#!/bin/bash
n=$1
m=$2
if ! [ $n -eq $m ]
#if [ ! $n -eq m ] t h e n e c h o " m ] then echo " m]thenecho"n is not equal m " e l s e e c h o " m" else echo " m"elseecho"n is equal $m"
fi

if [ $n -lt $m ] || [ $n -eq $m ]
if [ $n -lt $m -o $n -eq m ] t h e n e c h o " m ] then echo " m]thenecho"n is less than or equal m " e l s e e c h o " m" else echo " m"elseecho"n is greater than $m"
fi

#if [ “10” -eq “10” -a “20” -eq “20” ]
#if [ “10” -eq “10” ] && [ “20” -eq “20” ]
if [ “10” -eq “10” && “20” -eq “20” ]
then
echo “10=10 and 20=20”
fi

执行:./test02.sh 100 200

d、字符串运算符
= 检测两个字符串是否相等,相等返回 true。 [ $a = $b ] 返回 false。
!= 检测两个字符串是否相等,不相等返回 true。 [ $a != $b ] 返回 true。
-z 检测字符串长度是否为0,为0返回 true。 [ -z $a ] 返回 false。
-n 检测字符串长度是否为0,不为0返回 true。 [ -n $a ] 返回 true。
str 检测字符串是否为空,不为空返回 true。 [ $a ] 返回 true。

vi test03.sh
#!/bin/bash
n=“abc”
m=“bc”
#引用系统变量HIVE_HOME
#echo “${HIVE_HOME}”
if [ $n = m ] t h e n e c h o " m ] then echo " m]thenecho"n=$m"
fi
if [ $n != m ] t h e n e c h o " m ] then echo " m]thenecho"n!=$m"
fi

-z判断字符串是否为0 下面-z $n 返回false

if [ ! -z n ] t h e n e c h o " n ] then echo " n]thenecho"n is not 0"
fi
#判断x是否为mull,目前x没有定义
if [ x ] t h e n e c h o " x ] then echo " x]thenecho"n is null"
fi

执行:./test03.sh

e、文件测试运算符
-d file 检测文件是否是目录,如果是,则返回 true。
-f file 检测文件是否是普通文件
-r file 检测文件是否可读,如果是,则返回 true。 [ -r $file ] 返回 true。
-w file 检测文件是否可写,如果是,则返回 true。 [ -w $file ] 返回 true。
-x file 检测文件是否可执行,如果是,则返回 true。 [ -x $file ] 返回 true。
-s file 检测文件是否为空(文件大小是否大于0),不为空返回 true。 [ -s $file ] 返回 true。
-e file 检测文件(包括目录)是否存在,如果是,则返回 true。 [ -e $file ] 返回 true。

案例:
vi test04.sh
#!/bin/bash
fileName=/shell/file.txt
if [ -f f i l e N a m e ] t h e n e c h o " fileName ] then echo " fileName]thenecho"fileName is file"
if [ -r f i l e N a m e ] t h e n e c h o " fileName ] then echo " fileName]thenecho"fileName can read"
fi
if [ -x f i l e N a m e ] t h e n e c h o " fileName ] then echo " fileName]thenecho"fileName can excute"
fi
fi

dir=/shell/folder
if [ -d d i r ] t h e n e c h o " dir ] then echo " dir]thenecho"dir is folder"
fi

if [ -e $dir ] && [ -e $fileName ]
then
echo “exists”
fi

if [ -s f i l e N a m e ] t h e n e c h o " fileName ] then echo " fileName]thenecho"fileName size is 0"
fi

(4)、shell的条件判断
if [ ]
then
command
fi

if [ ]
then
command
else
command
fi

if [ ]
then
command
elif
then
command
fi

if [ ]
then
command
elif
then
command
elif
then
command
else
command
fi

案例:
vi test05.sh
#!/bin/bash
if [ 500 -eq 600 ]
then
echo “500=600”
elif [ 500 -gt 600 ]
then
echo “500>600”
else
echo “500<600”
fi

((5)、shell for/while循环
#!/bin/bash
echo "for i in …====== "
for i in 1 2 3 4 5
do
echo $i
done
echo "for((i=0;i<=10;i++))= "
for((i=0;i<=10;i++))
do
echo $i
done
echo "=for a in seq 1 10
= "
for a in seq 1 10
do
echo $a
done

echo “遍历目录下的文件”
for file in ls ./
do
echo $file
done

while循环
#!/bin/sh
n=0
while(( n < 10 ) ) d o e c h o " n = n<10)) do echo "n= n<10))doecho"n=n"
let “n++”
done

n=0
while(( n < 10 ) ) d o e c h o " n = n<10)) do echo "n= n<10))doecho"n=n"
n=expr $n + 1
done

continue/break/while/case综合使用:
#!/bin/sh
n=0
while(($n<10))
do
case $n in
1|3|5|7|9)
if [ $n -eq 5 ]
then
n=expr $n + 1
continue
fi
echo “n=$n”
;;
2|4|6|8)
if [ n − e q 8 ] t h e n b r e a k f i e c h o " n = n -eq 8 ] then break fi echo "n= neq8]thenbreakfiecho"n=n"
;;
)
echo "
=$n"
;;
esac
n=expr $n + 1
done

(6)、shell的方法定义
1.可选部分:
[function] funname [()]{
action;
[return int;]

}

2.方法先定义在调用:
#!/bin/sh
fun1(){
echo “fun1()”
}
fun1

3.方法的参数:
#!/bin/sh
fun1(){
echo “fun1()”
}
fun1
echo “参数1:$1,参数2:$2”
fun2(){
echo “方法参数1:$1,方法参数2:$2”
}
fun2 10 20

fun3(){
echo “参数1:$1,参数2:$2”
}
fun3 $1 $2

fun4(){
n=expr $1 + $2
return $n
}
fun4 $1 2 e c h o " 2 echo " 2echo"?"

(7)、shell时间

【作业】
1.按照我上面知识点y一个知识点一个案例
2.下载自己写的shell脚本提交.sh文件

五、综合案例
1、删除当前目录下大小为0的文件
vi rmfile.sh
#!/bin/bash
cd $1
for f in ls ./
do
if ! [ -s $f ]
then
rm -rf $f
if [ ? − e q 0 ] t h e n e c h o " ? -eq 0 ] then echo " ?eq0]thenecho"f文件删除成功!"
fi
fi
done

./rmfile.sh /folder2

2、给某个文件夹下所有文件问好
3、列出某个路径下所以文件的路径(非文件夹)
4、判断hdfs文件是否存在
#!/bin/bash
HADOOP=$(which hadoop)
$HADOOP fs -test -e $1
if [ $? -eq 1 ]
then
$HADOOP fs -put $1 /
if [ $? -eq 0 ]
then
echo “the file $1 put successfully!”
fi
fi
1.判断文件是否存在
2.如果不存在,上传文件到hdfs系统
3.判断上传是否成功!

你们做:
1.判断文件是否存在
2.如果文件存在,则删除
3.判断是否删除成功!

7、模拟(删除/远程拷贝)当前一周的日志文件
vi rmlog.sh
#!/bin/bash
cd $1
#获取七天前时间
time7daysago=date -d"7 days ago" +%s
for log in ls ./
do
filetime=basename $log .log
echo “ f i l e t i m e " f i l e t i m e s e c o n d = ‘ d a t e − d " filetime" filetime_second=`date -d " filetime"filetimesecond=dated"filetime” +%s`
if [ $time7daysago -gt $filetime_second ]
then
rm -rf $log
if [ ? − e q 0 ] t h e n e c h o " ? -eq 0 ] then echo " ?eq0]thenecho"log 文件删除成功!"
fi
fi
done

./rmlog.sh /log/

(7题扩展作业,定时器操作、shell脚本)
(1)使用脚本生产日志文件每分钟生产一个log文件(文件格式:2016-12-22-00-01.log、2016-12-22-00-02.log,内容随便)
(2)使用脚本检查删除十分钟前的日志

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值