linux learning 19 shell script programming foundation base -bash script and configuration file

A, shell scripting

  Category 1, programming languages, depending on the operating mode

    a, compile and run: Source -> compiler (Compiler) -> file

      C Language:

    B, interpretive runtime: Source -> interpreter starts running, interpreted by an interpreter running side edge; i.e., the source code itself does not run, but the startup process of an interpreter, the entire contents of the source code as an interpreter parameters. Because it is a side edge explanation therefore run during operation than the speed of compilation will be worse.

    c, whether it is the middle of the compiler or interpreter always need another program that full participation in the operation, which is what we call a translator, he needs us to be able to identify the code into machine code machine can recognize .

  2, according to the programming process to achieve its function is to call the library or call an external program file:

    a, shell scripting:

      Commands and programming using assembly system programming

    b, complete program:

      Use programming library or programming components

  3, are classified according to the programming model: procedural programming languages, object-oriented programming language,

    Program instructions = data +

      Process formula: instruction code to organize the center, to the data service code.

        Execution order.

        Choose Execute

        Cycle execution

        C,bash

      Object of formula: Center to organize the code, instructions about the data to organize data.

        Class (class): instantiating the object, method;

        Representatives: java, C ++, Python

  4, shell scripting: procedural programming, operation explanation depending on the running external programs files;

Second, how to write shell scripts

  1, the first line of the script file, the top grid: given the shebang, interpreter pathway for indicating the interpreted script interpreter of the current file.

    a, common interpreter

      #!/bin/bash

      #!/usr/bin/python

      #!/usr/bin/perl

    b, common text editor: nano

      Line editor: sed

      Full-screen editor: nano, vi, vim

[root@node1 ~]# nano myfirst.sh
[root@node1 ~]# cat myfirst.sh 
#!/bin/bash
useradd user3
echo "user3"|passwd --stdin user3
mktemp -d /tmp/test.xxxx

[root@node1 ~]# chmod +x myfirst.sh 
[root@node1 ~]# ./myfirst.sh 
Changing password for user user3.
passwd: all authentication tokens updated successfully.
mktemp: too few X's in template ‘/tmp/test.xxxx’

  2, what is the shell script?

    Command accumulation

    But many commands are not idempotent, program logic needed to determine the operating conditions is satisfied, to avoid errors in its operation.

  3, run the script

    a, given execute permissions, and direct file to run this program

      chmod +x /PATH/TO/SCRIPT_FILE

      /PATH/TO/SCRIPT_FILE

    B, run directly interpreter, script command line arguments passed to the interpreter program

      bash /PATH/TO/SCRIPT_FILE

    c, Exercise 1: Write a script to achieve the following functions:

      (1), show all begin with an uppercase or lowercase p p file or directory in the / etc directory itself

[root@node1 ~]# ls -d /etc/[pP]*
/etc/pam.d   /etc/passwd-  /etc/pkcs11  /etc/plymouth  /etc/polkit-1  /etc/postfix  /etc/prelink.conf.d  /etc/profile    /etc/protocols
/etc/passwd  /etc/pinforc  /etc/pki     /etc/pm        /etc/popt.d    /etc/ppp      /etc/printcap        /etc/profile.d  /etc/python

      (2) to display all files or directories under the / var directory itself, and the results show the lowercase to uppercase converted;

[var to the root @ node1] # the ls the -d / var / * | tr 'az' "AZ" 
/ VAR / ACCOUNT 
/ VAR / ADM 
/ VAR / CACHE 
/ VAR / CRASH 
/ VAR / DB 
/ VAR / even EMPTY 
/ VAR / GAMES 
/ VAR / GOPHER 
/ VAR / KERBEROS 
/ VAR / LIB 
/ VAR / LOCAL 
/ VAR / LOCK 
/ VAR / LOG 
/ VAR / to MAIL 
/ VAR / the NIS 
/ VAR / OPT 
/ VAR / PRESERVE 
/ VAR / RUN 
/ VAR / SPOOL 
/ VAR / TMP 
/ VAR / the WWW 
/ VAR / YP

      (3), creates a temporary file /tmp/myfile.XXXX

[root@node1 var]# mktemp -d /tmp/myfile.XXXX
/tmp/myfile.Z19s

46:14

Guess you like

Origin www.cnblogs.com/Presley-lpc/p/12085132.html