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