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

shell练习题

时间:2019-08-14 22:04:39      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:习题   tab   grep   sum   awk   else   print   passwd   大于   

1、判断/etc/inittab文件是否大于100行,如果大于,则显示”/etc/inittab is a big file.”否者显示”/etc/inittab is a small file.”

#!/bin/bash
#判断文件行数是否大于100行
if [ `wc -l /etc/inittab | awk ‘{print $1}‘` -gt 100 ];then
  echo "/etc/inittab is a big file."
else
  echo "/etc/inittab is a samll file."
fi

2、给定一个用户,来判断这个用户是什么用户,如果是管理员用户,则显示“该用户为管理员”,否则显示“该用户为普通用户”

#!/bin/bash
#判断用户类型`
a=`id -u $1`
if [ $a -eq 0 ];then
  echo "这是管理员用户"
else
  echo "普通用户"
fi

3、判断某个文件是否存在

#!/bin/bash
#判断文件是否存在
if [ -e $1 ];then
  echo "存在"
exit 0
else
  echo "不存在"
fi

4、判断当前系统上是否有用户的默认shell程序是否为bash程序,如果有,就显示有多个这类用户,否则就显示没有这类用户;【并且显示出那些用户是bash】

方法一:

#!/bin/bash
#判断用户默认shell程序类型
a=`grep "/bin/bash" /etc/passwd | wc -l`
if [ $a -gt 0 ] ;then
  echo "存在bash用户有 $a 个"
else
  echo "不存在"
fi

方法二:

#!/bin/bash
#判断用户的默认shell程序类型
declare -i sum=`grep "/bin/bash" /etc/passwd | wc -l`
if grep "/bin/bash" /etc/passwd &> /dev/null;then
  echo "存在 $sum 个用户,shell程序为/bin/bash"
  grep "/bin/bash" /etc/passwd | cut -d: -f1
  exit 0
else
  echo "没有这类用户"
exit 1
fi

 

shell练习题

标签:习题   tab   grep   sum   awk   else   print   passwd   大于   

原文地址:https://www.cnblogs.com/ty-wk/p/11354692.html

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