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

Linux基础入门及系统管理01-bash脚本编程之五字符串测试及for循环21

2014-04-19 20:50 1016 查看
一、字符串测试
1、==或=:测试是否相等,相等为真,不能为假。等号前后有空格;
2、!=:测试是否不等,不等为真,等为假;
3、>:大于;
4、<:小于;
5、-n string:测试指定字符串是否为空,空为真,不空为假;
6、-z string:测试指定字符串是否不空,不空为真,空为假;
练习1,写脚本:
传递一个用户参数给脚本,判断此用户的用户名跟其基本组的组名是否一致,并将结果显示出来;
参考答案:# nano usertest.sh
#!/bin/bash
#
if ! id $1 &> /dev/null;then
echo "Not such user."
exit 6
fi
if [ $1 == 'id -n -g $1' ];then
echo "Equal."
else
echo "Not equal."
fi
练习2,写脚本:
传递一个参数(单字符就行)给脚本,如参数为q、Q、quit或Quit,就退出脚本;否则,就显示用户的参数;
参考答案:# nano string.sh
#!/bin/bash
#
if [ $1 == q ];then
echo "Quiting..."
exit 1
elif [ $1 == Q ];then
echo "Quiting..."
exit 2
elif [ $1 == quit ];then
echo "Quiting..."
exit 3
elif [ $1 == Quit ];then
echo "Quiting..."
exit 4
else
echo "$1"
fi
练习3,写脚本:
传递三个参数给脚本,第一个为整数,第二个为算术运算符,第三个为整数,将计算结果显示出来,要求保留两位精度,形如:./calc.sh 5 / 2;
参考答案:# nano match1.sh
#!/bin/bash
#
echo "scale=2;$1$2$3;" | bc
练习4,写脚本:
给脚本传递三个整数,判断其中的最大数和最小数,并显示出来。
参考答案:# nano maxmin.sh
#!/bin/bash
#
if [ $1 -gt $2 ];then
max=$1
else
max=$2
fi
if [ $1 -gt $3 ];then
max=$1
else
max=$3
fi
if [ $2 -gt $max ];then
echo "The max is $2."
else
echo "The max is $max."
fi

if [ $1 -lt $2 ];then
min=$1
else
min=$2
fi
if [ $1 -lt $3 ];then
min=$1
else
min=$3
fi
if [ $2 -lt $min ];then
echo "The min is $2."
else
echo "The min is $min."
fi

练习5,写脚本:
判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor id一行中。
如果其生产商为AuthenticAMD,就显示其为AMD公司;
如果其生产商为GenuineIntel,就显示其为Intel公司;
否则,就说其为非主流公司。
参考答案:# nano cpu.sh
#!/bin/bash
#
TYPE=`sed -n '2p' /proc/cpuinfo | cut -d: -f2 | sed -r 's/^[[:space:]]+//g'`
if [ $TYPE == 'AuthenticAMD' ];then
echo "The cpu is AMD company."
elif [ $TYPE == 'GenuineIntel' ];then
echo "The cpu is Inter company."
else
echo "The cpu is other company."
fi

练习6,写脚本:
传递3个参数给脚本,参数均为用户名。将此些用户的帐号信息提取出来后放置于/tmp/testusers.txt文件中,并要求每一行行首有行号。
提示:echo "1 $LINE" >> /tmp/testusers
echo "2 $LINE" >> /tmp/testusers
参考答案:# nano user.sh
#!/bin/bash
#
for I in `seq 1 $#`;do
string="$I grep "^$1"`/etc/passwd"
echo "$string" >> /tmp/testusers.txt
shift
done
或者
#!/bin/bash
#
echo "1 `grep "^$1" /etc/passwd`" >> /tmp/testusers.txt
echo "2 `grep "^$2" /etc/passwd`" >> /tmp/testusers.txt
echo "3 `grep "^$3" /etc/passwd`" >> /tmp/testusers.txt

二、for循环
1、循环:进入条件,退出条件;
2、常用循环:for、while、until;
3、for循环的使用语法:
for 变量 in 列表;do
循环体
done
4、如何生成列表:
{1..100};
seq [起始数 [步进长度]] 结束数;
如:计算1+...+100的整数之和;
#!/bin/bash
#
declare -i SUM=0
for I in {1..100};do
let SUM=$[$SUM+$I]
done
echo "SUM=$SUM."

5、declare -i SUM=0:声明SUM为整型;
如:如何向/etc/passwd中的用户问好;
# LINE=`wc -l /etc/passwd | cut -d' ' -f1`
# for I in `seq 1 $LINE`;do echo "Hello,`head -n -$I /etc/passwd | cut -d: -f1 | tail -1`."; done
练习1,写脚本:
1、设定变量FILE的值为/etc/passwd;
2、依次向/etc/passwd中的每个用户问好,并显示对方的shell,形如:
Hello,root,you shell:/bin/bash
3、统计一共有多少用户;
4、(扩展),只向默认shell为bash的用户问声好;
参考答案:# nano hello.sh
#!/bin/bash
#
FILE=/etc/passwd
LINE=`wc -l $FILE | cut -d' ' -f1`
for I in `seq 1 $LINE`;do
echo "Hello,`head -$I $FILE | cut -d: -f1 | tail -1`,you shell:`head -$I $FILE | cut -d: -f7 | tail -1`"
done
echo "The users sum $LINE."
练习2,写脚本:
1、添加10个用户user1到user10,密码同用户名:但要求只有用户不存在的情况下才能添加;
参考答案:# nano adduser.sh
#!/bin/bash
#
for I in `seq 1 10`;do
if grep "\<user$I\>" /etc/passwd &> /dev/null;then
echo "user$I exists."
else
useradd user$I
echo "user$I" | passwd --stdin user$I &> /dev/null
echo "Add user$I successfully."
fi
done

练习3,写脚本:
计算100以内所有能被3整除的正整数的和;
参考答案:# nano three.sh
#!/bin/bash
#
declare -i SUM=0
for I in `seq 1 100`;do
if [ $[$I%3] -eq 0 ];then
SUM=SUM+$I
fi
done
echo "The sum is:$SUM"

练习4,写脚本:
计算100以内所有奇数的和以及所有偶数的和:分别显示;
参考答案:# nano sum.sh
#!/bin/bash
#
declare -i SUM1=0
declare -i SUM2=0
for I in `seq 1 100`;do
if [ $[$I%2] -eq 0 ];then
SUM1=SUM1+$I
else
SUM2=SUM2+$I
fi
done
echo "The even sum is:$SUM1."
echo "The obb sum is:$SUM2."

练习5,写脚本:
分别显示当前系统上所有默认shell为bash的用户和默认shell为/sbin/nologin的用户,并统计各类shell下的用户总数。
显示结果形如:
BASH,3users,they are:root,redhat,gentoo
NOLOGIN,2user,they are:bin,ftp
参考答案:# nano bashuser.sh
#!/bin/bash
#
NUM1=`grep "bash$" /etc/passwd | wc -l`
BASHUSERS1=`grep "bash$" /etc/passwd | cut -d: -f1`
BASHUSERS1=`echo $BASHUSERS1 | sed 's@[[:space:]]@,@g'`
echo "BASH,$NUM1users,they are:"
echo "$BASHUSERS1."

本文出自 “Jessen Liu的博文” 博客,请务必保留此出处http://zkhylt.blog.51cto.com/3638719/1398718
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐