Case shell scripts and applications

1, it is determined / etc / inittab file is greater than 100 lines, if yes, display the "/ etc / inittab is a big file." Whether the person display "/ etc / inittab is a small file."
Code:
! # / Bin / the bash
#
LINES = `-l WC / etc / inittab`
echo LINES
FINLINES LINES =` echo $ | Cut -d '' -f1`
echo $ FINLINES
[$ FINLINES -gt 100] && echo "/ etc / IS A Big inittab 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 normal user"
Code:
# / bin / bash # NAME! = USER1USERID = `id -u $ nAME` [USERID -eq 0] && echo" Admin "|| echo" Common user. " by the if statement:! # / bin / bash
#
USERID =` 1` the above mentioned id -u $
if [ the USERID -eq 0 $]; the then
echo "the Admin"
the else
. "the Common User" echo
Fi

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 of code as follows: # / bin / bash!



















#
SPACELINE = `grep" ^ $ "$ 1 | WC -l`if [$ SPACELINE -eq 0]; the then  
echo" File not have have This Space Line. "
The else  
echo" This have have $ SPACELINE Space File Number The IS $ line.This . 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 "
code is as follows:
# /! bin / bash
#
USERID = `1` the above mentioned id -u $
GRPID the above mentioned 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 the user last modification time from the password if today is less than the warning period;
code is as follows:
! # / Bin / bash
#
W = `grep" abc "/ etc / shadow | cut -d: -f6` # password expiration warning time taken
S =` date +% s` # year to the specified system time (in seconds) elapsed now
T = `expr $ S / 86400 `# specify the first year of the system to the current number of elapsed days (day)
L =` grep "^ abc" / etc / shadow | cut -d: -f5` # remove the password expiration time
N = `grep" ^ abc " / etc / shadow | cut -d: -f3` # remove the password last modified time
SY = $ [$ L - $ [$ T- $ N]] # calculate how many days left in the expired (SY)

IF [-LT-SY $ - $ W is]; the then
echo "Worning"
the else
echo "the OK"
Fi
. 8, determines whether command history command history entries total greater than 1000, if it exceeds, "some command will gone" is displayed, otherwise OK display
code is as follows:
! # / bin / bash
#
HISTLINE = `History | WC -l`

IF [$ HISTLINE -ge 1000]; the then
. "s Some the Command by Will Gone" echo
the else
echo "the ok"
fi
9, given a file, if an ordinary file, on the show, if it is a directory file, also show up, or else displayed "does not recognize"
code is as follows:
! # / bin / the bash
#
[! -e $. 1] IF; the then
echo. "SUCH No File"
Exit. 6
Fi

IF [-f $. 1]; the then
echo "the Common File."
elif [-d $. 1]; the then
echo. "Directory"
the else
echo. "Unknown"
Fi
10, to write a script that accepts a parameter (file path), it is determined If this parameter is an existing file on the show "ok", otherwise, it displays "No such file"
code is as follows:
! # / bin / bash
#
IF [$ # -lt 1]; the then # $ # this parameter indicates the statistical input parameters number;
echo "Usage: ./JudgeFile2.sh AG1 [AG2 ...]"
Exit 7
fi
; IF [-e $ 1] the then
echo. "the OK"
the else
. "No SUCH File" echo
fi
11, a write script, the script to pass two parameters, displaying both the two and the sum of the product of
the following code:
# / bin / the bash!
#
IF [$ # -LT-2]; the then
echo "the 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/wangshilin/p/11313090.html