Section 5: About program design
5.1. Write a narrative program: .bat gear
You may use. Bat to be an abbreviation for a long string (I often do this). This thing can be
Set Alias in PROFILE or .Profile. However, once .bat file is too complicated, then you
I will like the narrative language provided by Shell: It is almost as strong as QBasic.
Large and easy to use. It can use variables, you can have like while, for, case if ... .. Else,
The syntax structure; it has other advantages - for example, it can be used as a substitute for a program.
To write a script - just like writing. BAT file under DOS - as long as you write an ASCII file,
Contains the instruction you want, then store, then use the following command to let it be executed:
$ CHGMOD U X
When you want to execute, you can type it.
Here is a small warning: the system's internal editor is called VI, it has a little difficult to use, I think you should also
Will think that it is a bit annoying. I will not discuss it in this article - I still haven't found the use of it.
Door: P For reference Matt Welsh's "Linux Installation ...", page 109. But you can also use it
Its editor, such as Joe or Emacs under X. Here only explains the VI knowledge you must know.
(At least let you quit: p)
- Add a paragraph can be used to press 'I' in the article;
- - Leave Vi but not store article: Please press ESC to hit: Q!
- - Leave and save: Press ESC, hit: WQ
Writing shell script in Linux is a university asked - it can be almost a book to speak in detail.
This article will not discuss this problem more in depth; however, the following provides a number of examples of useful (hopeful), hope
Ability to make you a preliminary understanding of Shell Script.
EXAMPLE 1: First_Script
#! / bin / sh
# I am a comment
# Don't change the first line - it's got to be there
Echo "Today Is` Date` "
Echo "My Name IS" $ 0
Echo "You Gave Me The Following" $ # "Parameters:" $ *
Echo "First Parameter IS" $ 1
echo "Have you grasped the trick?"
Example 2: 2exe
#! / bin / sh
Echo "MAKING" $ 1 "Executable ..."
CHMOD U X $ 1
EXAMPLE 3: Backup
#! / bin / sh
Echo "Copying Files in ~ / Bak ..."
For Name IN $ *
DO
CP $ {name} ~ / bak
DONE
EXAMPLE 4: FMTA
#! / bin / sh
Echo "I Remind You That Only Root Can Format Disks"
FDFORMAT / DEV / FD0H1440
MKFS -T EXT2 -C / DEV / FD0H1440
echo "Disk formatted."
EXAMPLE 5: MNTA
#! / bin / sh
Echo "I Remind You That Only Root Can Mount Disks"
Mount -T MSDOS / dev / fd0 / mntecho "Don't forget to umount when you've done."
5.2. Write a C program yourself
You should not expect to have GW / QBasic in Linux ?? In UN * X, the system has a language in the system C,
Whether you like or hate it. Of course, there are other languages (Fortran, Pascal, Lisp,
Bacic
But there is no Turbo Pascal ^ Q ^).
Suppose you know C .. If you have been shared by Turbo C or it's brothers under DOS, here
There are two sentences, I am afraid it is not a sunny day: the C compiler under Linux is called GCC, but it doesn't have an IDE environment.
There is no online help system, no integrated decentralizer, .., etc. Only one command column compiler, but powerful
And efficient. The following command can compile you Hello.c:
$ GCC Hello.c
This will generate an execution file of a.out. If you want GCC to make the execution file of other files, type
$ gcc -o hola hello.c
To join a program, you have to add one -L
Library
$ gcc -o mathprog mathprog.c -lm
(-l
/usr/lib/libm.a)
For small prices, this is a good way. But if the process consists of several Source Files, we may
Make will need Make. Suppose we have written a syntax analysis program Parser.c, it #include
Two .h: Parser.h, XY.H. Now, there is a Calc.c that needs to be used in Parser.c.
What should I do??
We can write a file called Makefile, tell the compiler, all Source and Object Files
The relationship between, in this example,
# This is makefile, used to compile calc.c
# Press the
Calc: Calc.o Parser.o
# Calc Depends on Two Object Files: Calc.o and Parser.o
Calc.o: Calc.c Parser.h
# Calc.o Depends on Two Source Files
Parser.o: Parser.c Parser.h xy.h
# Parser.o Depends on Three Source Files
# End of makefile.
Store, then type
$ Make
To compile the program. Or, this file is existing in Calc.mak, then it is necessary
$ Make -f Calc.mak
Of course, please refer to Man Pages for more information.
In addition, some functions can be found in Man Pages, for example
$ Man Printf