Shell exercises

Whether a judgment / etc / inittab files larger than 100 lines, if yes, display the "/ etc / inittab is a big file." Whether the person display "/ etc / inittab IS A Small File."
#! / Bin / the bash
Row = `cat / etc / inittab | wc -l` # Get the number of rows / etc / inittab file
if [$ row -gt 100]; then # determines whether the number of lines greater than 100
echo" / etc / inittab iS a Big file " # if greater than the output of "/ etc / inittab iS a Big File"
the else
echo "/ etc / inittab iS a Small File" # if less than or equal outputs "/ etc / inittab iS a Small File"
Fi
2, given a user to determine what the user is the user if the user is an administrator, it will show "the user is an administrator". otherwise, "the user is a normal user"
#! / bin / bash
the user = `$ 1` # obtain the above mentioned id -u $ 1 the uid
IF [$ user -eq 0]; the then # $ user determines the uid values (administrator of the uid 0) is equal to 0
echo "of the user is an administrator user" # 0 is output if " the user is an administrator user "
elif [$ user -ge 500]; then # $ 1 Analyzing the uid value (ordinary user uid 500+) 500+ whether
echo "that the user is a normal user" # 500+ output if "the ordinary user user "
fi
3, to determine whether a file exists
# / bin / bash!
IF [-f $ 1]; TH # judge $ 1 this file exists
echo" $ 1 file already exists "if the file exists then output" $ 1 file already exists "
the else
echo" $ 1 file does not exist "if the file does not exist, the output" $ 1 file does not exist "
Fi
. 4, determines whether a user's default shell whether the bash program, if any, is displayed on the current system with a plurality of these category of users, otherwise showed no such users; [and show those users is bash]
# / bin / bash!
NUM = `CAT / etc / passwd | grep 'bash $' | WC -l` get bash by default # the number of users
user = `cat / etc / passwd | cut -d": "-f 1` # get user defaults to bash
if [$ num -eq 0]; then # If the number of whether the user is 0
echo "no such user" # 0 output if it is "no such user"
the else
echo "the User" # otherwise displays the user name
fi
5, write a script, given a file, for example: / etc / inittab a, 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!
Row = `CAT / etc / iiittab | grep '^ $' | WC -l` statistics # / etc / inittab number of blank lines file
if [$ row -eq 0]: then # determines there is no blank line file
echo "no blank line" If there is no output "no blank line"
the else
echo "$ row" # If there is output the number of lines
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 " Guy Bad "
# / bin / bash!
uid =` the above mentioned id $ 1 | Cut -d "=" -f 2 | Cut -d "(" -f # 1` given a user $ 1 to get its uid
gid = `the above mentioned id $ 1 | cut -d "=" -f 3 | cut -d "(" -f 1` # 1 $ given a user, obtains its uid
iF [$ $ uid -eq GID]; uid and pid the then determined whether # equal
echo "good guy" # if equal output "Good Guy"
the else
echo "Bad Guy"# If you do not want to and so the output "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;
Note: / etc / shadow file ":" delimiter is divided into:
        Login: encrypted password: Last modified: minimum time interval: maximum time interval: Alarm Time: No Time: Time: Flag
# / bin / bash!
W = `grep" DMM "/ etc / Shadow | Cut - d: -f6` # password expiration warning time taken
S = `date +% s` # specify the system year to the current time (in seconds) elapsed
T =` expr $ S / 86400` # specify the system now passes into the first year number (days)
L = `grep" ^ dmm " / etc / shadow | cut -d: -f5` # password extracted expiration time
N =` grep "^ dmm" / etc / shadow | cut -d: - f3` # remove the last modification time password
SY = $ [$ L - $ [$ T- $ N]] # is also calculated remaining number of days expired (SY)
IF [-LT-SY $ - $ W is]; the then # judge the rest of the expiration time is less than the warning time
echo "Worning"# If less than warn
the else
echo "the OK" # otherwise outputs "OK"
Fi
. 8, if the total entry is determined command history in the history command is greater than 1000, if it exceeds, "some command will gone" is displayed, otherwise the OK
#! / bin / the bash
Row = `history | wc -l` # acquired history of the number of rows
if [$ row -gt 1000]; then # determines whether the number of lines is greater than 1000
echo" Will some Command Gone "# If the output is greater than the" Command Will some Gone "
the else
echo" ok "# otherwise, output" the 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 = `ll $ 1 | cut -b 1` # Get Type $ 1
if [$ file == 'd' ]; then # determines whether the file type is d (d represents a directory file)
echo" $ 1 is a directory file "# If the output "$ 1 is a directory file"
elif [$ file == '-']; the then # determines whether the file type is - (- Representative regular file)
echo "$ 1 is an ordinary file" If the output "$ 1 is the directory file "
the else
echo" $ 1 does not recognize the "# otherwise outputs" $ 1 does not recognize the "
fi
10, to write a script that can accept a parameter (file path) to determine this parameter if it is an existing file on the show" ok ", otherwise display" SUCH File NO "
#! / bin / bash
if [-f $ 1]; then # judge $ 1 if there is
echo "ok" # if the output is "ok" there is
the else
echo "No SUCH File" # Otherwise, output "No SUCH File"
fi
11, write a script to the script passes two parameters, display two sum of both the product of
#! / bin / the bash
sUM = $ (($ 1 + $ 2)) # Get $ 1 and and $ 2
product = $ (($ 1 * $ 2)) # Get $ 1 and product of $ 2
echo $ sum # output and the
echo $ product # outputs the product

Guess you like

Origin www.cnblogs.com/shandong123/p/11319489.html