Shell Program Design (3)

Source: Internet
Author: User
Tags echo command

Shell programming (3) shell Syntax Functions

To define a shell function, we only need to write its name, then a pair of empty parentheses, and then put the related statements in a pair of curly braces, as shown below:

Function_name (){

Statements

}

Experiment: simple functions

#! /Bin/bash

Foo (){

Echo "Function foo is executing"

}

Echo "script starting"

Foo

Echo "script ended"

Exit 0;

Output:

Script starting

Function foo is executing

Script ended

When a function is called, the location parameters $ *, $ @, $1, and $2 of the script program are replaced with the function parameters. This is also the way to read the parameters passed to the function. After the function is executed, these parameters are restored to their previous values.

You can use the local keyword to declare local variables in the shell function. local variables are limited to the scope of the function.

Experiment: return a value from the function

Script program:

#! /Bin/sh

Yes_or_no (){

Echo "IS your name $ *? "

While true

Do

Echo-n "Enter yes or no :"

Read x

Case "$ x" in

Y | yes) return 0 ;;

N | no) return 1 ;;

*) Echo "Answer yes of no"

Esac

Done

}

Echo "original parameters are $ *"

If yes_or_no "$1"

Then

Echo "ho $1, nice name"

Else

Echo "Never mind"

Fi

Exit 0

Execution and output results

Root @ ubuntu:/home/chen123/C ++ #./a RickNeil

Original parameters are Rick Neil

IS your name Rick?

Enter yes or no: yes

Ho Rick, nice name

Command

Break command: Jump Out Of The for, while, or until Loop

: Command: Empty command, equivalent to an alias of true

Continue command: Make the for, while, or until loop jump to the next loop to continue execution

. Command: used to execute commands in the Current shell

Echo command: returns a string with a linefeed at the end of the output.

Eval command: Allows parameter evaluation

Exec command: a typical usage is to replace the current shell with a different program. The second method is very rare, that is, to modify the current file descriptor.

Exit n command: run the script program exit code n.

Export command: export the variable used as its parameter to the sub-shell and make it valid in the shell.

Expr command: Evaluate its parameters as an expression

Printf command: format the output

Return command: Make the function return

Set command: set parameter variables for shell

Shift command: shift all parameter variables to the left, so that $2 programming $1, $3 programming $2, and so on. The original value of $1 is discarded.

Trap Command: used to specify the action to be taken after receiving the signal

Unset command: delete a variable or function from the Environment

Find command

Function: Search for files.

A simple example: Use find to search for a file named wish on a Local Machine

$ Find/-name wish-print

/Usr/bin/wish

It takes a long time to execute this command. If linux mounts a file system of a large windows machine, it will search for the mounted directory.

You can use the-mount option to tell the find command not to search for Mounted Directories.

$ Find/-mount-name-wish-print

/Usr/bin/wish

The complete syntax format of the find command is as follows:

Find [path] [options] [tests] [actions]

Grep command

Function: Search for strings in a file.

Syntax:

Grep [options] PATTERN [FILES]

Root @ ubuntu:/home/chen123/C ++ # grep-chello hello. c

1

Output the number of rows matching hello in hello. c.

Here document

The here document starts with a continuous less than sign <, followed by a special character sequence, which will appear again at the end of the document. <Is the shell tag redirection symbol. At this time, it indicates that the command is entered as a here document.

Example:

#! /Bin/sh

Cat <! FUNKY!

Hello

This is a here

Document

! FUNKY!

Execution and Output

Root @ ubuntu:/home/chen123/C ++ #./B

Hello

This is a here

Document

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.