shell学习笔记

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell学习笔记相关的知识,希望对你有一定的参考价值。

一、标准输入命令read与实践

1、read基础用法

[[email protected] day3]# read -p "Pls input tow num:" a1 a2
Pls input tow num:1 2

2、小脚本示例

[[email protected] day3]# vim read.sh

read -p "pls input two number:" var1 var2
echo "the nums you input is: $var1 $var2"

3、设置超时时间

[[email protected] day3]# vim read.sh

read -t 5 -p "pls input two number:" var1 var2
echo "the nums you input is: $var1 $var2"

[[email protected] day3]# sh read.sh 
pls input two number:the nums you input is:

4、echo -n配合

[[email protected] day3]# vim echo.sh

echo -n "please input a num:" 
read number
echo $number

[[email protected] day3]# sh echo.sh 
please input a num:45
45

5、加减乘除,read方式

[[email protected] day3]# vim read01.sh

#!/bin/bash
read -p "input input tow num caculate:" num1 num2
echo "$num1-$num2 = $(( $num1 - $num2))"
echo "$num1+$num2 = $(( $num1 + $num2))"
echo "$num1*$num2 = $(( $num1 * $num2))"
echo "$num1/$num2 = $(( $num1 / $num2))"
echo "$num1**$num2 = $(( $num1 ** $num2))"
echo "$num1%$num2 = $(( $num1 % $num2))"

input input tow num caculate:5 2
5-2 = 3
5+2 = 7
5*2 = 10
5/2 = 2
5**2 = 25
5%2 = 1
[[email protected] day3]# 

6、条件判断,只能传两个参数

#!/bin/bash
if [ $# -ne 2 ];then
    echo "USAGE:$0 NUM1 NUM2"
    exit 1
fi
#read -p "input input tow num caculate:" num1 num2
num1=$1
num2=$2
echo "$num1-$num2 = $(( $num1 - $num2))"
echo "$num1+$num2 = $(( $num1 + $num2))"
echo "$num1*$num2 = $(( $num1 * $num2))"
echo "$num1/$num2 = $(( $num1 / $num2))"
echo "$num1**$num2 = $(( $num1 ** $num2))"
echo "$num1%$num2 = $(( $num1 % $num2))"

二、条件测试

1、语法格式

1.1 格式1 test<测试表达式>

判断文件是否存在:
[[email protected] day3]# test -f file && echo true || echo false
false
[[email protected] day3]# 

[[email protected] day3]# touch file
[[email protected] day3]# test -f file && echo true || echo false
true

不存在所以不报错:
[[email protected] day3]# rm -rf file 
[[email protected] day3]# test -f file && cat file
[[email protected] day3]# 
文件存在:
[[email protected] day3]# echo 1 > file
[[email protected] day3]# test -f file && cat file
1
[[email protected] day3]# 

1.2 test的!(非)用法

[[email protected] day3]# rm -rf file 
[[email protected] day3]# test ! -f file && cat file
cat: file: No such file or directory
[[email protected] day3]# 

[[email protected] day3]# echo 1 > file
[[email protected] day3]# test ! -f file && cat file
[[email protected] day3]# 

1.3 查看test帮助

[[email protected] day3]# help test

2、格式2 [<测试表达式>]

2.1 简单示例

[[email protected] day3]# rm -rf file 
[[email protected] day3]# [ -f file ] && echo 1 || echo 0
0
[[email protected] day3]# 

存在:
[[email protected] day3]# touch file
[[email protected] day3]# [ -f file ] && echo 1 || echo 0
1
[[email protected] day3]# 

非:
[[email protected] day3]# [ ! -f file ]&&echo 1 ||echo 0
0
[[email protected] day3]# 

3、格式3:[[ <测试表达式> ]]

[[email protected] day3]# [[ ! -f file ]]&&echo 1 ||echo 0
0
[[email protected] day3]# 

4、&&

[[email protected] day3]# [[ -f file && -d folder ]] && echo 1 || echo 0
0
[[email protected] day3]# mkdir folder
[[email protected] day3]# [[ -f file && -d folder ]] && echo 1 || echo 0
1
[[email protected] day3]# 

格式化2报错:
[[email protected] day3]# [ -f file && -d folder ] && echo 1 || echo 0
-bash: [: missing `]‘
0
[[email protected] day3]# 
需要这样用:

[[email protected] day3]# [ -f file -a -d folder ] && echo 1 || echo 0
1
[[email protected] day3]# 

或
[[email protected] day3]# [ -f file ] && [ -d folder ] && echo 1 || echo 0
1
[[email protected] day3]# 

3、文件测试操作符

3.1 记忆方法

[[email protected] day3]# echo f=file
f=file
[[email protected] day3]# echo d=directory
d=dirctory
[[email protected] day3]# echo s=size
s=size
[[email protected] day3]# echo e=exist
e=exist
[[email protected] day3]# echo r=read
r=read
[[email protected] day3]# echo w=write
w=write
[[email protected] day3]# echo x=executable
x=executable
[[email protected] day3]# echo nt="newer than"
nt=new than
[[email protected] day3]# echo ot="older than"
ot=old than
[[email protected] day3]# 

4、字符串测试操作符

4.1

5、二元比较

5.1 二元比较特殊状况示例

逻辑运算错误:
[[email protected] day3]# [ 2 > 1 ] && echo 1 || echo 0
1
[[email protected] day3]# [ 2 < 1 ] && echo 1 || echo 0
1
[[email protected] day3]# 

转义,正确了:
[[email protected] day3]# [ 2 \< 1 ] && echo 1 || echo 0
0
[[email protected] day3]# 

最好使用二元比较
[[email protected] day3]# [ 2 -gt 1 ] && echo 1 || echo 0
1
[[email protected] day3]# [ 2 -lt 1 ] && echo 1 || echo 0
0
[[email protected] day3]# 

双中括号:
[[email protected] day3]# [[ 2 < 1 ]] && echo 1 || echo 0
0
[[email protected] day3]# [[ 2 < 3 ]] && echo 1 || echo 0
1
[[email protected] day3]# 

建议用==
[[email protected] day3]# [[ 2 == 3 ]] && echo 1 || echo 0
0
[[email protected] day3]# [[ 2 == 2 ]] && echo 1 || echo 0
1
[[email protected] day3]# 
[[email protected] day3]# [[ 2 = 2 ]] && echo 1 || echo 0
1
[[email protected] day3]#

[[email protected] day3]# [[ 2 != 2 ]] && echo 1 || echo 0
0
[[email protected] day3]# 

[[email protected] day3]# [[ 2 -eq 3 ]] && echo 1 || echo 0
0
[[email protected] day3]# [[ 2 -eq 2 ]] && echo 1 || echo 0
1
[[email protected] day3]# 

5.2 二元字符串比较

逻辑报错:
[[email protected] day3]# [ "a" > "ab" ] && echo 1 || echo 0
1
[[email protected] day3]# [ "ab" > "a" ] && echo 1 || echo 0
1
[[email protected] day3]# 

使用转义符号:
[[email protected] day3]# [ "a" \> "ab" ] && echo 1 || echo 0
0
[[email protected] day3]# [ "ab" \> "a" ] && echo 1 || echo 0
1
[[email protected] day3]# 

[[email protected] day3]# [ "ab" \> "aaaa" ] && echo 1 || echo 0
1
[[email protected] day3]# 

6、逻辑操作符

6.1 示例

三、举例

1、条件测试 1表示真,0表示假

条件和if语句对比
[[email protected] day3]# [ -f "$file1" ] && echo 1 || echo 0
0
[[email protected] day3]# if [ -f "$file1" ];then echo 1;else echo 0;fi
0

if [ -f "$file1" ]; then 
    echo 1
else 
    echo 0
fi

提示:
1、以上两条语句的功能是等同的。
2、变量$file加了双引号,这是编程的好习惯,可以防止很多意外的错误发生。

2、文件测试举例

[[email protected] day3]# file1=/etc/services
[[email protected] day3]# file2=/etc/rc.local

如果存在且是文件,为真
[[email protected] day3]# [ -f "$file1" ] &&  echo 1 || echo 0
1

如果存在且是目录,为真
[[email protected] day3]# [ -d "$file1" ] &&  echo 1 || echo 0
0

如果文件存在,且不为空,为真
[[email protected] day3]# [ -s "$file1" ] &&  echo 1 || echo 0
1

如果存在,则为真
[[email protected] day3]# [ -e "$file1" ] &&  echo 1 || echo 0
1

如果去掉引号,测试正常:
[[email protected] day3]# [ -f $file1 ] &&  echo 1 || echo 0
1
[[email protected] day3]# [ -f $file2 ] &&  echo 1 || echo 0
1

[[email protected] day3]# [ -f /etc/services ] &&  echo 1 || echo 0
1
[[email protected] day3]# [ -f /etc/rc.locl ] &&  echo 1 || echo 0
0
[[email protected] day3]# 
[[email protected] day3]# [ -f /etc/rc.local ] &&  echo 1 || echo 0
1

到底怎么使用呢?答:看系统脚本

如果执行不成功,执行后面的
[ -x /usr/sbin/rpc.nfsd ] || exit 5

如果执行成功,执行后面的
[ -x /usr/sbin/rpc.nfsd ] && exit 5

2.1 脚本示例

[[email protected] day3]# cat 04.sh 
echo 1 

[[email protected] day3]# vim 05.sh

[ -x ./04.sh ] || exit 5
./04.sh

[[email protected] day3]# sh 05.sh 
[[email protected] day3]# 

给04.sh加了权限:
[[email protected] day3]# chmod +x 04.sh
[[email protected] day3]# sh 05.sh 
1
[[email protected] day3]# 

3、多条件文件测试

3.1 示例

[[email protected] day3]# [ -f $file1 -a -f $file2 ] && echo 1 || echo 0
1

[[email protected] day3]# [ -f $file1 -a -f ${file:-null} ] && echo 1 || echo 0
0

双引号的作用出来了
[[email protected] day3]# [ -f "$file1" -a -f "$file" ] && echo 1 || echo 0
0
[[email protected] day3]# [ -f "$file1" -o -f "$file" ] && echo 1 || echo 0
1
[[email protected] day3]# 

[[email protected] day3]# [ -f "$file1" -a -d "$file2" ] && echo 1 || echo 0
0

小结:
1、"-a"和"-o" 逻辑操作符用于[]中使用,语法[ $m -a $n ]
2、"&&"和"||" 逻辑操作符号用于[[]]中使用[ $m && $n ]。如果单中括号使用[] && []
3、注意括号两端,必须要有空格

条件之后需要执行多条语句,需要使用大括号
[[email protected] day3]# vim test.sh

[ 3 -ne 3 ] || {
    echo "I am oldboy"
    echo "I am coming"
    exit 1
}

如果使用一行,使用分号隔开:
[[email protected] day3]# [ 3 -ne 3 ] || { echo "I am oldboy";echo "I am coming";exit 1; }

4、字符串测试举例

4.1 例子

字符串要加引号[ -n "$file1" ]

[[email protected] day3]# echo $file2
/etc/rc.local
[[email protected] day3]# echo $file1
/etc/services
[[email protected] day3]# echo $file0

[[email protected] day3]# [ -n "$file" ] && echo 1 || echo 0
0

内容为空,为真:
[[email protected] day3]# [ -z "$file" ] && echo 1 || echo 0
1

[[email protected] day3]# [ -z "$file1" ] && echo 1 || echo 0
0
[[email protected] day3]# [ -n "$file1" ] && echo 1 || echo 0
1

小结:
    -z STRING 字符串为空为真
    -n STRING 字符串不为空则为真

5、多条件字符串测试

5.1 例子

[[email protected] day3]# echo $file

[[email protected] day3]# echo $file1
/etc/services
[[email protected] day3]# echo $file2
/etc/rc.local

[[email protected] day3]# [ -n "$file1" -a -z "$file2" ] && echo 1 || echo 0
0

一个为真,输出1
[[email protected] day3]# [[ -n "$file1" || -n "$file2" ]] && echo 1 || echo 0
1

[[email protected] day3]# [[ "$file1" = "$file2"  ]] && echo 1 || echo 0
0
[[email protected] day3]# 

[[email protected] day3]# [[ "$file1" != "$file2"  ]] && echo 1 || echo 0
1

比较长度:
[[email protected] day3]# [[ "${#file1}" = "${#file2}"  ]] && echo 1 || echo 0
1
[[email protected] day3]# 

[[email protected] day3]# [[ "${#file1}" != "${#file2}"  ]] && echo 1 || echo 0
0

小结:
字符串比较,必须加双引号。
[ -z "$file1" ]

[ "$file1" != "$file2" ]

6、整数测试

6.1 示例

整数比较不要加双引号

[[email protected] day3]# a1=10;a2=13
[[email protected] day3]# echo $a1 $a2
10 13

[[email protected] day3]# [ $a1 -eq $a2 ] && echo 1 || echo 0
0

[[email protected] day3]# [ $a1 -gt $a2 ] && echo 1 || echo 0
0
[[email protected] day3]# 
[[email protected] day3]# [ $a1 -ge $a2 ] && echo 1 || echo 0
0
[[email protected] day3]# 
[[email protected] day3]# [ $a1 -ne $a2 ] && echo 1 || echo 0
1
[[email protected] day3]# [ $a1 -le $a2 ] && echo 1 || echo 0
1

[[email protected] day3]# [[ $a1 -eq $a2 ]] && echo 1 || echo 0
0
[[email protected] day3]# [[ $a1 -gt $a2 ]] && echo 1 || echo 0
0
[[email protected]4 day3]# [[ $a1 -g\lt $a2 ]] && echo 1 || echo 0
[[email protected] day3]# [[ $a1 -lt $a2 ]] && echo 1 || echo 0
1

[[email protected] day3]# [[ $a1 = $a2 ]] && echo 1 || echo 0
0
[[email protected] day3]# [[ $a1 != $a2 ]] && echo 1 || echo 0
1
[[email protected] day3]# [[ $a1 > $a2 ]] && echo 1 || echo 0
0
[[email protected] day3]# [[ $a1 < $a2 ]] && echo 1 || echo 0
1

双小括号比较:
[[email protected] day3]# (( $a1 > $a2 )) && echo 1 || echo 0
0
[[email protected] day3]# (( $a1 < $a2 )) && echo 1 || echo 0
1

7、test命令

7.1 测试示例

[[email protected] day3]# test -z "$file1" && echo 1 || echo 0
0

[[email protected] day3]# test -n "$file1" && echo 1 || echo 0
1

[[email protected] day3]# test 3 -ne 3 || echo 0
0

[[email protected] day3]# test "dd" != "ff" || echo 0
[[email protected] day3]# 

[[email protected] day3]# test "dd" != "dd" || echo 0
0

test对文件的测试:
[[email protected] day3]# test  ! -f /etc/rc.local || echo 1
1

[[email protected] day3]# touch file1
[[email protected] day3]# touch file2
[[email protected] day3]# 
[[email protected] day3]# [ file1 -nt file2 ] && echo 1 || echo 0
0
[[email protected] day3]# [ file1 -ot file2 ] && echo 1 || echo 0
1
[[email protected] day3]# 

[[email protected] day3]# test file1 -ot file2 && echo 1 || echo 0
1

有关test,[],[[]]的操作符的用法,help test.

8、逻辑操作符测试

8.1 示例

[[email protected] day3]# [ -f "$file1" -a "$file2" ] && echo 1 || echo 0
1

[[email protected] day3]# [ -f "$file1" -o "$file2" ] && echo 1 || echo 0
1

[[email protected] day3]# [ ! -f "$file1" -o ! "$file2" ] && echo 1 || echo 0
0

[[email protected] day3]# [ -f "$file1" -a -f "$file2" ] && echo 1 || echo 0
1
[[email protected] day3]# [ ! -f "$file1" -a -f "$file2" ] && echo 1 || echo 0
0

[[email protected] day3]# test ! -f "$file1" -a -f "$file2" && echo 1 || echo 0
0

[[email protected] day3]# test ! -f "$file1" && -f "$file23" && echo 1 || echo 0
0

[[email protected] day3]# test -f "$file1" && -o "$file2" && echo 1 || echo 0
-bash: -o: command not found
0
[[email protected] day3]# 
[[email protected] day3]# test -f "$file1" -o -f "$file2" && echo 1 || echo 0
1

四、示例

1、read打印菜单

[[email protected] day3]# cat test01.sh 
cat <<END
    1.DDD
    2.FFF
END
[[email protected] day3]# sh test01.sh 
    1.DDD
    2.FFF

1.1 menu.sh ,打印一个简单菜单

[[email protected] day3]# cat menu.sh 
menu(){
cat <<END
    1.[install lamp]
    2.[install lnmp]
    3.[exit]
    pls input the num you want:
END
}

menu

改进:
[[email protected] day3]# cat menu.sh 
menu(){
cat <<END
    1.[install lamp]
    2.[install lnmp]
    3.[exit]
    pls input the num you want:
END
}
menu
read num
echo "you have selected $num"

测试:
[[email protected] day3]# sh menu.sh 
    1.[install lamp]
    2.[install lnmp]
    3.[exit]
    pls input the num you want:
2
you have selected 2
[[email protected] day3]# 

继续改进:
[[email protected] day3]# cat menu.sh 
menu(){
cat <<END
    1.[install lamp]
    2.[install lnmp]
    3.[exit]
    pls input the num you want:
END
}
menu
read num
echo "you have selected $num"
[ $num -eq 1 ]  && {
    echo "starting install lamp"
    #/bin/sh /server/scripts/lamp.sh
    exit 
}

[ $num -eq 2 ] && {
    echo "staring install lnmp"
    #/bin/sh /server/scripts/lnmp.sh
    exit 
}

[ $num -eq 3 ] && {
    echo "this scripts logout."
    exit 
}

[ ! $num -eq 1 -o ! $num -eq 2 -o ! $num -eq 3 ] &&{
    echo "bye"
    exit 1
}

1.2 多级菜单

[[email protected] day3]# cat menu1.sh 
menu1(){
cat <<END
    ********************************
    1.[install lamp]
    2.[install lnmp]
    3.[exit]
    pls input the num you want:
   ********************************* 
END
}

menu2(){
cat <<END
    =================================
    1.[install apache]
    2.[install php]
    3.[instal mysql]
    4.[back]
    pls input the num you want:
    =================================
END
}

menu1
read -p "you input the num is:" num
[ $num -eq 1 ]  && {
    menu2
    read -p "you input the num is:" num2
    [ $num2 -eq 1 ] && {
    echo "start installing apache."
    exit
    }
}

测试:
[[email protected] day3]# sh menu1.sh 
    ********************************
    1.[install lamp]
    2.[install lnmp]
    3.[exit]
    pls input the num you want:
   ********************************* 
you input the num is:1
    =================================
    1.[install apache]
    2.[install php]
    3.[instal mysql]
    4.[back]
    pls input the num you want:
    =================================
you input the num is:1
start installing apache.

以上是关于shell学习笔记的主要内容,如果未能解决你的问题,请参考以下文章

[原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段

Shell脚本(学习笔记1)

shell脚本编程学习笔记-函数

16/11/2017 shell学习笔记

linux学习笔记--工程师技术:shell脚本基础

《用Python玩转数据》学习笔记