小脚本练习

脚本练习:

传递一个用户名参数给脚本,判断此用户的用户名跟其基本组的组名是否一致,并将结果显示出来。

写一个脚本:

#!/bin/bash

USER=$1

if [ ! $# -eq 1 ];then

echo "please input only one username "

exit 2

fi

if ! id $1 &> /dev/null;then

echo "the user is not exist"

exit 3

fi

if [ id -u $1 -eq id -g $1 ];then

echo "same"

else

echo "different"

fi

判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor id一行中。

如果其生产商为AuthenticAMD,就显示其为AMD公司;

如果其生产商为GenuineIntel,就显示其为Intel公司;

否则,就说其为非主流公司;

#!/bin/bash

CPUFAC=sed -n '/^vendor_id/p' /proc/cpuinfo | cut -d' ' -f2

if [ $CPUFAC == AuthenticAMD ];then

echo "AMD"

elif

[ $CPUFAC == GenuineIntel ];then

echo "Inter"

else

echo"other company"

fi

写一个脚本:

给脚本传递三个整数,判断其中的最大数和最小数,并显示出来。

#!/bin/bash

declare MAXNUM

declare MINNUM

if [ $# -ne 3 ];then

echo "please input three number"

exit 2

fi

if [ $1 -ge $2 ];then

MAXNUM=$1

MINNUM=$2

else

MAXNUM=$2

MINNUM=$1

fi

if [ $MAXNUM -ge $3 ];then

echo "the maxnum is $MAXNUM"

[ $1 -ge $2 ] && echo "is first number" || echo "is second number"

else

MAXNUM=$3

echo "the maxnum is $MAXNUM is third number"

fi

if [ $MINNUM -le $3 ];then

 [ $1 -le $2 ]&& echo "the minnum is $1" || echo "the minnum is $2 "

[ $1 -le $2 ]&& echo "the minnum is first" || echo "the minnum is second"

else

echo "the minnum is $3 is third number "

fi

测试字符串

1 [ $A == $B ] 此时的==前后都要接空白符 不然默认会识别成赋值,

2 [ $A != $B ]

3测试字符串是否为空的单目运算符

-n string 空为真,不为空为真 为空时为假 且测试时 [空格变量空格] 这样的格式进行测试

4测试字符串不为空

-s 不为空为真 ,空为假

4测试字符串是否大于 小于 > < 来表示

for 循环的使用

for 变量 in 列表 ;do

命令行1;

命令行2;

done

1.1生成列表

1.1{1..100}

1.2 seq 起始数 步长 结束数

seq number 表示从1开始到number

seq number1 number2 表示步长为1 起始数为number1 结束数为number2

seq number1 number2 number3 1表示起始数 2表示步长 3表示结束数

1.3 ls /etc/

数据类型的申明 declare

declare -i SUM=0 申明为整数!

declare -x 表示申明为环境变量

写一个脚本:

1、设定变量FILE的值为/etc/passwd

2、依次向/etc/passwd中的每个用户问好,并显示对方的shell,形如:

Hello, root, your shell: /bin/bash

3、统计一共有多少个用户

4、只向默认shell为bash的用户问声好

cat /etc/passwd | cut -d: -f1,7 | head -1 | tail -1 | cut -d: -f2 | sed 's@/.*/@@'

cat /etc/passwd | cut -d: -f1,7 | head -1 | tail -1 | cut -d: -f1

#!/bin/bash

declare I

declare J=0

NUM=cat /etc/passwd | wc -l

for I in seq $NUM ;do

USER=`cat /etc/passwd | cut -d: -f1,7 | head -$I | tail -1 | cut -d: -f1`

USHELL=`cat /etc/passwd | cut -d: -f1,7 | head -$I | tail -1 | cut -d: -f2 | sed 's@/.*/@@'`

echo "HI $USER your shell is $USHELL"

if [ $USHELL == bash ];then

   echo "hi $USER your shell is $USHELL"

  let J=$J+1

fi

done

echo "the users total is $I the bash is $J"

exit

写一个脚本:

1、添加10个用户user1到user10,密码同用户名;但要求只有用户不存在的情况下才能添加;

扩展:

接受一个参数:

add: 添加用户user1..user10

del: 删除用户user1..user10

其它:退出

adminusers user1,user2,user3,hello,hi

!/bin/bash

if [ $1 == "add" ];then

for I in {1..10};do

 if  id user$I &> /dev/null;then

   echo "user$I is exist"

else

   useradd user$I

   echo "user$1" | passwd --stdin user$I &> /dev/null

fi

done

elif [ $1 == "del" ];then

 for I in {1..10};do

 userdel -r user$I

 done

else

echo "please input add or del "

exit 2;

fi

写一个脚本,分别显示当前系统上所有默认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

组合测试条件

-a: 与关系

-o: 或关系

!: 非关系

if [ $# -gt 1 -a $# -le 3 ]

if [ $# -gt 1 ] && [ $# -le 3 ]

猜你喜欢

转载自blog.51cto.com/2858259639/2392552
今日推荐