Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of Bash-Script under LINUX

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "the usage of Bash-Script under LINUX". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The first Bash-Script

There are many kinds of LINUX shell, but Bash is the most widely used at present. This paper is also based on the Shell environment of Bash. Here is a simple example:

#! / bin/shecho 'hello worldview'

This is the simplest shell script. # of the first line! Used to tell the system what interpreter to use to execute the script (note: sh and bash themselves are different Shell, but in my current version of CentOS7, sh and bash are equivalent, sh is a symbolic link to bash). The echo command outputs text to the screen

How to run a script

One way is to use the script as a parameter to the interpreter, such as:

Sh test.sh

The second way is to grant executable permission to the file

Chmod + x test.sh or chmod 755 test.sh execute scripts. / test.sh variables and parameter variables

Bash is a weakly typed language, you just need to define the variable name = value directly. Use $var_name or ${var_name} when you need to reference this variable. $var_name is an abbreviation for ${var_name}, but in some cases you can only use ${var_name}, for example:

# echo'${num} instance runningstrings' when you need to concatenate two variables your_id=$ {USER}-on-$ {HOSTNAME} # or write them together with other strings

Therefore, it is recommended to use the style of ${var_name} when writing shell. The variable in bash is weakly typed, so he will accept it regardless of whether you pass him a string, number, or array. Sometimes this will lead to some problems, such as if you want to make a calculator and pass the variable a 'ABCDEFG', then we think that this variable can only accept numbers. Or maybe you want to define a constant and don't want the user to modify it. So we can use declare to declare some properties of the variable. The common parameters of declare are as follows:

-r defines a read-only variable, or you can use "readonly var_name" to define a variable as a read-only variable-I define the variable type as integer-a defines the variable type as an array-f defines a function-x defines a global variable

Just use unset var_name when you want to delete a variable. Note that this command cannot delete a read-only variable.

Parameters.

So how do you pass parameters to a script? When we want to pass parameters to a script, we can simply follow the command that executes the script with the parameters:

Sh. / test.sh parm1 parm2 parm3

Use $n (n is a number, such as $1 to get the first parameter) in the script to receive the incoming parameter. Here are the parameter variables that may be used frequently:

$0 the file name of the current script. The parameters passed to the script or function. N is a number. For example, the first parameter is $1. $# the number of parameters passed to the script or function. $* all parameters passed to the script or function. All parameters passed to the script or function by $@. It is different from $* when enclosed in double quotation marks (""). $? The exit status of the previous command, or the return value of the function. $_ the last argument of the previous command

One of the Linux commands we often use is something called option, the basic mode-optionname or-- longoptionname. If we need option when writing a script, a simple script can just treat it as a normal parameter manually, and for more complex ones, we can use getopt or getopt. Here I feel the need to mention the command set, which I find very powerful, very useful, but I have never used in a script. The set command is used to change the variables or options of internal scripts. One application is to change the options to influence the behavior of the script. Another application is to reset the position parameters using the output of set `commond`.

Example

#! / bin/bashecho "Command-line argument # 1 = $1" echo "Command-line argument # 2 = $2" echo "Command-line argument # 3 = $3" echo "-" echo "uses the output of `uname-a` to reset the location parameter" set `uname-a`echo "Field # 1 of 'uname-a' = $1" echo "Field # 2 of 'uname-a' = $2 "echo" Field # 3 of' uname-a' = $3 "exit 0output: [root@localhost study] # sh test_set.sh one two threeCommand-line argument # 1 = oneCommand-line argument # 2 = twoCommand-line argument # 3 = three--Sets the positional parameters to the output of the commond `uname-a`Field # 1 of 'uname-a '= LinuxField # 2 of' uname-a' = localhost.localdomainField # 3 of 'uname-a' = 3.10.0-514.el7.x86_64 array

Currently, only one-dimensional arrays are supported! You can assign values to an array in two ways:

#! / bin/bashmy_array= (A B C "D") my_array [4] = Fmy_array [5] = "G"

Read the values in the array through ${my_ array [IDX]}, or you can read all the elements in the array at once through ${my_array [*]} or ${my_array [@]}. Get the array length through ${# my_array [*]} or ${# my_array [@]}.

#! / bin/bashmy_array= (A B C "D") my_array [4] = Fmy_array [5] = "G123" echo "first element: ${my_array [0]}" echo "get all the elements of the array through *: ${my_array [*]}" echo "get all the elements of the array through @: ${my_array [@]}" echo "get the array length through *: ${# array [ *]} "echo" gets the array length through @: ${# my_array [@]} "output: [root@localhost study] # sh test_arr.sh first element: a gets all the elements of the array through *: A B C D F G123 gets all the elements of the array through @: A B C D F G123 gets the array length through *: 6 gets the array length by @: 6 about quotation marks

The string enclosed in backquotes is interpreted by bash as command-line execution and replaces the entire backquotation mark with its output.

#! / bin/shstr= `echo $path `echo ${str} output: [root@localhost study] # sh qut.sh / usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin single quotation marks

Characters in single quotation marks are treated as ordinary characters, and all special characters lose the meaning of crude oil.

#! / bin/shstr='$ {PATH} 'echo ${str} output: [root@localhost study] # sh qut.sh ${PATH} double quotes

Characters enclosed in double quotation marks are treated as ordinary characters except $,\,', and "which are still special characters and retain their special functions.

While and forfor cycle

The general format of the for loop is

For var in [list] do commondsdone

Let's look at an example:

#! / bin/bash# if the following list is not in quotation marks, the default division of for var in A B C Ddo echo "${var}" doneecho;echo# will treat the contents within the quotation marks as an element for var in "A", "B B B", "C C" do echo "${var}" doneecho The above two ways of writing echo# are a bit similar to java's for-each#. If you have used C, then you are more familiar with for ((axi1, bread1; a =: greater than or equal to)

-le or

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report