Two tips for Linux shell: Shell arrays and string judgments

Source: Internet
Author: User
Tags python list

Recently, when using shell to write scripts, I want to implement two very simple functions in Python: 1: Determine if a string contains another string. 2: How to implement the list of Python work: 1. Here to share with you.


1: Determine if a string contains another string:

string= "ABCDEFG" if [["$string" =~ "abc"]];then echo "do something ..." else echo "nothing ..." Fi

The shell above determines whether "ABC" is included in the string $string.

The result of the operation is: do something.


2: Use the shell's array to reach the Python list effect.

To define an array:

# test= (a b c D E F g)

To view an array:

# echo ${test[@]}

A b c d E F g

Append an element e to the array:

# test= (${test[@]} e)

# echo ${test[@]}

A b c d E F g E

Removing an element from the subscript index, the following table of the shell array, like Python, starts at 0, so the 2 subscript represents the third element C:

# unset Test[2]

# echo ${test[@]}

A b d e F g E

Array Shard, the following command intercepts the range of elements of the test array 0-3, which is the first three elements:

# echo ${test[@]:0:3}

A b D

Iterate through an array:

# for element in ${test[@]} does echo "This is $element ..." Done#result This is b ... This is d ... This is e ... This is f ... This is g ... This is e ...

This article from the "Play God Clown" blog, reproduced please contact the author!

Two tips for Linux shell: Shell arrays and string judgments

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.