Shell string operation summary

Source: Internet
Author: User
Shell string operation summary

#! /Bin/bash

######################################## ##
#
# Demonstrate string operations in bash
#
# Output result
#
######################################## ##

STR = "abc123abcabc"
# |-|
# | ------- |

# Calculate the length of a string
Echo $ {# STR} #12

# Operations related to location parameters
Echo $ {# *} # Number of location parameters
Echo $ {## the number of location parameters is the same as the above
Echo $ * # output all location parameters as a string
Echo [email protected] # same effect as above
# String Truncation
#
# $ {String # substring}
# Remove the first matched $ substring from the first character on the left of $ string
#$ {String ## substring}
# Remove the last matched $ substring from the first character on the left of $ string
#
# Start from the left
Echo $ {STR # A * c} # minimum matching between A and C
Echo $ {STR # A * c} # maximum matching between A and C

# Start from the right
Echo $ {STR % A * c} # Start from the last character on the right to find the shortest match between A and B.
Echo $ {STR % a * B} # Find the longest match between A and B starting from the last character on the right
# The mode cannot be found.

#== Substring =
# $ {String: Position}
# $ {String: positon: length}
#
Echo $ {STR: 2} # extract the string value from the first position
Echo $ {STR: 2: 3} # extract a value with a length of 3 characters from the first position
# Reverse extraction of substrings
Echo $ {STR :(-2)} # extract the string from the first position in the reverse direction
Echo $ {STR :(-2): 6} # extract a string with a length of 6 characters from the reverse of 2nd characters (:( it is impossible)
# How to extract 6 characters with only two characters in length?
Echo $ {STR :(-6): 3} #6 medium proposed 3 Total can be

# Location Parameter Extraction

Echo $ {*: 2} # extract the values of all the following parameters from the second parameter
Echo $ {*: 2: 3} # extract the values of the three parameters from the second parameter

# = Substring replacement =
# $ {String/substring/replacement}
# Use $ replacement to replace the first matched $ substring.
# $ {String // substring/replacement}
# Use $ replacement to replace all matched $ substring.
#
# $ {String/# substring/replacement}
# If $ substring matches the starting part of $ string, replace $ substring with $ replacement.
# $ {String/% substring/replacement}
# If $ substring matches the end of $ string, replace $ substring with $ replacement.
#
Str1 = "123abcabcab12"

Echo $ {str1/12/hover} # Replace the first
Echo ${str1 // 12/hover} # Replace all

Echo $ {str1/#12/hover} # Match 12 from the beginning. If yes, replace
Echo $ {str1/% 12/hover} # Match 12 from the end. If it is found, replace

######################################## ############# 3

Assign a single string to an array:

[Email protected] ~
$ STR = abcdefg

[Email protected] ~
$ I = 0; while (I <$ {# STR}); Do array [$ I] =$ {STR: I: 1 }; (I ++); done

[Email protected] ~
$ Set | grep Array
Array = ([0] = "A" [1] = "B" [2] = "C" [3] = "D" [4] = "E" [5] = "F" [6] = "G" [7] = "")

[Email protected] ~
$ Echo $ {array [0]}
A

[Email protected] ~
$ Echo $ {array [1]}
B

[Email protected] ~
$ Echo $ {array [@]}
A B C D E F G


For index in 'seq 0 $ ($ {# STR}-1 ))'
Do
Array [$ Index] =$ {STR: $ index: 1}
Done

 

 

 

Shell String Array Processing

This code is used to process the call of a program (SCRIPT) in the system, but does not know the specific location of the program file, but only knows the possible location. A little smart.


Exp_cmds = "$ base_dir/$ old_version/bin/exp_my_db/
$ Base_dir/$ old_version/bin/exp_db/
$ Base_dir/$ version/bin/exp_db/
$ Base_dir/$ old_version/My/scripts/exp_my_db/
$ Base_dir/$ old_version/My/scripts/exp_db/
$ Base_dir/$ version/My/scripts/exp_db "# create a string array separated by symbols /.

Exp_defaults _found = 0

For CMD in $ exp_cmds # Use for to loop
Do
If [-F $ cmd] # exp_db exists or exp_my_db is the script file.
Then
Echo "exporting database ..."
Run_as_oracle_nolog $ cmd 100 $ my_backup_dir 2> & 1 | $ tee-A $ tmp_file # core program
Exp_1__found = 1
Break # Haha found one and immediately quit after the function is completed.
Fi
Done

Although Sparrow is small, it is very enlightening.

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.