码迷,mamicode.com
首页 > 系统相关 > 详细

shell学习笔记(2)

时间:2018-03-15 23:04:24      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:shell 基础 杂记

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

1、read基础用法

[root@master4 day3]# read -p "Pls input tow num:" a1 a2
Pls input tow num:1 2

2、小脚本示例

[root@master4 day3]# vim read.sh

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

3、设置超时时间

[root@master4 day3]# vim read.sh

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

[root@master4 day3]# sh read.sh 
pls input two number:the nums you input is:

4、echo -n配合

[root@master4 day3]# vim echo.sh

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

[root@master4 day3]# sh echo.sh 
please input a num:45
45

5、加减乘除,read方式

[root@master4 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
[root@master4 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<测试表达式>

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

[root@master4 day3]# touch file
[root@master4 day3]# test -f file && echo true || echo false
true

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

1.2 test的!(非)用法

[root@master4 day3]# rm -rf file 
[root@master4 day3]# test ! -f file && cat file
cat: file: No such file or directory
[root@master4 day3]# 

[root@master4 day3]# echo 1 > file
[root@master4 day3]# test ! -f file && cat file
[root@master4 day3]# 

1.3 查看test帮助

[root@master4 day3]# help test

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

2.1 简单示例

[root@master4 day3]# rm -rf file 
[root@master4 day3]# [ -f file ] && echo 1 || echo 0
0
[root@master4 day3]# 

存在:
[root@master4 day3]# touch file
[root@master4 day3]# [ -f file ] && echo 1 || echo 0
1
[root@master4 day3]# 

非:
[root@master4 day3]# [ ! -f file ]&&echo 1 ||echo 0
0
[root@master4 day3]# 

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

[root@master4 day3]# [[ ! -f file ]]&&echo 1 ||echo 0
0
[root@master4 day3]# 

4、&&

[root@master4 day3]# [[ -f file && -d folder ]] && echo 1 || echo 0
0
[root@master4 day3]# mkdir folder
[root@master4 day3]# [[ -f file && -d folder ]] && echo 1 || echo 0
1
[root@master4 day3]# 

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

[root@master4 day3]# [ -f file -a -d folder ] && echo 1 || echo 0
1
[root@master4 day3]# 

或
[root@master4 day3]# [ -f file ] && [ -d folder ] && echo 1 || echo 0
1
[root@master4 day3]# 

3、文件测试操作符

3.1 记忆方法

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

4、字符串测试操作符

4.1

5、二元比较

5.1 二元比较特殊状况示例

逻辑运算错误:
[root@master4 day3]# [ 2 > 1 ] && echo 1 || echo 0
1
[root@master4 day3]# [ 2 < 1 ] && echo 1 || echo 0
1
[root@master4 day3]# 

转义,正确了:
[root@master4 day3]# [ 2 \< 1 ] && echo 1 || echo 0
0
[root@master4 day3]# 

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

双中括号:
[root@master4 day3]# [[ 2 < 1 ]] && echo 1 || echo 0
0
[root@master4 day3]# [[ 2 < 3 ]] && echo 1 || echo 0
1
[root@master4 day3]# 

建议用==
[root@master4 day3]# [[ 2 == 3 ]] && echo 1 || echo 0
0
[root@master4 day3]# [[ 2 == 2 ]] && echo 1 || echo 0
1
[root@master4 day3]# 
[root@master4 day3]# [[ 2 = 2 ]] && echo 1 || echo 0
1
[root@master4 day3]#

[root@master4 day3]# [[ 2 != 2 ]] && echo 1 || echo 0
0
[root@master4 day3]# 

[root@master4 day3]# [[ 2 -eq 3 ]] && echo 1 || echo 0
0
[root@master4 day3]# [[ 2 -eq 2 ]] && echo 1 || echo 0
1
[root@master4 day3]# 

5.2 二元字符串比较

逻辑报错:
[root@master4 day3]# [ "a" > "ab" ] && echo 1 || echo 0
1
[root@master4 day3]# [ "ab" > "a" ] && echo 1 || echo 0
1
[root@master4 day3]# 

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

[root@master4 day3]# [ "ab" \> "aaaa" ] && echo 1 || echo 0
1
[root@master4 day3]# 

6、逻辑操作符

6.1 示例

三、举例

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

条件和if语句对比
[root@master4 day3]# [ -f "$file1" ] && echo 1 || echo 0
0
[root@master4 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、文件测试举例

[root@master4 day3]# file1=/etc/services
[root@master4 day3]# file2=/etc/rc.local

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

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

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

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

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

[root@master4 day3]# [ -f /etc/services ] &&  echo 1 || echo 0
1
[root@master4 day3]# [ -f /etc/rc.locl ] &&  echo 1 || echo 0
0
[root@master4 day3]# 
[root@master4 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 脚本示例

[root@master4 day3]# cat 04.sh 
echo 1 

[root@master4 day3]# vim 05.sh

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

[root@master4 day3]# sh 05.sh 
[root@master4 day3]# 

给04.sh加了权限:
[root@master4 day3]# chmod +x 04.sh
[root@master4 day3]# sh 05.sh 
1
[root@master4 day3]# 

3、多条件文件测试

3.1 示例

[root@master4 day3]# [ -f $file1 -a -f $file2 ] && echo 1 || echo 0
1

[root@master4 day3]# [ -f $file1 -a -f ${file:-null} ] && echo 1 || echo 0
0

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

[root@master4 day3]# [ -f "$file1" -a -d "$file2" ] && echo 1 || echo 0
0

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

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

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

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

4、字符串测试举例

4.1 例子

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

[root@master4 day3]# echo $file2
/etc/rc.local
[root@master4 day3]# echo $file1
/etc/services
[root@master4 day3]# echo $file0

[root@master4 day3]# [ -n "$file" ] && echo 1 || echo 0
0

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

[root@master4 day3]# [ -z "$file1" ] && echo 1 || echo 0
0
[root@master4 day3]# [ -n "$file1" ] && echo 1 || echo 0
1

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

5、多条件字符串测试

5.1 例子

[root@master4 day3]# echo $file

[root@master4 day3]# echo $file1
/etc/services
[root@master4 day3]# echo $file2
/etc/rc.local

[root@master4 day3]# [ -n "$file1" -a -z "$file2" ] && echo 1 || echo 0
0

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

[root@master4 day3]# [[ "$file1" = "$file2"  ]] && echo 1 || echo 0
0
[root@master4 day3]# 

[root@master4 day3]# [[ "$file1" != "$file2"  ]] && echo 1 || echo 0
1

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

[root@master4 day3]# [[ "${#file1}" != "${#file2}"  ]] && echo 1 || echo 0
0

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

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

6、整数测试

6.1 示例

整数比较不要加双引号

[root@master4 day3]# a1=10;a2=13
[root@master4 day3]# echo $a1 $a2
10 13

[root@master4 day3]# [ $a1 -eq $a2 ] && echo 1 || echo 0
0

[root@master4 day3]# [ $a1 -gt $a2 ] && echo 1 || echo 0
0
[root@master4 day3]# 
[root@master4 day3]# [ $a1 -ge $a2 ] && echo 1 || echo 0
0
[root@master4 day3]# 
[root@master4 day3]# [ $a1 -ne $a2 ] && echo 1 || echo 0
1
[root@master4 day3]# [ $a1 -le $a2 ] && echo 1 || echo 0
1

[root@master4 day3]# [[ $a1 -eq $a2 ]] && echo 1 || echo 0
0
[root@master4 day3]# [[ $a1 -gt $a2 ]] && echo 1 || echo 0
0
[root@master4 day3]# [[ $a1 -g\lt $a2 ]] && echo 1 || echo 0
[root@master4 day3]# [[ $a1 -lt $a2 ]] && echo 1 || echo 0
1

[root@master4 day3]# [[ $a1 = $a2 ]] && echo 1 || echo 0
0
[root@master4 day3]# [[ $a1 != $a2 ]] && echo 1 || echo 0
1
[root@master4 day3]# [[ $a1 > $a2 ]] && echo 1 || echo 0
0
[root@master4 day3]# [[ $a1 < $a2 ]] && echo 1 || echo 0
1

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

7、test命令

7.1 测试示例

[root@master4 day3]# test -z "$file1" && echo 1 || echo 0
0

[root@master4 day3]# test -n "$file1" && echo 1 || echo 0
1

[root@master4 day3]# test 3 -ne 3 || echo 0
0

[root@master4 day3]# test "dd" != "ff" || echo 0
[root@master4 day3]# 

[root@master4 day3]# test "dd" != "dd" || echo 0
0

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

[root@master4 day3]# touch file1
[root@master4 day3]# touch file2
[root@master4 day3]# 
[root@master4 day3]# [ file1 -nt file2 ] && echo 1 || echo 0
0
[root@master4 day3]# [ file1 -ot file2 ] && echo 1 || echo 0
1
[root@master4 day3]# 

[root@master4 day3]# test file1 -ot file2 && echo 1 || echo 0
1

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

8、逻辑操作符测试

8.1 示例

[root@master4 day3]# [ -f "$file1" -a "$file2" ] && echo 1 || echo 0
1

[root@master4 day3]# [ -f "$file1" -o "$file2" ] && echo 1 || echo 0
1

[root@master4 day3]# [ ! -f "$file1" -o ! "$file2" ] && echo 1 || echo 0
0

[root@master4 day3]# [ -f "$file1" -a -f "$file2" ] && echo 1 || echo 0
1
[root@master4 day3]# [ ! -f "$file1" -a -f "$file2" ] && echo 1 || echo 0
0

[root@master4 day3]# test ! -f "$file1" -a -f "$file2" && echo 1 || echo 0
0

[root@master4 day3]# test ! -f "$file1" && -f "$file23" && echo 1 || echo 0
0

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

四、示例

1、read打印菜单

[root@master4 day3]# cat test01.sh 
cat <<END
    1.DDD
    2.FFF
END
[root@master4 day3]# sh test01.sh 
    1.DDD
    2.FFF

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

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

menu

改进:
[root@master4 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"

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

继续改进:
[root@master4 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 多级菜单

[root@master4 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
    }
}

测试:
[root@master4 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学习笔记(2)

标签:shell 基础 杂记

原文地址:http://blog.51cto.com/zhongle21/2087364

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!