Shell theory Study (iv)

Source: Internet
Author: User

    • Take a string slice

1. Starts with the fourth string, intercepts the string to the end of the string

[Email protected] ~]# myname= "Hello myworld" [[email protected] ~]# Substr=${myname:3}[[email protected] ~]# echo $substr Lo MyWorld

2. Starting with the 6th string, intercept 8 character-length strings

[Email protected] ~]# filename= "/root/test.txt" [[email protected] ~]# Substr=${filename:6:8}[[email protected] ~]# echo $substrtest. txt

3. Take a partial position variable

#! /bin/bashecho $0echo ${@:1} starts with the first positional variable and goes to all positional variables to execute the result: [[email protected] ~]#./1.SH 77 88 99

4. Calculating string Lengths

[Email protected] ~]# filename= "/root/test.txt" [[email protected] ~]# echo ${#filename}14
    • Variable expansion: Contrasting styles

    1. By the front of the string, delete the matching person, delete the shortest

[Email protected] ~]# filename= "/etc/sysconfig/network-scripts/ifcfg-eth0" [[email protected] ~]# r=${filename#/*/}[ [Email protected] ~]# echo $rsysconfig/network-scripts/ifcfg-eth0

2. Delete the match from the front of the string, delete the longest

[Email protected] ~]# filename= "/etc/sysconfig/network-scripts/ifcfg-eth0" [[email protected] ~]# r=${filename##/*/} [Email protected] ~]# echo $rifcfg-eth0

3. Delete the match from the back of the string, delete the shortest

[Email protected] ~]# filename= "/etc/sysconfig/network-scripts/ifcfg-eth0" [[email protected] ~]# r=${filename%/*}[[ Email protected] ~]# echo $r/etc/sysconfig/network-scripts

4. After the string, delete the match, delete the longest

[Email protected] ~]# url= "www.baidu.com" [[email protected] ~]# R=${url%%.*}[[email protected] ~]# echo $rwww
    • Replace or delete a partial string

Syntax: ${variable/style/replacement string}

    1. Replace only the first conforming string

[Email protected] ~]# act= "Root:x:0:0:root:/root:/bin/bash" [[email protected] ~]# R=${act/:/#}[[email protected] ~]# Echo $rroot #x:0:0:root:/root:/bin/bash

2. Replace all matching strings

[Email protected] ~]# act= "Root:x:0:0:root:/root:/bin/bash" [[email protected] ~]# R=${act//:/#}[[email protected] ~]# Echo $rroot #x#0#0#root#/root#/bin/bash
    • Delete matched strings

Syntax: ${variable/style}

    1. Delete only the first matching string

[Email protected] ~]# act= "Root:x:0:0:root:/root:/bin/bash" [[email protected] ~]# R=${act/:/}[[email protected] ~]# echo $rrootx: 0:0:root:/root:/bin/bash

2. Delete all strings that match

[Email protected] ~]# act= "Root:x:0:0:root:/root:/bin/bash" [[email protected] ~]# R=${act//:/}[[email protected] ~]# Echo $rrootx 00root/root/bin/bash
    • Take a list of variable names, array index lists

    1. Take a list of variable names

[Email protected] ~]# filename= "Ifcfg-eth0" [[email protected] ~]# dir= "/etc/sysconfig/network-scripts/" [[Email Protected] ~]# dir_file= "$dir/$filename" [[email protected] ~]# echo ${[email protected]}dir dir_file

2. Take an array index list

[[email protected] ~]# ar= (a b c xy z)

[[email protected] ~]# r=${!ar[@]}
[Email protected] ~]# echo $r
0 1 2) 3 4

    • Flow control Statements

    1. Using compound commands (arithmetic operations)

    2. Use bash keywords [[]] to make a formula: [[judging]]

[[Email protected] ~]# if [[str > XYZ]];then> echo ' string str larger "> else> echo" string str relatively small "> fi string str is smaller

3. Using built-in commands: test-judged


This article is from the "Linux Revolution" blog, so be sure to keep this source http://kaibinyuan.blog.51cto.com/7304008/1619672

Shell theory Study (iv)

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.