Chapter 6 of linuxshell programming guide ------ shell function 1

Source: Internet
Author: User
When selecting from the menu, you must enter the enter key after selection or Display & ldquo; pressanykeytocontinue & rdquo ;. You can use the dd command to solve the problem of not typing a carriage return to send the strike key sequence. Dd commands are often used to extract tapes or general tapes.

When selecting a key from the menu, you must enter the enter key or press any key to continue ". You can use the dd command to solve the problem of not typing a carriage return to send the strike key sequence.

Dd commands are often used to question or convert data problems in tape or general tape decompression tasks, but can also be used to create fixed-length files. The following is a 1 MB file.
[Root @ localhost huangcd] # dd if =/dev/zero of = myfile11 count = 512 bs = 2048
512 + 0 records in
512 + 0 records out
1048576 bytes (1.0 MB) copied, 0.0106073 seconds, 98.9 MB/s
[Root @ localhost huangcd] # ls-al | grep "myfile11"
-Rw-r -- 1 root 1048576 12-11 12:32 myfile11

When copying files, it is common to test whether the directory exists. The following function tests whether the file name passed to the function is a directory. The result can be tested using the I f statement because the function returns a value of success or failure.
[Root @ localhost huangcd] # cat is_it_directory
#! /Bin/bash
Is_it_directory (){
If [$ #-lt1]
Then
Echo "is_it_directory: I need an angument"
Return 1
Fi
_ DIRECTORY_NAME = $1
If [! -D $ _ DIRECTORY_NAME]
Then
Return 1
Else
Return 0
Fi
}
Echo-n "enter destination directory :"
Read DIREC
If is_it_directory $ DIREC
Then
Echo "$ DIREC does not exist, create it now? [Y. n]"
Fi

If Statement Summary:

Summary:

1. if must be followed by a space

2. [] there must be a space at the beginning and end of the brackets!

3. [$ 1-eq "root"] there is no space between $1 and-eq and "root" in brackets!

In the v I editor, you can list the line numbers for debugging. However, if you print several files with a line number, you must use the nl command. The following function uses the n l Command to list the object row number. The original file does not contain a row number.

To call the n u m B e r f I l e function, you can use a file name as a parameter, or provide a file name in s h e l, for example:
$ Number_file myfile

You can also write or use the following code in the script:
Number_file $1

Number_file ()
{
_ FILENAME = $1
If [$ #-ne 1]
Then
Echo "number_file: I need a filename to number"
Return 1
Fi
Loop = 1
While read LINE
Do
Echo "$ loop: $ LINE"
Loop = 'expr $ loop + 1'
Done <$ _ FILENAME
}
[Root @ localhost huangcd] # number_file/home/huangcd/OK .txt
1: AC456
2: acs0169
3: AC9967
4: AC88345

There are two different methods to use functions: call functions from the original file and use functions in the script.

To use the functions in the script, you only need to ensure that the functions are defined before they are called.

Now you can write a script to call the function in f u n c t I o n s. s h. Note that the function file is located in the following command format in the script:
.\

This method will not create another s h e l, and all functions are executed under the current s h e l.

[Root @ localhost huangcd] # cat functions. sh
#! /Bin/bash
Func1 ()
{
Echo "func1 () is used"
}
[Root @ localhost huangcd] # cat direc_check
#! /Bin/bash
./Home/huangcd/functions. sh
Func1
Echo "func1 is used in direc_check"
[Root @ localhost huangcd] # sh direc_check
Func1 () is used
Func1 is used in direc_check

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.