shell --Day 5

if statement:

 

Statement format:

1, a single branch statement:

                      if conditions; then

                          Execute the statement

                      be

                      Only condition is true, execution statement will go back;

2, double branching statements:

                       if conditions; then

                            Statement 1 is executed

                       else

                            Execute the statement 2

                        be

3, multi-branch statement:

                         if condition 1; then

                             Statement 1 is executed

                         elif Condition 2; then

                              Execute the statement 2                      

                         elif condition 3; then

 

                              3 execution statement

                          ...
                          the else
                               executes the statement the n-
                           fi

Experimental part:

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."

 

[root@lsl ~]vi 2.txt
#!/bin/bash
a=` wc -l /etc/inittab|cut -d ' ' -f1`
if [ $a -gt 100 ];then
echo '/etc/inittab is a big file.'
else
echo '/etc/inittab is small file.'
fi

 

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."

[root@lsl ~]vi 6.txt
#!/bin.bash
#
read -p "please input a user:" user
if id -u $1 &> /dev/null;then a=$(id -u $1) if [ $a -eq 0 ];then echo "$1 is a supper user" elif [ $a -ge 1 -a $a -lt 500 ];then echo "$1 is a system user" else echo "$1 is a ordinary user" fi else echo "$1 user not exist" fi

 

3, to determine whether a file exists

[root@lsl ~]# vi 7.txt
#!/bin/bash
#
read -p "please input a file:" file
if [ -e $1];then if [ -f $1];then echo "file exists" else echo "$1 is exists but not a file" fi else echo "$1 does not exists" f

 

4, to determine whether a user's default shell bash whether the program, if there are, there are more of these users is displayed on the current system, otherwise showed no such users

[root@lsl~]vi 5.txt
#!/bin/bash
#
grep "\<nologin\>" /etc/passwd &> /dev/null

if [ $? -eq 0 ];then
     echo "cunzai user"
     echo "nologin user such as:"
     grep "\<nologin\>" /etc/passwd | cut -d":" -f1
     echo "user total"
     grep "\<nologin\>" /etc/passwd | wc -l
else
     echo "shell user not exist"
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

[root@lsl~]vi 4.txt
#!/bin/bash
#
if [ -f $1 ];then
   grep "^[[:space:]]*$" /tmp/inittab.bak &> /dev/null
   if [ $? -eq 0 ];then
         echo "space have:"
         grep -n "^[[:space:]]*$" /tmp/inittab.bak | cut -d":" -f1
         exit 0
   else
         echo "no such:"
         exit 1
   fi

else
         echo "file not exits,or not a file!"
         exit 2
fi

 

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

[root@lsl ~]# vi 8.txt  
#!/bin/bash
#
read -p "please input a user:" user ERNAME
=user1 USERID=`id -u $USERNAME` GROUPID=`id -g $USERNAME ` if [ $USERID -eq $GROUPID ];then echo "good boy" else echo "bad boy" 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

[root@lsl ~]# vi 9.txt  
#!/bin/bash
#
read -p "please input a user:" user W
=`grep "student" /etc/shadow | cut -d: -f6` S=`date +%s` T=`expr $S/86400` L=`grep "^student" /etc/shadow | cut -d: -f5` N=`grep "^student" /etc/shadow | cut -d: -f3` SY=$[$L-$[$T-$N]] if [ $SY -lt $W ]; then echo 'Warning' 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

[root@lsl ~]# vi b.txt
#!/bin/bash
#
num=`history | wc -l`
if [ $num -gt 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"

[root@lsl ~]# vi c.txt
#!/bin/bash
#
read -p "please input a file:" file
if [ -f $file ];then
   echo "Ordinary file"
elif [ -d $file ];then
   echo "Directory file"
else
   echo "can not Distinguish"
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"

[root@lsl ~]# vi d.txt
#!/bin/bash
#
read -p "please input a pash:" route
if [ -f $route ];then
   echo "ok"
else
   echo "No such file"
fi

 

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

[root@lsl ~]# vi e.txt  
#!/bin/bash
#
read -p "plesae input num1:" num1
read -p "please input num2:" num2
sum=$[$num1+$num2]
product=$[$num1*$num2]
      echo "$num1+$num2=$sum"
      echo "$num1"x"$num2=$product"

                       

Guess you like

Origin www.cnblogs.com/Blockblogs/p/11556908.html