shell script work exercises 8.6

1, it is determined / etc / inittab file is greater than 100 lines, if yes, display the "/ etc / inittab is a big file." Displays whether the person "/ etc / inittab is a small file."

#!/bin/bash
#
LINES=`wc -l /etc/inittab`
echo LINES
FINLINES=`echo $LINES | cut -d‘ ‘ -f1`
echo $FINLINES
[ $FINLINES -gt 100 ] && echo "/etc/inittab is a big file." || echo "/etc/inittab is a small file."

2, given a user, the user is to determine what the user if the user is an administrator, it will show "the user is an administrator". Otherwise, "the user is a regular user."

#!/bin/bash
#
USERID=`id -u $1`
if [ $USERID -eq 0 ]; then
echo "Admin"
else
echo "Common user."
fi

3, to determine whether a file exists

#!/bin/bash
#if [ -e $1 ]; then
echo "File exists."
else
echo "File does not exists."
fi

4, the system determines whether the current user whether the default shell bash program, if so, displaying a plurality of such users, otherwise showed no such users; [users and those showing the bash]

#!/bin/bash
#
BASHLINE=`grep "bash$" /etc/passwd | wc -l`


if [ $BASHLINE -eq 0 ]; then
  echo "We don‘t have /bin/bash user."
else
  echo "We have $BASHLINE user,This number is $BASHLINE."
  echo "grep bash$ /etc/passwd | cut -d‘:‘ -f1"
fi

5, write a script, given a file, for example: / etc / inittab a, to determine whether there is a blank line in the file? b, if so, its line number blank lines displayed, otherwise there is no blank lines

#!/bin/bash
#
SPACELINE=`grep "^$" $1 | wc -l`

if [ $SPACELINE -eq 0 ]; then
  echo "This file not have space line."
else
  echo "This file have $SPACELINE space line.This number is $SPACELINE."
fi

6, write a script, given a user to determine whether the UID and GID, as if, like, it shows that the user is "good guy", otherwise it is "bad guy"

#!/bin/bash
#
USERID=`id -u $1`
GRPID=`id -g $1`

if [ $USERID -eq $GRPID ]; then
echo "good guy."
else
echo "bad guy."
fi

7, write a script, given a user to get their password warning period; then determine whether the user was last modified from time password if today is already less than the warning period;

#!/bin/bash
#
W=`grep "abc" /etc/shadow | cut -d: -f6`
S=`date +%s`
T=`expr $S/86400`
L=`grep "^abc" /etc/shadow | cut -d: -f5`
N=`grep "^abc" /etc/shadow | cut -d: -f3`
SY=$[$L-$[$T-$N]]

if [ $SY -lt -$W ]; then
echo "Worning"
else
echo "OK"
fi

8, to determine the command history command history in total is greater than 1000 entries, if greater than, "some command will gone" is displayed, otherwise OK

#!/bin/bash
#
HISTLINE=`history | wc -l`

if [ $HISTLINE -ge 1000 ]; then
echo "Some command will gone."
else
echo "ok"
fi

9, given a file, if an ordinary file, on the show, if the file is a directory, is also displayed, otherwise it displays "does not recognize"

#!/bin/bash
#
if [ ! -e $1 ]; then
echo "No such file."
exit 6
fi

if [ -f $1 ]; then
echo "Common file."
elif [ -d $1 ]; then
echo "Directory."
else
echo "Unknown."
fi

10, write a script that can accept a parameter (file path) to determine if this parameter is an existing file on the show "ok", otherwise, it displays "No such file"

#!/bin/bash
#
if [ $# -lt 1 ]; then
echo "Usage: ./JudgeFile2.sh AG1 [AG2 ...]"
exit 7
fi
if [ -e $1 ]; then
echo "OK."
else
echo "No such file."
fi

11, write a script, pass two parameters to the script, and display both the product and the two of

#!/bin/bash
#
if [ $# -lt 2 ]; then
echo "Usage: cacl.sh ARG1 ARG2"
exit 8
fi
echo "The sum is:$[$1+$2]"
echo "The prod is:$[$1*$2]"

 

Guess you like

Origin www.cnblogs.com/zwl123456/p/11317319.html