Linux Programming Learning notes (1-6)

Source: Internet
Author: User
Tags lstat time and date

First, Getting Started

View the environment variables for Linux echo $PATH
Use a colon to split the entry inside the path variable instead of using a semicolon in Windows.

Gcc-o exename codename.cpp Build a exename application by compiling codename.cpp
If you do not write the-o exename parameter, the generated application is a.out
Contains header file-I header file directory
Include Lib Library-L Library directory-L library name

The library name starts with LIB, but is not necessarily the name of the library when it is used.
Like Libcurlxxx.xxx, then write-lcurl when it's included.
The. o target file, typically a program-compiled binary file, is equivalent to the VC's obj file.
Generated with the-c parameter, a code file generates a corresponding target file.
. A stands for the static function library, which is equivalent to the VC lib file.
Static libraries, also known as Archive Files (archive), can be generated through AR and ranlib.
The AR command can add a target file to the end of a static library file.
The ranlib command to update the symbol Index table of a static library.
. So represents the Congratulations function library, equivalent to the VC DLL file.

I mainly use kdevelop as a development tool, and there is not much real direct use of GCC.

Second, shell program design

All we normally use is/bin/bash, the shell.
/bin/sh is a link file that links to/bin/bash.

P17 page set-c equivalent to Set-o noclobber function is unable to overwrite the file, if the file is overwritten, the "Cannot overwrite existing file" prompt appears, and terminates.
If you are recovering, use set +o noclobber to restore the normal state.

> can overwrite the screen input to a file, such as Ll>123.txt
>> can append a screen input to a file, such as Ps>123.txt
< can input the contents of the file to the program, such as More<123.txt in more form on the screen output.
2> can overwrite the error message input to a file, such as Kill 9999999 2>123.err at this point, the file will be empty if used.
2>> can append the error message input to the file.
If you want to output standard output and errors to the same file, 2>&1 such as kill 9999999 >123.txt 2>&1
If you want to discard the input, that is, do not display the screen is not saved, you can write to the Recycle Bin (/dev/null) such as Kill 9999999 >/dev/null 2>&1

| That is, pipelines can implement multiple commands at the same time, and the transfer of data flows between them can be automatically reconciled.

If you run the program in the script, then this is a blocking process, and only the end of the program runs, the next script statement will be executed. If you want to continue the script operation only after starting the program, you can add the & parameter background to run.
(By the way, if the execution of the application gives the parameter is &, then the parameters obtained in the main function are not & this parameter, if the & in the middle, then the parameters are all invalid, the ARGC parameter number is only recorded & the preceding parameters. The first parameter you get in Linux, like Windows, is the execution path for your application. The difference is that Windows is an absolute path, and Linux is just a startup command, such as "./test.exe")

I used to think that./is the command that executes the application and is now known./is to tell the shell to execute the full path of the program, and if you want to execute the application, you need to add the program directory to the environment variable.

$for file * To display the file name and encoding format of the current directory files.

The rules of the script is very annoying, the space requirements are very strict, sometimes a number of spaces do not, sometimes less space is not.
Do not add spaces, otherwise error:
a1= "123" #yes
A1 space = "123" #error
a1= space "123" #a1为空

There are places where spaces must be added, otherwise an error occurs:
If ["$SHELL" = "/bin/bash"];then
If [space "$SHELL" = "/bin/bash" Space];then

[-F "1.txt"]&&{Echo ' & Yes '; exit 1;}
[Space-F ' 1.txt ' space]&&{space echo ' & Yes '; exit 1;} Semicolons must be added


The special variable $# represents the number of custom parameters that are included, or 0 if no custom parameter is defined
$ A indicates the name of the script (in fact, it is not accurate, it should be the command to start the script, such as "./test.sh". If you start with the full path, then the specific path name + script name is not counted in the $ #中.
$ $ represents the first parameter passed in.
$$ The process number of this script.

If the specific if command see P27 page, can not use greater than less than the number is very frustrating.
If the variable is to be compared, add "" double quotation marks.

Other use to check the book again.

Third, file operation

Linux almost everything is a file, you can use the file to open the way the network connection, serial port and other devices.

0 Standard input
1 Standard output
2 standard Error
can use write (1, "", N) or write (2, "", N), read (0,buf,n)

size_t is an unsigned integral type

If the third argument of read is greater than the actual string length, then the other values in memory are read together.

Open returns the file descriptor is unique, if 2 programs open the same file, then you will get 2 different file descriptors, their write location is independent of each other, will cause each other to overwrite.
3 must be established access mode O_rdonly,owronly,o_rdwr, if not written, then the default o_rdonly .
O_append (Append write, This is the default ), O_trunc (the setting file is empty when open), o_creat (according to the third parameter to create a file, if you do not give parameters, then no write permission is read-only file, that is, 500 permissions, but write writable, If you do not have this parameter, you cannot create a new file. If the file already exists, the third argument is not valid), O_EXCL (used with o_creat, if the previous file already exists, the return fails)

creat equivalent to open with no second argument, with the second parameter (o_creat,owronly,o_trunc) only write permission to write from scratch.

It may not be possible to create the file according to the mode parameter because the user mask (umask) will change the result.
If you want to create the required permissions, you can use chmod to change the permissions after the file is created.
Enter Umask to view the values of the system settings. is typically 0022, which restricts write permissions for user groups and other users.

Lseek set file pointer position, 3 parameters Seek_set (offset from file header), Seek_cur (offset from current position), seek_end (offset from end of file)

Stat,fstat,lstat and a series of macro definitions for judging, see P88 page, or man stat
If you open a symbolic connection, stat returns a pointer to the file, Lstat points to the open link.

The standard IO library is basically the same as in Windows and is part of ANSI C.

r+ w+ both have read and write permissions and are also written from the beginning of the file header.

Chmod,fchmod Change file permissions.

Unlink,link is consistent with commands on the command line, creating new hard links.
Symlink, like Ln-s in the command line, creates symbolic links that are equivalent to shortcuts in Windows.

Mkdir,rmdir Creating, deleting directories
CHDIR,GETCWD change and get the current working directory

Scan Catalog Series functions take a look at the example of P103.

Error handling in the Errno.h file, after an error occurs, use errno, which is an integer that displays the error number.
Strerror This error number into a specific description.
or use perror to print out the most recent error content directly.
In the current version this does not need to define a global variable, as long as the Errno.h file is included on the line, as in Windows, get the last error, with thread protection, rest assured.

Fcntl can do a lot of things in the bottom, remember this function, and later in the concrete example slowly study.

Mmap,msync,munmap allows programs to share memory, which can also be used for file processing through virtual memory segments with special permissions.
Feel it to read the structure of the file is very convenient, you can see the example of P111, should be used later.

In addition, if you delete the file manually after using open or fopen, both write and Fwrite will return to success, but the actual failure is true.

IV. Linux Environment

The parameters passed by the program are mentioned earlier:
If the execution of the application gives the parameter is &, then the parameters obtained in the main function are not & this parameter, if the & in the middle, then the parameters are all invalid, argc the number of parameters will only record & the preceding parameters.
The first parameter you get in Linux, like Windows, is the execution path for your application. The difference is that Windows is an absolute path, and Linux is just a startup command, such as "./test.exe"

Getopt and Getopt_long can be used to parse input parameters.

Getenv,outenv is used to get and add environment variables, note that this new only affects itself and does not go back to the parent process, that is, the shell.

extern char **environ (an array of null-terminated strings) can get all the environment variables.

The time and date are basically the same as on Windows.
Strftime uses the format conversion control to see the P126 page.

Temporary files return a unique identifier with Tmpnam (Nam is not name), but if multiple processes may be the same, it is best to open a file directly with Tmpfile. Note that when this file stream pointer is closed, the file is automatically deleted.
Note that the input parameter of the Tmpnam is also the return value, so the input parameter needs to be a long enough string variable.

User information and logs and so on need to look at it, and now is not very familiar with Linux system.

Five Terminal

Slightly

Six curses

Slightly


This article is from "Flying Justice Blog" blog, please be sure to keep this source http://xzq2000.blog.51cto.com/2487359/1766844

Linux Programming Learning notes (1-6)

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.