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
line = `wc -l /etc/inittab | cut -d '' -f 1`
if [$line -gt 100]; then echo "/etc/inittab is a big file" else echo"/etc/inittab is a 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."
! # / bin / the bash the userid = ` ID -u $ . 1 ` IF [$ the userid -eq 0 ]; the then echo " Admin User " the else echo " normal user " Fi
3, to determine whether a file exists
! # / bin / the bash #if [-e $. 1]; # $. 1 the then pass a parameter, this parameter is determined echo " file exists " the else echo "file does not exist " 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 bash=`cat /etc/passwd | grep "bash$" | wc -l` if [ $bash -eq 0 ]; then echo "我们没有bash程序" else echo "We have $bash user,This number is $bash." 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 File = ` CAT / etc / inittab | Cut -d ' # ' -f2 | grep " ^ $ " | WC - l` IF [$ File -eq 0 ]; the then echo " This file is not empty line " the else echo " this document empty lines, empty behavior file $ " 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 pid='id -u $1' uid='id -p $2' if [ $pid -eq $uid ];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` # Remove password expiration warning time S =` DATE +% s` # specify the system after the first year to now time (seconds) T = ` expr $ S / 86400 ` # year now designated system number of elapsed days (day) L = ` grep " ^ ABC " / etc / Shadow | Cut -d: - f5` # password removed the expiration time N = ` grep " ^ abc " / etc / Shadow | Cut -d: - f3` # remove the password last modified time SY$ = [L $ - $ [T-$ $ N]] # is also calculated remaining number of days expired (SY) IF [-LT-SY $ - $ W is]; the then echo " Worning " the else echo " the 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 his=`history | wc -l` if [ $his -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"
! # / bin / bash File = ` LS the -l / etc / $ 1 | Cut -b 1 ` IF [$ File == ' - ' ]; the then echo " $ 1 for general file " elif [$ File == ' d ' ]; the then echo " $ file directory. 1 " the else echo " unrecognized " 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 file=`ll /etc/ $1 | wc -l` if [ $file -eq = 0 ] echo "ok" else echo "No such file"
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]"