Shell (3) + File Operations

Source: Internet
Author: User

2.6.6 Command Execution

1 $ (command): capture the execution result of a command and use it in the shell script program
Echo the date is $ (date) // The result is the output of the command, which is a string output.
Set $ (date)
Echo the month is $2 // output the string in $ (date) through the Location Parameter

2 arithmetic extension $ ((...))
X = $ ($ x + 1) // faster than expr

3. Parameter Extension
Unset foo
Echo $ {FOO:-bar}: When foo is empty, set it to the bar value. // bar
Foo = fud
Echo $ {FOO:-bar} // fud
Foo =/usr/bin/X11/startx
Echo $ {Foo # */} // usr/bin/X11/startx
Echo $ {Foo # *} // usr/bin/X11/startx
$ {Param # Word} // Delete the smallest part that matches the word from the param header, and then return the remaining part, not including at most/
Echo $ {Foo # */} // startx deletes the largest part that matches the word from the param header, and returns the remaining part, at least excluding/
Bar =/usr/local/etc/local/networks
Echo $ {bar % local *} // usr/local/etc/
$ {Param % word}: Delete the minimum part that matches the word from the end of the Param, and return
Echo $ {bar % local *} // usr/
Echo $ {# bar} // 29 Param Length

Echo $ {FOO: = bar} // If foo is empty, assign bar to foo bar.
Echo $ {FOO :? Bar}

2.6.7 here document
A special method for passing input to a command in a shell script program
Cat <! Funky! // <This is the start mark of the here document! Funky! And end with it
Hello
This is a here
Document
! Funky!

3. File Operations
3.1 Linux File structure
The file structure provides a simple and unified interface for OS services and devices. In Linux, everything is a file, and even hardware devices are usually mapped to files in Linux.
3.1.2 files and devices
1/dev/console: system console. The error message and diagnosis information are sent to this device.
2/dev/tty: if a process is interrupted,/dev/tty is the alias of this control terminal (keyboard, display ).
3/dev/null: all output data written to this device is discarded. When you read this file, the system returns a file with the ending sign of love you, redirecting unwanted output to/dev/null.
Devices can be divided into character devices and Block devices (supporting instant file system access settings, hard disks): The difference is whether to read and write a whole block at a time during access settings.

3.2 system calls and Device Drivers
Functions that can access and control files and devices are called by the system. They are provided directly by Linux and are interfaces to the OS itself.
The underlying functions used to access device drivers (system calls) include:
Open: open a file active device
Read:
Write
Close
IOCTL: transmits control information to the device driver.
The standard function library provides high-level interfaces for devices and disk files.
3.4 access to underlying files
3.4.1 write

# Include <unistd. h>
Size_t write (INT Fildes, const void * Buf, size_t nbytes );
Write the first nbytes bytes of the Buf in the cache to the file associated with the file descriptor Fildes, and return the actual number of bytes written. 0 indicates that no data is written.-1 indicates that an error occurs in write.

If (write (1, "Here is some data/N", 18 ))! = 20)
Write (2, "A write error has occurred on file descripter./N", 46 );

The execution result is:
$ Gcc-o sim simple_write.c
$./Sim
Here is some data
A write error has occurred on file descripter.
3.4.2 Read system call
# Include <unistd. h>
Size_t read (INT Fildes, void * Buf, size_t nbytes)
// Read nbytes bytes from the file associated with Fildes to the Buf data Zone
Returns the actual number of bytes read.
0: reached the end of the file
-1: Read call error

Read (0,...): input from the keyboard
Write (1,...): output to the display

Interestingly, you can not only use the standard input on the keyboard, but also use the file to redirect the input. Then the program executes the write (1,...) to output the text.
However,./RD <simple_read.c can be used, while GDB./RD <simple_read.c and gdb rd <simple_read.c cannot.

3.4.3 Open System Call
# Include <fcntl. h>
Int open (const char * path, int oflags );
Int open (const char * path, int oflage, mode_t Mode)

Open creates an access path to the Active Directory of the file. If the operation succeeds, a file descriptor is returned. Read and Write call this descriptor to open the file and perform operations. Even if two programs open the same file, they will get different file descriptors. If all write operations are performed on the files, write each of them separately, and then continue to write at the last exit to overwrite each other.
File Access Mode: Required
O_rdonly
O_wronly
O_rdwr
Optional:
O_append: append to the end of the file
O_trunc: the object length is set to 0 and the existing content is discarded.
O_creat: Creates a file according to the access mode given by mode.
O_excl: used together with o_create
-1 is returned for open failure. errno is used to specify the cause of the error.

Open ("myfile", o_creat, s_irusr | s_ixoth)

$ LS-ls myfile
0-r ------- x 1 yaozhangjun 0 2009-10-22 23:03 myfile
But the open file descriptor seems to be 0.

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.