Shell:. Day04 notes

grep with regular expression:
1, grep program
 text of the trio processing the Linux - - Sed awk grep
 grep: line of text filtering tools
 sed: text line editor (stream editor)
 awk: report generator (do formatting text output)

 grep
  contains three commands: grep egrep fgrep, which are used for line mode (pattern) matching
  egrep = grep - E // use extended regular expressions to match
  fgrep = fast grep // only files matching the wildcard
  * grep default text using regular expressions to match *
 grep usage:
  grep [the Option] ... the PATTERN [filename]
 common grep's options - the Option
  -E support the expansion of the use of regular expressions (ERE) (regexp)
  -P use perl language regular expression search engine (being each language expression engine is not the same, even sed grep awk regexp engine used is not the same
  -i ignore the size of the
  -v option anti
  -o output only content that matches (the default output is matched to the line)
  --color = auto syntax coloring
  -n display line numbers
 
 PATTERN-- regular expression
  functions: through some special characters to represent a class character, and then to the front of the command to execute; if you use special characters meaning in itself, it needs \ translates;

  Review: file wildcard (globbing)
   * [] [^]?
  1, character match
   . Any one character?
   Any of the [] range of a character
   [^] outside the range of any character
   character class: [: digit] Digital [: alnum:] letters and numbers [: alpha:] letter [: lower:] [: upper :] [; space:] space [: punct:]
  2, the number of match
   * matches the preceding character zero to many times
   ? Matches the preceding character zero to 1
   + Matches the preceding character once to many times
   \ {m \} matches the preceding character m times
   \ {m, n \} matches the preceding character m to n times
   \ {0, n \} matches the preceding character zero to n times
   \ {m, \} the preceding matching character least m

  3, the position of the anchoring
   ^ anchors the first line
   $ anchor end of a line
   \ b anchor the first word and the anchoring suffix
   \> anchor ending
   <\ anchoring the first word
  4, packet
   \ (\) Example: \ (abc \) ABCABCABC ... *
   ** grouping characteristics: By default, Linux system is a packet specified variable, variable representation of \ 1 \ 2 \ 3 ...
   example: \ (ab + \ (xy \) * \), wherein \ =. 1 + ab & \ (XY \) *, \ 2 = XY
    abbbbxyxyxyabxy
 
 
if statement :
 Use format if conditional statement:
 1, a single branch statements
  if condition; then
   execute the statement
  Fi
 2, double branch statements
  if condition; then
   execute statement 1
  the else
   execute statements 2
  Fi
 . 3, multi-branch statements
  if condition; then
   execute the statement . 1
  elif; the then
   execute the statement 2
  elif; the then
   executing the statement. 3
  the else
   execute the statement. 4
  Fi
 Exit codes:
  Exit
  under certain conditions to determine if the condition is not satisfied, we have to manually exit the program, otherwise the code behind can not be executed;
  after the completion of the implementation of the code is correct, we have developed the correct exit code exit 0;
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."
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 for the average user,"
3, to determine whether a file exists
 #! / bin / bash
 # judgment document whether there
 iF [-LT-$ #. 1]; the then
  echo. "At Least One argument"
  Exit. 1
 Fi
 IF [-e $. 1]; the then
  echo "present"
 the else
  echo "absent"
 IF
4, to determine whether a user's default shell bash whether the program, if there is, it shows how many of these users on the current system, otherwise showed no such users; [and show those users is bash]
 # /! bin / bash
 # determine the user's default shell type
 
 declare -i sum = `grep" bin / bash $ "/ etc / passwd | wc -l`
 IF grep "/ bin / bash $" / etc / passwd &> / dev / null; the then
  echo "there is $ sum users, shell program to / bin / bash"
  grep "/ bin / bash $" / etc / passwd | -d Cut: -f1
  Exit 0
 the else
  echo "no such user"
  Exit 1
 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 line
 ^ [[: space:]] * $
 ! # / bin / bash
 #
 
 B = `grep -n" ^ [[: Space:]] * $ "/ etc / inittab | WC -l`
 C =` grep -n "^ [[: Space:]] * $ "/ the root / ABC | Cut -d: -f1`
 
 IF [$ B -eq 0]; the then
  echo" no blank line "
  Exit. 1
 the else
  echo" blank lines, either blank lines $ C "
  Exit 0
 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
 #
 
 1, for do traversal / etc / the passwd
 for in $ I (CAT / etc / the passwd); // do Sed complete;
  2, each row is determined UID and the GID
  IF [Cut -d `:` Cut - f3 = $ i` - D: -F4 $ i`]; the then
   echo "Good Guy"
   Exit 0
  the else
   echo "Bad Guy"
   Exit. 1
  Fi
 DONE
 
 
7, write a script, given a user to get their password warning period ($ W); then determine the user last modification time from the password if today is less than the warning period;
 / etc / passwd / etc / Shadow / etc / group / etc / gshadow
 user (X) cipher group password
 #! / bin / bash
 #
 
8, to determine the command history in the history commands total entries is greater than 1000, if greater than, "some command will gone" is displayed, otherwise OK
 ! # / bin / the bash
 #
 S = `History | awk '{}. 1 Print $' | tail -1`
 
 IF [-gt S $ 1000]; the then
  echo" Command Will some Gone "
  Exit 0
 the else
  echo" 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
 #
 # the INPUT ()
 the Read 5 -t -p ( "Please enter a file:") filename // -t latency
 echo # default to wrap
 if [ -n $filename ];then
  echo "eg. /etc/fstab"
  exit 8
 fi
 IF [-f $ filename]; the then
  echo "$ filename is an ordinary file"
  Exit 0
 elif [-d $ filename]; the then
  echo "$ filename directory is a file"
  Exit 0
 the else
  echo "unrecognized"
  Exit. 1
 Fi
 -----------------------------------------------------
 Into a case statement;
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 display "No SUCH File"
 #! / Bin / bash
 #
 
 the Read -p "Enter a file path: "filename
 
 IF [-e $ filename]; the then
  echo" the OK "
 the else
  echo" No SUCH file "
 fi
11, write a script, the script to pass two parameters, displaying both the two and the sum of the product of
 #! / Bin / the bash
 #
 echo $ [$ + $ 2. 1]
 echo $ [$ * $ 2. 1]

Guess you like

Origin www.cnblogs.com/why098/p/11353418.html