Linux Advanced Bash Programming (functions and summaries)

Source: Internet
Author: User
Tags logical operators

The definition format of the 1:bash function

[Function] funname [()]{action; [return int;]}


1: Can be defined with function fun () or directly fun (), without any parameters.

2: The function returns a value that can be returned with a return, or, if not, the result of the execution of the last line of the function body as the return value. Return followed by a value of 0-255. After calling the function, the return value is obtained by $?

3: Parameter pass, call function, can pass parameter inside, $n ($1,$2 ...) Gets the parameter value.

2: Practical Practice

Design a function to implement the addition operation.    #!/bin/bashfunwithreturn () {echo "a+b=?" Echo-n "A=" read a echo-n "b=" read B echo-n "$a + $b =" return $ (($a + $b)} #函数调用funWithReturn # get function Execution result return value EC Ho "$?" Script execution: [[email protected] shell]$ Bash add.sha+b=?a=3b=43+4=7 Note: the addition of two variables is implemented here: $ (($a + $b)), $[a+b], ' expr $a + $b ', or $ ( Expr $a + $b)

3.bash Programming Review

Special variables:
$: script name. This variable contains the address, and you can use basename $ to get the script name.
$: First parameter
$2,$3,$4,$5,... One analogy.
$# the number of arguments passed to the script
$* displays all parameters passed to the script as a single string, to ("$ ...")
$$ the ID number of the script run
$! ID number of the last process running in the background
[Email protected] is the same as $*, but is used with quotation marks and returns each parameter in quotation marks. ("$" "$" ...)
$-Displays the current options used by the shell.
$? Displays the launch status of the last command. 0 indicates no error.

logical operators

Corresponding operation integer string
Same-eq =
Different-ne! =
Greater than-GT >
Less than-lt <
Greater than or equal to-ge
Less than or equal to-le
is empty-Z
Not empty-n

File Manipulation logical operators:
-D file----Returns True when file is a directory
-F file----Returns True when file is a normal
-R file----Returns True when file is a read-only
-S file----returns True when the files length is greater than 0 o'clock
-W file----returns True when file is a writable
-X "/bin/ls"----when/bin/ls is an executable file, returns True if the directory is accessible
-E file----Returns True when file exists
-O file----Returns True when the file owner is the current user
-Z File----Returns True when file length is 0 o'clock
-U-----The UID flag of the file is set
-G-----File has the same group ID as the current user
File1-nt file2-----file file1 than file2 update
File1-ot file2-----file file1 older than file2

4:bash Commissioning

It's hard to make mistakes during programming, and sometimes it takes more time to debug a program than to write a program, as is the case with shell programs.

The shell program is debugged primarily by using the Bash command to interpret the program's selections.
The form of calling bash is:
Bash-Select the shell program file name a few common selections are:
-e If a command fails, exit immediately.
-N reads commands but does not execute them.
-U permutation considers an undefined variable as an error.
-V displays them when they are read into the shell input lines.
-X displays commands and their arguments when the command is executed.

This article is from the "Reminder Flower Rain" blog, please make sure to keep this source

Linux Advanced Bash Programming (functions and summaries)

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: 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.