字符测试:
==:测试是否相等,相等为真,不等为假
!=: 测试是否不等,不等为真,等为假
>
<
-n string: 测试指定字符串是否为空,空则真,不空则假
-z string: 测试指定字符串是否不空,不空为真,空则为假
组合测试条件
-a: 与关系
-o: 或关系
!: 非关系
练习:
[root@localhost ~]# useradd -g hello user2
useradd: user ‘user2‘ already exists
[root@localhost ~]# useradd -g hello hi
[root@localhost ~]# ./same.sh hi
different
[root@localhost ~]# ./same.sh user1
same
[root@localhost ~]# cat ./same.sh
#!/bin/bash
练习:
if [ `id -n -u $1` == `id -n -g $1` ]; then
echo "same"
else
echo "different"
fi
[root@localhost ~]#
[root@localhost ~]# ./test2.sh asdf
no such user
[root@localhost ~]# ./test2.sh user1
same
[root@localhost ~]# ./test2.sh hi
different
[root@localhost ~]#
练习:写一个脚本
传递一个参数(单字符就行)给脚本,如参数为q,就退出脚本;否则,就显示用户的参数;
练习:写一个脚本
传递一个参数(单字符就行)给脚本,如参数为q、Q、quit或Quit,就退出脚本;否则,就显示用户的参数;
#!/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
练习:
传递三个参数给脚本,第一个为整数,第二个为算术运算符,第三个为整数,将计算结果显示出来,要求保留两位精度。形如:
./calc.sh 5 / 2
echo "scale=2;111/22;" | bc
bc <<< scale=2;111/22;"
[root@localhost ~]# nano calc.sh
[root@localhost ~]# chmod +x calc.sh
[root@localhost ~]# ./calc.sh 111 / 22
5.04
[root@localhost ~]# ./calc.sh 5 / 2
2.50
[root@localhost ~]# cat calc.sh
#!/bin/bash
echo "scale=2;$1$2$3" | bc
[root@localhost ~]#
练习:
传递3个参数给脚本,参数均为用户名。将此些用户的帐号信息提取出来后放置于/tmp/testusers.txt文件中,并要求每一行行首有行号。
写一个脚本:
判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor id一行中。
如果其生产商为AuthenticAMD,就显示其为AMD公司;
如果其生产商为GenuineIntel,就显示其为Intel公司;
否则,就说其为非主流公司;
写一个脚本:
给脚本传递三个整数,判断其中的最大数和最小数,并显示出来。
[root@localhost ~]# nano max.sh
[root@localhost ~]# ./max.sh 1 2 3
max: 3
[root@localhost ~]# cat ./max.sh
#!/bin/bash
if [ $1 -gt $2 ];then
MAX=$1
else
MAX=$2
fi
if [ $MAX -gt $3 ];then
MAX=$MAX
else
MAX=$3
fi
echo "max: $MAX"
[root@localhost ~]#
循环:进入条件,退出条件
for
while
until
for 变量 in 列表; do
循环体
done
for I in 1 2 3 4 5 6 7 8 9 10; do
加法运算
done
遍历完成之后,退出;
如何生成列表:
{1..100}
`seq [起始数 [步进长度]] 结束数`
1,...,100
declare -i SUM=0
integer
-x
写一个脚本:
1、设定变量FILE的值为/etc/passwd
2、依次向/etc/passwd中的每个用户问好,并显示对方的shell,形如:
Hello, root, your shell: /bin/bash
3、统计一共有多少个用户
[root@localhost ~]# !nano
nano hello.sh
[root@localhost ~]# cat hello.sh
#!/bin/bash
line=`wc -l /etc/passwd | cut -d‘ ‘ -f1`
for i in `seq 1 $line`;do
echo "hello,`head -n $i /etc/passwd | tail -1 | cut -d: -f1`";
done
[root@localhost ~]#
for I in `seq 1 $LINES`; do echo "Hello, `head -n $I /etc/passwd | tail -1 | cut -d: -f1`"; done
写一个脚本:只向默认shell为bash的用户问声好
[root@localhost ~]# cat hello.sh
#!/bin/bash
line=`wc -l /etc/passwd | cut -d‘ ‘ -f1`
for i in `seq 1 $line`;do
user=`head -n $i /etc/passwd | tail -1 | cut -d: -f1`
shell=`head -n $i /etc/passwd | tail -1 | cut -d: -f7`
if [ $shell = /bin/bash ]; then
echo "hello,$user;your shell is $shell";
fi
done
[root@localhost ~]#
写一个脚本:
1、添加10个用户user1到user10,密码同用户名;但要求只有用户不存在的情况下才能添加;
扩展:
接受一个参数:
add: 添加用户user1..user10
del: 删除用户user1..user10
其它:退出
adminusers user1,user2,user3,hello,hi
写一个脚本:
计算100以内所有能被3整除的正整数的和;
取模,取余:%
3%2=1
100%55=45
写一个脚本:
计算100以内所有奇数的和以及所有偶数的和;分别显示之;
let I=$[$I+1]
SUM=$[$SUM+$I]
let SUM+=$I
let I+=1 相当于 let I++
-=
let I-=1 相当于 let I--
++I, --I
*=
/=
%=
写一个脚本,分别显示当前系统上所有默认shell为bash的用户和默认shell为/sbin/nologin的用户,并统计各类shell下的用户总数。显示结果形如:
BASH,3users,they are:
root,redhat,gentoo
NOLOGIN, 2users, they are:
bin,ftp
#!/bin/bash
#
NUMBASH=`grep "bash$" /etc/passwd | wc -l`
BASHUSERS=`grep "bash$" /etc/passwd | cut -d: -f1`
BASHUSERS=`echo $BASHUSERS | sed ‘s@[[:space:]]@,@g‘`
echo "BASH, $NUMBASH users, they are:"
echo "$BASHUSERS
原文:http://f1yinsky.blog.51cto.com/12568071/1899318