Usage of shell and make in Linux

Source: Internet
Author: User
Tags what echo


Shell and make in Linux I. What is shell1.1? shell ● user and Linux interface ● command interpreter ● support multiple users ● support complex programming languages ● There are many Shell types, such: csh, tcsh, pdksh, ash, sash, zsh, bash, etc. The default Shell in Linux is bash (Bourne Again Shell ). Shell is the main interface between users and operating systems. Through Shell, you can run multiple applications in the background at the same time and run the programs that need to interact with users on the foreground. By using variables and Flow Control in the sequence of multiple commands, Shell can be used as a complex programming language. 1.2 escape characters and wildcard characters ● metacharacters refers to characters with special meanings in Shell. Example: <>| ;!? * [] $ \"''~ () {}^ # Ls file [1-10]. c # count = 'LS-l | grep '^ d' | wc-l' ● The wildcard character (wildcards) is a subset of escape characters used to find matching file names such :? * [] [-] [!] Www.2cto.com ● use the <Tab> key to automatically complete the configuration. Wildcard character :"? "Wildcard characters:" * "1.3 input/output redirection ● The system defines three standard files: standard input (stdin), standard output (stdout), and standard error input (stderr) ● Pipeline character: "|", which converts the output of the previous command to the input of the next command. ● Filter: it is used to receive standard input. After certain conversions, it is written to standard output. Therefore, the filter is generally placed in the middle of the pipeline operator. When each program starts, there are three file descriptions: STDIN (0), STDOUT (1), STDERR (2 ). Use "<" to change the input, use ">" to change the output, use "2>" to change the error output, and use ">" to append the output to an existing file. Common filters are as follows: expand, sed, awk, fmt, tac, tr, grep, nl, and pr. Multiple commands can be entered into one line and separated by ";" characters. Add "\" after a command line to indicate another line to continue the input. The prompt in the next line becomes ">" printf ("\ t"); 1.4 Shell variable ● Shell variable (Variables) it is used by programs to save data. ● Shell special variables: The system defines some special variables. For example, $ HOME. Use the set command to view the variables defined in the system at that time. Special variables defined by the system include: $ TERM (current terminal type), $ PATH (command search PATH), $ MANPATH (directory on the manual page ), $ HOME (user's main directory), $ LANG (current language), $ PS1, $ PS2 (command prompt), $ SHELL (Current Shell Name ). Enter the variable name = variable value in the command line to define the variable. Use the echo $ variable name to view the variable value. Unset is used to cancel variable definition. # Hello = "123" single quotation marks indicate that all escape characters are ignored, and double quotation marks indicate that escape characters except $ '\ are ignored. Backslash (\) indicates that the next escape character is ignored. Multiple commands can be entered into one line and separated by ";" characters. Add "\" after a command line to indicate entering another line, and the prompt of the next line becomes ">" ● Shell system variable $?: Returned value of the recently executed command; $: process ID of the current process $!: Number of latest backend processes $ #: Number of shell independent variables, $1 indicates the first independent variable ● Shell user variable www.2cto.com # varname = value: Assigned value # readonly varname: Mark Read-Only # export varname: mark removal: variables can be inherited by the quilt process # setenv PATH =/home:/usr:/etc:/bin:/usr/bin: (IN csh) # varname = 'expr $ varname + 1': variable value increases by 1 # x = $ [$ x + 1] # echo $ PATH1.5 Custom User Environment ● when a user logs on to Shell, execute a series of scripts in sequence. ● Use the alias command to define aliases. When Alias 'rm-F' rm logs on to BASH, the user executes a series of scripts in sequence:/etc/profile, $ HOME /. bash_profile (if not, run $ HOME /. bash_login. No, run $ HOME /. profile ). When you log out, $ HOME/. bash_logout is automatically executed. When you log on to a user using redhat, in addition to the two files, $ HOME/is automatically executed /. bashrc, this file will then execute the/etc/bashrc1.6 Conditional Control 1) test file, variable attributes, expression value, or command execution return value. Test-d/usr à [-d/usr] test-f. bashrc à [-f. bashrc] test $ count-gt 0 à [$ count-gt 0] 2) if statement if (expression) then command-list else command-listfi 3) case statement case $ var in pattern1) command-list; pattern2) command-list ;;... Www.2cto.com esac4) logical operators & amp; and | # test-f myfile. c & echo "file found" if test-f myfile. c then echo "file found" fi # test-f myfile. c | echo "file not found" if test! -F myfile. c then echo "file not found" fi1.7 Loop Control 1) for statement for var in word-list do command-listdone for count in 1 2 3 do echo $ I donefor vardo 2) while statement while (expression) do command-list done # greeting = 'Hello world' I = 1 while test $ I-le 100; do case $ I in * 0) echo "**********"> file $ I; *) echo $ I> file $ I ;; esac I = 'expr $ I + 1' done example: Implementation of the append Command: case $ # in1) cat >>1 1; 2) cat <$1> $2 ;;*) Echo 'usage: append [from] to '; esac # chmod + x myappend # myappend file1 file2 1.8 function functionname () {command-list} usage () {echo "usage :..... $1 "} www.2cto.com usage" from... To "NOTE: functions are used just like executable programs, but must be defined first and then used. 1.9 here document refers to specifying the Input Source in the shell script, instead of from the file or standard input, where "<" is the reserved word of here document. # Mail cindy <! @ $ Happy birthdayI love you! @ $1.10 shell internal command: do not generate sub-process 1) eval: in the shell program, use the value of the variable to construct the command A = lsB = '| wc-W' eval $ A $ B 2) exec: After the exec command is executed, no new process is created, it does not return to the current execution process, which is equivalent to the go to statement. # Cat execdemoexec dateecho hello 3) read: reads a row from a standard input device (keyboard), assigns the words to each variable in sequence, and assigns all remaining words to the last variable. # Cat parrot echo "you say: \ c" read what echo "I repeat: $ what" 4) shift: shift the command line parameter to the left, make the variable $ # Minus 1 # cat shiftdemowhile test $ # For the total number of parameters recorded #! = 0do echo $1 $2 $3 shiftdone # shiftdemo a B ca B cb cc 5) wait: wait until all the sub-processes of the current process end. If wait is followed by parameter n, wait until process n ends. # Cat waitdemoecho "This is a new file" (sleep 5; date) & waitecho "the file terminate" execution result: www.2cto.com This is a new file named l 20 10:08:26 BJT 2002-04-20 The file terminate 6) trap: interrupt Processing Command trap Command table interrupt signal table # cat trapfiletrap echo 'this is INT 2' trap echo 'This is INT 3' 3for I in/bin/usrdo echo $ Idone the following program implements scan: scan each subdirectory in the current directory and execute the command submitted by the user: d = 'pwd' for I in * do if test-d $ d/$ I then cd $ d/$ I while Echo "$ I:" trap exit 2 read x do trap: 2; eval $ x; done fi done7) Point command. use in bsh. when a command is executed, no sub-process is created. (Use source in csh) 8) Empty command: do not do anything. 1.11 The program dircmp below the shell program instance tests whether the current directory and the specified directory contain the same number of files if test $ #-ne 1 then echo "Usage: dircmp dirname "exit 1 else if test! -D $1 then echo "\" $1 \ "is not a directory" exit 1 else this = 'LS-l | grep '^-' | wc-l' that = 'LS-l $1 | grep' ^-'| wc-l' if test $ this-ne $ that then echo "Current directory and \" $1 \ "do not match "elseecho" Current directory and \ "$1 \" have same number of files "www.2cto.com IFI # dircmp abc" abc "is not a directory1.12 shell program execution method: 1) $ chmod u + x dircmp $. /dircmp/usr/bin 2) $ sh dircmp 3) $ Sh <dircmp 4) $. dircmp (execute the program by using the dot command without creating sub-processes) % source dircmp (in csh) 2. make2. 1 make large program maintenance tool Makefile or makefile: Tell make to maintain a large program, what to do. Makefile describes the relationship between modules of the program and the actions required to update modules. make automatically maintains these modules according to these instructions. In makefile, the dependency between modules and implementation methods are shown from top to bottom: network. o subrs. o (1) cc-o network. o subrs. o (2) network. o: network. c netdefs. h (3) cc-c network. c (4) subrs. o: subrs. c netdefs. h (5) cc-c subrs. c (6) (3)-(6) can be simplified to: (implicit rule) network. o subrs. o: netdefs. h # make network # make-f makefile 2.2 makefile key points (1) macro (variable) CC = gcc $ (CC) test. c $ @: current target $ <: first prerequisite $ ^: all prerequisites (2) suffix rules (suffix rules ). c. o: $ (CC)-c $ (CFLAGS)-o $ <(3) pattern Rule (pattern rules) %. o: %. c $ (CC)-c $ (CFLAGS)-o $ @ $ <(4) wildcard * objects = *. oobjects: = $ (wildcard *. o) objects: = $ (patsubst %. c, %. o, $ (wildcard *. c) www.2cto.com foo: $ (objects) cc-o foo $ (objects) (5) phony target. PHONY: clean # If this line is not added, if there is a clean file in the current directory, make clean has no action clean: # This is phony target rm *. o temp (6) include filename: Nested makefile, pause the current makefile, and read the specified filename-include filename: Ignore the error when filename cannot be found (7) control statement: ifeq, ifneq author zhengmeifu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.