shell_if practice

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

 

1    line=`wc -l /etc/inittab|cut -d'' -f1`
2 
3     if [ $line -gt 100];then
4         echo"/etc/inittab is a big file"
5     else
6         echo"/etc/inittab is a small file"
7     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."

1      two methods:
 2      1 , the whoami (displays the current user name)
 . 3          
. 4          IF [ ` the whoami ` `= root`]; the then 
. 5              echo " of the user is an administrator user " 
. 6          the else 
. 7              echo " the ordinary user user " 
. 8  
. 9      2 , ID - U (the current user's UID)
 10  
. 11          IF [` ID -u` -eq 0 ]; the then 
12 is              echo " the user is a user administrator " 
13 is          the else 
14              echo "The user is a normal user " 
15  
16  
. 17  . 3 , it is determined whether a file exists
 18 is  
. 19      IF [-f " file name " ]; the then 
20 is          echo " file exists " 
21 is      the else 
22 is          echo " file does not exist " 
23 is  
24 # /! bin / the bash
 25  # determines whether there is file
 26 is  
27  iF [-LT-$ # . 1 ]; the then 
28      echo " At Least One argument " 
29      Exit . 1 
30  Fi 
31 is 
32 if[-e $1];then
33     echo"cunzai"
34 else
35     echo"bucunzai"
36 
37 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]

. 1 ! # / Bin / the bash
 2  default shell type determines the user #
 . 3  
. 4 DECLARE -i SUM = ` grep  " / bin / the bash " / etc / the passwd | WC - l`
 . 5  
. 6  IF  grep  " / bin / the bash $ " / etc / passwd &> / dev / null ; the then 
7      echo " there is $ sum users, shell program to / bin / bash " 
8      grep  " / bin / bash $ " / etc / passwd | Cut -d: - f1
. 9      Exit 0 
10  the else 
. 11      echo " No such user " 
12 is      Exit . 1 
13 is  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
    

. 1 ! # / Bin / the bash
 2  #
 . 3  
. 4 B = ` grep -n " ^ [[: Space:]] * $ " / etc / inittab | WC - l`
 . 5 C =` grep -n " ^ [[: Space:]] * $ " / etc / inittab | Cut -d: - f1`
 . 6  
. 7  IF [$ B -eq 0 ]; the then 
. 8      echo " no blank line " 
. 9  the else 
10      echo " blank lines, either blank $ C line "    

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"

 1    #!/bin/bish
 2     for i in $(cat /etc/passwd);do
 3 
 4         if [ `cut -d: -f3 $i` -eq `cut -d: -f4 $i` ];then
 5             echo"good guy"
 6             exit 0
 7         else
 8             echo"bad guy"
 9             exit 1
10         fi
11 
12     done

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;

. 1 # / etc / the passwd     / etc / Shadow / etc / Group / etc / gshadow
 2  # User password group group password
 . 3  
. 4      ! # / Bin / the bash
 . 5      
. 6      W is = ` grep  " ABC " / etc / Shadow | Cut - D: - f6`
 . 7      S = Data + `% s`
 . 8      T =` expr $ S / 86400 `
 . 9      L =` grep  " ^ ABC " / etc / Shadow | Cut -d: - f5`
 10      N = ` grep "^abc" /etc/shadow | cut -d: -f3`
11     SY=$[$L-$[$T-$N]]
12 
13     if [$SY -lt -$W];then
14         echo"worning"
15     else
16         echo"ok"
17     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

1 #!/bin/bash
2 
3     s=`history|awk'{print $1}'|tail -1`
4     if [ $s -gt 1000 ];then
5         echo"gone"
6         exit 0
7     else
8         echo"ok"
9     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"
  

. 1   ! # / Bin / the bash
 2      
. 3      Read -t . 5 -p " Please enter a file: " filename   // -t latency 
. 4      echo   # default for wrapping
 . 5  
. 6      IF [the -Z $ filename]; the then 
. 7          echo " eg./etc/fstab " 
. 8          Exit . 8 
. 9      Fi 
10  
. 11      IF [-f $ filename]; the then 
12 is          echo " $ filename is an ordinary file " 
13 is          Exit 0 
14      elif[-d $ filename]; the then 
15          echo " $ filename directory file is a " 
16          Exit 0 
. 17      the else 
18 is          echo " not recognized " 
. 19          Exit . 1 
20 is      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"

. 1   ! # / Bin / the bash
 2      #
 . 3      
. 4      Read -p " Please enter a file path: " filename
 . 5      
. 6      IF [-e $ filename]; the then 
. 7          echo " OK " 
. 8      the else 
. 9            echo " meiyouwenjian " 
10      Fi

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

1   #!/bin/bash
2     #
3 
4     echo $[$1+$2]
5     echo $[$1*$2]

 



















Guess you like

Origin www.cnblogs.com/TheNeverLemon/p/11353644.html