The Linux command learns the shift command

Source: Internet
Author: User

Positional parameters can be shift shifted left with the command. For example, the shift 3 original $4 now become $1 , the original $5 now become and $2 so on, the original $1 , $2 $3 Discard, $0 do not move. Commands with no parameters are shift equivalent shift 1 .

A very useful Unix command: Shift. We know that for positional variables or command-line arguments, the number must be deterministic, or when the Shell program does not know its number, you can assign all parameters together to the variable $*. If the user requires that the Shell do not know the number of positional variables, but also one by one to the parameter one by one processing, that is, after $ $, after the $ $ $, and so on. The value of the variable before the shift command executes is not available after the shift command executes.
Examples are as follows:
#测试 shift Command (X_SHIFT.SH)
1 until [$#-eq 0]2 Do3 echo "The first parameter is:" The number of arguments is: $# "4 shift5 Done
Execute the above procedure x_shift.sh:
$./x_shift.sh 1 2 3 4
The results are shown below:
The first parameter is: 1 The number of parameters is: 4
The first parameter is: 2 The number of parameters is: 3
The first parameter is: 3 The number of parameters is: 2
The first parameter is: 4 The number of parameters is: 1
From the above, the shift command executes every time, the number of variables ($#) minus one, and the value of the variable is one bit ahead of the other, the following code calculates all the arguments of the command line with the until and shift commands.
#shift the application of the command on the file (x_shift2.sh)
1 if [$#-eq 0] 2 then 3 echo "Usage:x_shift2.sh parameter" 4 exit 1 5 fi 6 sum=0 7 until [$#-EQ 0] 8 do 9 sum= ' expr $sum + ' Shift11 done12 echo ' sum is: $sum "
Execute the above procedure:
$x _shift2.sh 10 20 15
It shows the result as:
45
The shift command has another important purpose, and BSH defines 9 positional variables, from $9 to $9, which does not mean that the user can only use the command line with more than 9 parameters, and with the shift command, it is possible to access more than one or more of them.
The shift command moves the number of arguments at a time, specified by the parameters they take. For example, when the shell program finishes processing the first nine command-line arguments, you can use the Shift 9 command to move the $ $ $.

Positional parameters in a 1.shell script

There are two types of positional parameters in a shell script:

One is the argument passed to the script when the script is invoked, and the other is the argument to the function when invoking the script's function.

2.shift

Shift can be used to move positional parameters to the left.

How to use:

Shift num Moves the positional parameter to the left in NUM positions, and NUM is 1 by default.

To illustrate:

[Plain]View PlainCopy
    1. #!/bin/bash
    2. function func_t () {
    3. Shift #将函数func_1的位置参数向左移动一位
    4. echo [email protected]
    5. }
    6. Shift #将脚本tmp The position parameter of the. SH Move left one
    7. func_t EE RR SS
    8. echo [email protected]

Using the SH tmp.sh 11 22 33来 Call script, the execution results are as follows:

[Plain]View PlainCopy
    1. RR SS
    2. 22 33

Under Linux If you want to modify the network card parameters there are many ways, the first method is to modify the/etc/network/interfaces file, this method can still be valid after restarting, and the other way is to use the Ifconfig directive, this method can only temporarily modify the network card parameters, Will expire after a reboot.

The first type: Modify the Interfaces file

The interfaces file is a network interface configuration file under the/etc/network/folder, which contains configuration information for all network interfaces.

The interfaces file has its own authoring rules, where rows beginning with ' # ' are ignored by the system and can therefore be used as annotations. Each instruction takes on one line, and if it cannot be written, use the ' symbol as the end of this line, and then write the command on the next line.

The file consists of "Iface", "Mapping", "Auto", "allow-" and "source" fields. Here is an example:

auto lo eth0allow-hotplug eth1iface lo inet loopbacksource interfaces.d/machine-dependentmapping eth0    script /usr/local/sbin/map-scheme    map HOME eth0-home    map WORK eht0-workiface eth0-home inet static address 192.168.1.1 netmask 255.255.255.0 up flush-mailiface eth0-work inet dhcpiface eth1 inet dhcp

The line that starts with "auto" is used to determine that the physical network interface will be started when the ifup -a instruction is run (this instruction will be used in the system startup script, so the network adapter that starts with "Auto" will be started automatically when the boot is turned on). After the "Auto" field should be followed by the name of the physical interface, the interfaces file can be set up in multiple "auto" mode to start the network interface, the ifup instruction will start the network device in sequence.

The line that begins with "allow-" is used to determine which subsystem to start the network devices that should be started automatically. This allows you to use directives ifup --allow=hotplug to launch network devices that have "Allow-hotplug" set.

Note:"Allow-auto" and "auto" are synonyms.

Lines that start with "source" are fields that contain other files, so you can split the configuration into several files. The "source" is followed by a file path, and the wildcard character underneath the shell can be used.

The line that begins with "mapping" is used to determine the logical interface name for the physical device interface that will be started. The first line of the "Mapping" field begins with "Mapping", followed by a pattern match (such as eth*) that follows the glob instruction syntax under the shell. Each "mapping" field must contain a script definition that uses the physical device interface name as a parameter and will provide it with all fields starting with "map" (excluding "map" itself) as input. A string is output before the end of the script execution, which can be referenced in the example file below/usr/share/doc/ifupdown/examples.

Map a name that contains the search map matching pattern and execute the script using the first matching field, the name of the script output is the name of the original NIC that will be mapped.

Such as:

Configuring a network by pinging

Linux has a better way of handling PCMCIA cards and can determine network addresses with a configuration script when the Pcmica card is plugged in. However, the laptop network card is a laptop, not a PCMCIA card, because often need to rush between the Office, laboratory and home, often need to modify the network address. If I have DHCP installed in every place I go, I can set eth0 to DHCP, but my case is that you can use DHCP at home and use fixed addresses in offices and labs.

To solve this problem, we can use a mapping mechanism, the basic principle of which is to run a program to determine the current environment and choose a set of configurations for this environment. What I'm using now is to ping a network's gateway to determine which network the current NIC is connected to, and then select the network configuration.

First, there is a file ping-places.sh in the/usr/share/doc/ifupdown/examples, copy it to the/etc/network directory, and then chmod a+x /etc/network/ping-places.sh . Here is an example of editing the/etc/network/interfaces file:

mapping eth0①script/etc/network/ping-places.shmap 192.168.0.107/24 192.168.0.1 Homemap Span class= "Hljs-number" >10.1.133.165/24 10.1.133.1 Officemap 10.1.0.107/24 10.1.0.1 labiface Home inet dhcp②iface Office inet static③address 10.1.133.165netmask 255.255.255.0gateway 10.1.133.1up cp/etc/resolv.conf.school/etc/resolv.conf④iface Lab inet staticaddress 10.1.0.107netmask 255.255.255.0gateway Span class= "Hljs-number" >10.1.0.1up cp/etc/resolv.conf.school/etc/resolv.conf      
    1. Indicates that for network device call script/etc/network/ping-places.sh, if you can use address 192.168.0.107/24
      Ping the address 192.168.0.1, the eth0 is mapped to the device home, which is the configuration to start home. The following office and lab are similar.
    2. Represents the address that the virtual device home uses for DHCP assignment.
    3. Indicates that the virtual device Office uses a fixed address.
    4. Indicates that the CP command is also executed after starting this network device to specify a domain name resolution method. In addition to up, there are pre-up, down, and post-down that can be used to specify commands to execute before or after a network device is started or stopped.

Note: There are some examples of network configuration and scripts required in /usr/share/doc/ifupdown/examples .

ifupTypically, a physical interface name is given as its first parameter without an option. ifupThis name is also used as the initialization logic name for the interface, unless it is specified with a "=logical" format suffix, which ifup uses "LOGICAL" as the initialization logical name for the interface.

Network adapter Configuration Instance loopback parameter configuration
## The loopback network interface(配置环回口)# 开机自动激lo接口auto lo# 配置lo接口为环回口iface lo inet loopback
DHCP mode configuration
## The primary network interface (配置主网络接口)# 开机自动激活eth0接口auto eth0# 配置eth0接口为DHCP自动获取iface eth0 inet dhcp
Static IP address Assignment
##ThePrimaryNetworkInterface (configure primary network interface) #开机自动激活Eth0 interfaceAutoeth0# ConfigurationEth0 interface is statically setIP AddressIfaceEth0inetStaticAddress 10.16.3.99Netmask 255.255.255.0Network 10.16.3.0Broadcast 10.16.3.255Gateway 10.16.3. #dns-* Options  is implemented    by the resolvconf package, if installed (  DNS settings)dns-nameservers.153.177.196 . 153 177.197 Dns-search Fireteam. org                
Wireless Card Configuration
AutoWlan0IfaceWlan1inetstatic wpa-ssid  Wifi-name wpa-psk wifi-passwork address 192.168.1.200 netmask 255.255.0  Gateway 192.168.1 .1 dns-nameservers 202.196.64.1   

The Linux command learns the shift command

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.