Day: Shell programming Basics and writing Makefile

Source: Internet
Author: User

One: Shell Programming basics

Shell definition: The shell is a program that acts as an interface between a user and a Linux system. It allows the user to enter commands that need to be executed to the operating system. There are many shells in the shell, and the shell in the Linux system is bash.

Shell programming can be seen as a collection of commands. A script similar to the BAT program in Windows. As an explanatory language.

The first shell program is to determine the size of two numbers.

1 #!/bin/bash  2  3 num1=  4 num2=9  5  6  if test $num 1  -GT    7    8    echo $num 1  9elseten    echo $num 2fi

because it is the first program, the code is interpreted one line at a-#! in the first line Tell the system that the parameter immediately following it is the program used to execute the text file.

The third and fourth rows define two variables num1 and num2 and assign values, so note that there is no trailing space between the variable name and the equal sign. Because the space in the shell script is the end symbol. The six behavior Judgment statement, test for testing, can also be used [],-gt equivalent to >, (and the LT represents less than, GE means that >= le represents < = EQ means equals ne means not equal) $ symbol represents the value of the variable.

If then FI is the structure of the judgment statement. Echo Output Variable Value

Loops in the shell

1 #!/bin/bash23for ((i=0; i<=; i++)) 4 #for in12345 do 6     " Hello World $i " 7 Done

1 #!/bin/bash23 sum=0; 4 5  for inch 1  - ` 6  Do 7     sum= ' expr $sum + $i '8done9 Echo $sum

Functions in the shell

1 #!/bin/bash23hello ()4{5     "  Hello World ' date '"678}9 Hello

1 #!/bin/bash23function add {4     echo $[$1 + $2  5} 6 ' 7 echo   " result is $result "

$ "$" represents the parameters of the function. Or to use the main space. There is a space between add and {in the above code. I didn't notice it when I wrote it, I looked for it for a long time.

The shell knowledge that is needed now is to read the above-mentioned classic shell.

Lao Liu today just probably said the next shell, mainly lead to makefile writing. Lao Liu has written three functions, Main.c bunfly.c hello. The C main function has functions that are defined in BUNFLY.C and hello.c. Makefile function from simple to complex, and constantly use the variable to replace the original value, this process I did not save, so only the last code to paste:

1CC=GCC-C2Ld=GCC3RM=RM-RF4target=Test5obj=main.o bunfly.o hello.o6 ${target}:${obj}7${LD} $^-o [email protected]8%.O:%. C9${CC} $^-o [email protected]Ten #main. O:main.c One# ${CC} $^-o [email protected] A #bunfly. O:bunfly.c -# ${CC} $^-o [email protected] - #hello. O:HELLO.C the# ${CC} $^-o [email protected] - Clean : -${RM} ${obj} ${target}

There are several important points of knowledge, [email protected] indicates that the target file 2.$^ represents all dependent files 3.$< represents the first dependent file. Match mode%.o:%.c can use make clean to use the clean

Makefile also has a time-pinch concept, that is, according to the file change time to determine whether to execute some code, and a little, later Automake can automatically write makefile. That is to say, Makefile will write some, will look on the line.

In the afternoon, because of the basketball game, we talked about the use of typedef. Just read the code below.

1#include <stdio.h>2 3typedefintint32;4typedefint(*PF) (Int32, Int32);5 6 Int32 Ave (Int32 A,int32 B)7 {8     return(A + B)/2 ;9 Ten } One Int32 call (pf P,int32 a,int32 b) A { -     returnP (A, b); -  the } - intMain () - { -Int32 n = Ave (Ten, -); +printf"%d\n", n); -  +PF p =Ave; Aprintf"%d\n", P (Ten, -));  at  -printf"%d\n", Call (P,Ten, -)); -      -}
typedef

homework is to review the use of recursion. Two functions, one is to write the Atoi function recursively, and the other is to implement the decimal binary system with recursion.

1#include <stdio.h>2 3 intn =0;4 5 intMyatoi (Char*p)6 {7     8N. = nTen+ (*p- - );9     if(* (p+1) ==' /')Ten         return; OneMyatoi (+ +)p);  A     returnN; -           - } the intMain () - {     -     Chararr[Ten] = {"2312345"}; -n =Myatoi (arr); +printf"%d\n", n); -}
recursive atoi
1#include <stdio.h>2 intDtox (intN)3 {4     if(N/2==0)5     {6printf"%d"N2);7         return ;8     }9Dtox (n/2);Tenprintf"%d"N2); One  A } - intMain () - { the     intn =Ten; - Dtox (n); -}
recursive conversion into the system

The first one is to look at someone else's code, write it out in a few days, write it again, and the second one has been written before. This time of writing also made a few times, should not.

Day: Shell programming Basics and writing Makefile

Related Article

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.