您的位置:首页 > 运维架构 > Shell

shell 练习

2015-12-08 23:47 591 查看
符号作用
=赋值符号
“”弱引用其内部的变量引用会被替换为变量值
‘’强引用其变量的变量引用会保持原有字符
.字符匹配,这是作为正则表达式的一部分,用来匹配任何单个字符。
逗号链接一系列的算术操作,虽然里面的内容全部运行,但只有最后一项被返回
\转义字符,如\X 等价于"X"或'X'
:空命令,等价于"NOP"(no op,一个什么也不干的命令).也可以被认为与shell 的内建命令
··命令引用(true)作用相同.":"命令是一
#!个 bash 的内建命令,它的返回值为0,就是shell 返回的true.
#注释,用于帮助信息或者忽略暂时不执行的语句
${}变量正规表达式,避免变量名提前截断
$num位置参数$0,$1,…,${10};(注:如多于两位数需加中括号)
$?状态判定参数一般0表示正确(真),其它数字表错误(假)
$!最后一个命令执行的后台命令的ID
$$运行脚本进程的ID
$#传递给脚本或函数的参数的个数
$*和$@引用传递给脚本或函数的参数列表
()指令群组,将一串指令括起来,执行时shell会产生subshell来执行它们
(())bash的内建功能,用于算数运算
[]中括号1 在流程控制中表示判断式; 2 在正则表达式中表示"范围"或"集合"
[[]]中括号的加强版,当中允许使用||和&&,并且可以使用正则表达式
{}花括号指令群组,类似于(),但在当前shell中执行,还可以用于字符串的组合
整数测试隐含着做数值大小比较,所以不要给变量引用加引用
-gt是否大于,是则为真 ,否则为假
-ge是否大于等于
-lt是否小于
-le是否小于等于
-eq是否等于
-ne是否不等于
字符串测试ASCII数值越大,字符比较时其值越大,(注:使用时应用[[]])
>是否大于
<是否小于
==是否等于
!= 是否不等于
-z是否为空,空则为真,否则为假
-n是否不空,不空为真,否则为假
文件测试测试文件的存在性以及属性
-e $FILE是否存在,存在为,真否则为假
-f $FILE文件是否存在且为普通文件
-d存在且为目录
-h存在且为符号链接文件
-b存在且为块设备文件
-c存在且为字符设备文件
-s存在且为套接字文件
-p存在且为管道文件
-r当前用户对文件是否拥有读权限
-w当前用户对文件是否拥有写权限
-x当前用户对文件是否拥有执行权限
-u文件是否拥有suid权限
-g文件是否拥有sgid权限
-k文件是否拥有sticky权限
-O当前用户是否为指定文件的属主
-G当前用户是否为指定文件的属组
写一个脚本:如果某路径不存在,则将其创建为目录;否则显示其存在,并显示内容类型
#!/bin/bash
#
if [ $# -lt 1 ]; then
echo "Plz enter a path:"
exit 1
fi

if [ -e $1 ]; then
file $1
else
mkdir -p $1
fi

~
测试脚本:
[root@www bin]# bash test20.sh
Plz enter a path:
[root@www bin]# bash -x test20.sh abc
+ '[' 1 -lt 1 ']'
+ '[' -e abc ']'
+ mkdir -p abc
[root@www bin]# bash -x test20.sh abc
+ '[' 1 -lt 1 ']'
+ '[' -e abc ']'
+ file abc
abc: directory

写一个脚本,完成如下功能;判断给定的两个数值,孰大孰小;给定数值的方法:脚本参数,命令交互;

#!/bin/bash
#
if (($# < 2)); then
echo "Plz enter Two integer Numbers!"
exit 1
fi

if (($1 > $2)); then
echo "$1 > $2"
elif
(($1 < $2)); then
echo "$1 < $2"
elif
(($1 == $2)); then
echo "$1 = $2"
else
echo "disparate"
fi
~

测试脚本
[root@www bin]# bash test21.sh
Plz enter Two integer Numbers!
[root@www bin]# bash test21.sh 4
Plz enter Two integer Numbers!
[root@www bin]# bash test21.sh 4 5
4 < 5
[root@www bin]# bash test21.sh 5 5
5 = 5
[root@www bin]# bash test21.sh 5 4
5 > 4

求100以内所有奇数之和(至少用3种方法)
使用for

#!/bin/bash
#
for i in $(seq 100); do
if [ $[$i%2] -ne 0 ]; then
h=$[$h+$i]
fi
done

echo "sum:$h"
测试脚本:
[root@www bin]# bash test22.sh
sum:2500

2.使用while
#!/bin/bash
#
h=1
j=0
while [ $h -le 100 ]; do
if [ $[$h%2] -ne 0 ]; then
j=$[$h+$j]
fi
let h++
done
echo $j

测试脚本:
[root@www bin]# bash test23.sh
2500

3.暂时想不到

写一个脚本实现如下功能:
(1) 传递两个文本文件路径给脚本;
(2) 显示两个文件中空白行数较多的文件及其空白行的个数;
(3) 显示两个文件中总行数较多的文件及其总行数
#!/bin/bash
#

if [ $# -lt 2 ]; then
echo "Plz enter Two path:"
exit 1
fi

if [ ! -f $1 ]; then
echo "$1 not a common file!"
exit 1
fi
if [ ! -f $2 ]; then
echo "$2 not a common file!"
exit 1
fi
j=$(grep -c "^$" $1)
h=$(grep -c "^$" $2)
m=$(cat $1 |wc -l)
n=$(cat $2 |wc -l)
if [ $j -ge $h ]; then
echo "$1 is max ,the spaceline total:$j"
else
echo "$2 is max, the spaceline total:$h"
fi
if [ $m -ge $n ]; then
echo "$1 is max, the lines total:$m"
else
echo "$2 is max,the lines total:$n"
fi

测试脚本:
[root@www bin]# bash -x test24.sh /etc/fstab /etc/yum.conf
+ '[' 2 -lt 2 ']'
+ '[' '!' -f /etc/fstab ']'
+ '[' '!' -f /etc/yum.conf ']'
++ grep -c '^$' /etc/fstab
+ j=1
++ grep -c '^$' /etc/yum.conf
+ h=3
++ wc -l
++ cat /etc/fstab
+ m=10
++ wc -l
++ cat /etc/yum.conf
+ n=26
+ '[' 1 -ge 3 ']'
+ echo '/etc/yum.conf is max, the spaceline total:3'
/etc/yum.conf is max, the spaceline total:3
+ '[' 10 -ge 26 ']'
+ echo '/etc/yum.conf is max,the lines total:26'
/etc/yum.conf is max,the lines total:26
[root@www bin]# bash test24.sh
Plz enter Two path:
[root@www bin]# bash test24.sh abc
Plz enter Two path:
[root@www bin]# bash test24.sh abc cba
abc not a common file!

9、写一个脚本
(1) 提示用户输入一个字符串;
(2) 判断:
如果输入的是quit,则退出脚本;
否则,则显示其输入的字符串内容
#!/bin/bash
#
read -p "Plz input a string:" -t 5 a
if [ -z $a ]; then
echo "Plz enter string!"
exit 1
fi
if [[ $a == "quit" ]]; then
exit 1
else
echo "$a"
fi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux—shell练习