Introduction to Linux programming-bash, ShellScripts

Source: Internet
Author: User
Linux programming-bash, ShellScripts-Linux general technology-Linux programming and kernel information, the following is a detailed description. Introduction to Linux programming-bash and Shell Scripts

It is well known that UNIX is named by gadgets, and many simple gadgets are used to fulfill the original needs.

A lot of software development work, which makes UNIX an ideal system level for many people.

.


Among the many gadgets, Shell Script is the most basic and most? Nbsp; system is FreeBSD"

Echo "Do FreeBSD stuff here ..."

;;

*)

Echo "Unknown system: $ SYSTEM"

Echo "I don't what to do ..."

;;

Esac



Select name [in word;] do list; done


As the name suggests, select is to select an item in word.


Example


#! /Bin/sh


WORD = "a B c"


Select I in $ WORD; do

Case $ I in

A)

Echo "I am"

;;

B)

Echo "I am B"

;;

C)

Echo "I am C"

;;

*)

Break;

;;

Esac

Done


Execution result


[Foxman @ foxman bash] #./select_demo

1)

2) B

3) c

#? 1

I am

1)

2) B

3) c

#? 2

I am B

1)

2) B

3) c

#? 3

I am C

1)

2) B

3) c

#? 4

[Foxman @ foxman bash] #




If list then list [elif list then list]... [else list] fi


Several possible writing methods


If list then

Do something here

Fi


If list then

Do something here

Else

Do something else here

Fi


If list then

Do something here

Elif list then

Do another thing here

Fi


If list then

Do something here

Elif list then

Do another thing here

Else

Do something else here

Fi










Here we need to migrate the Exit Status problem. After I have explained the Exit Status problem, I will come back to continue.

Continue.



While list do list done/until list do list done


Example 1: infinite? Circle writing


#! /Bin/sh


While:; do

Echo "do something here"

Sleep 5

Done


Example 2














Here we need to migrate the Exit Status problem. After I have explained the Exit Status problem, I will come back to continue.

Continue.



[Function] name () {list ;}


Example


Function func (arg1, arg2 ){

Echo $ arg1

Echo $ arg2

Return 1

}


Similar to functions in Pascal.


Bash built-in Instruction Set


. Filename [arguments]

Source filename [arguments]


Read the command from filename and execute it.

You will find many

./Xxxx

And xxxx permission is not executable. In fact, in tcsh

Source/xxxx

Run the same command.

Note that there are spaces on the backend. Filename is a plain text file containing instructions.

Chmod 755 filename.


Example


Filename: my_source


DEV = lo

IP = 127.0.0.1

NETMASK = 255.0.0.0

BROADCAST = 127.20.255


Ifconfig $ IP netmask $ NETMASK broadcast $ BROADCAST dev $ DEV


Next

./Path/my_source

Or

Source my_source


This script can be executed without the need for "chmod 755 my_source"


Alias [name [= value]...]


Name

For example, if you are from the DOS world and are not used to UNIX commands, you can use alias to modify them to conform

Your habits.


Example


Alias ls = "ls -- color"

Alias dir = "ls"

Alias cd... = "cd .."

Alias copy = "cp-f" # dangerous, recommend, cp-I

Alias del = "rm-f" # dangerous, recommend, rm-I

Alias move = "mv-f" # dangerous, recommend, mv-I

Alias md = "mkdir"

Alias rd = "rmdir"


Unalias [-a] [name...]


Unalias cancels the setting of alias. Unalias-a cancels all alias.


Example


Unalias copy


Bg [jobspec]

Fg [jobspec]


Jobs [-lnp] [jobspec...]

Jobs-x command [args...]






Kill [-s sigspec |-sigspec] [pid | jobspec]...

Kill-l [signum]




Wait [n]


Bind [-m keymap] [-lvd] [-q name]

Bind [-m keymap]-f filename

Bind [-m keymap] keyseq: function-name


Break [n]


Control? Circle used


Example




Continue [n]


Control? Circle used


Example


Exit [n]


Leave the program. N is the Exit Status.


Return [n]


Used in function. N is the return value, which is the same as Exit Status.


Builtin shell-builtin [arguments]


Cd [dir]


Command [-pVv] command [arg...]


Declare [-frxi] [name [= value]

Typeset [-frxi] [name [= value]


Dirs [-l] [+/-n]


Echo [-neE] [arg...]


Enable [-n] [-all] [name...]


Eval [arg...]


Exec [[-] command [arguments]


Export [-nf] [name [= word]...

Export-p


Set [-- abefhkmnptuvxldCHP] [-o option] [arg...]


Unset [-fv] [name...]


Fc [-e ename] [-nlr] [first] [last]

Fc-s [pat = rep] [cmd]


Getopts optstring name [args]


Hash [-r] [name]


Help [pattern]


History [n]

History-rwan [filename]


Let arg [arg...]


Local [name [= value]...]


Logout


Popd [+/-n]


Pushd [dir]

Pushd +/-n


Pwd


Read [-r] [name...]


Readonly [-f] [name...]

Readonly-p


Shift [n]


Suspend [-f]


Test expr

[Expr]


Times


Trap [-l] [arg] [sigspec]


Type [-all] [-type |-path] name [name...]


Ulimit [-SHacdfmstpnuv [limit]


Umask [-S] [mode]



Bash built-in Parameter


Bash has many built-in parameters. You can check the parameters in man bash. Here I will only describe some common

And important.


PPID: The process ID of the bash caller.


PWD: current working directory.


OLDPWD: the previous working directory.


HOSTTYPE: machine type.


OSTYPE: name of the job system.


PATH: command search PATH.



PATH = "/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin :."


HOME: current user's home directory;


PS1: The value of this parameter is expanded (see PROMPTING

Below) and used as the primary prompt string.

Default

Value is ''bash \ $ ''.


PS2: The value of this parameter is expanded and used as

Secondary prompt string. The default is ''> ''.


PS3: The value of this parameter is used as the prompt for

Select command (see shell grammar abve ).


PS4: The value of this parameter is expanded and the value is

Printed before each command bash displays during

Execu-

Tion trace. The first character of PS4 is replicated

Mul-

Tiple times, as necessary, to indicate multiple levels

Of

Indirection. The default is ''+ ''.

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.