Get the App
SLTechnology News&Howtos  ›  Development  › 

Examples of string manipulation and for loop statements in shell

Shulou Source: shulou.com Published: 2022-06-03 19:16:11 09月25日 Update

This article focuses on "string manipulation in shell, examples of for loop statements". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "string manipulation in shell and examples of for loop statements".

The code is as follows:

#! / bin/bash

My_name= "jxq"

Echo $my_name

Echo ${my_name}

#--

# string operation

#--

# restrictions on single quotation mark strings, but not on double quotation marks:

# any character in the single quote will be output as is, and the variable in the single quote string is invalid

# single quotation marks cannot appear in single quotation marks (not even after using escape characters for single quotation marks)

Name= "will"

Age=24

My_full_name='$ {name} ${age}'

Echo ${my_full_name}

My_full_name= "${name} ${age}"

Echo ${my_full_name}

# string concatenation

Echo ${name} ${age}

# string length

Echo ${# name} # 4

# substring

Message= "I want to be healthy"

Echo ${message:2} # want to be health, 2 is position

Echo ${message:2:4} # want,2 is position,4 is len

# delete shortest match from front: ${string#substring}

Echo ${message#*want}

# delete shortest match from back: ${string%substring}

Echo ${message%healthy}

# delete longest match from front: ${string##substring}

Echo ${message##*h}

# delete longest match from back: ${string%%substring}

Echo ${message%%t*}

# find and replace: ${string/pattern/replacement}

Book_name= "Catch Eye Eye"

Echo ${book_name/Eye/Cat}

# find and replace all match: ${string//pattern/replacement}

Echo ${book_name//Eye/Cat}

File_path= "/ usr/local/bin"

# only replace when pattern match the beginning: ${string/#pattern/replacement}

Echo ${file_path/#\ / usr/tmp}

# only replace when pattern match the end: ${string/%pattern/replacement}

Echo ${file_path/%bin/tmp}

# string index

StringZ=abcABC123ABCabc

Echo `expr index "$stringZ" C12` # Mac OSX does not support expr

#--

# statement

#--

# if

If true

Then

Echo "ok, true"

Fi

# write on one line

If true; then echo "ok"; fi

Var='12'

If [$var-eq 12]; then

Echo "This is a numeric comparison if example"

Fi

If ["$var" = "12"]; then

Echo "This is a string if comparison example"

Fi

If [["$var" = * 12*]]; then

Echo "This is a string regular expression if comparison example"

Fi

Name= "jxq"

If ["$name" = "jxq"]; then

Echo "hello" $name

Fi

# Loop statement

For item in `ls * .sh`

Do

Echo $item

Echo "completed"

Done

# write on one line

For item in `ls * .sh`; do echo $item; echo "completed"; done

Counter=1

While:

Do

Echo "bee"

Let "counter=$counter+1"

If [$counter-eq 3]; then

Break # break/continue is similar to Java

Fi

Done

# Case statement

Opt= "install"

Case "${opt}" in

"install")

Echo "install..."

Exit

"update")

Echo "update..."

Exit

*) echo "bad opt"

Esac

At this point, I believe you have a deeper understanding of "string manipulation in shell and examples of for loop statements". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Characters strings quotes statements loops examples one line content learning restrictions no practical deeper code interest as is variables practicality practicality simple operation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Redmi macOS Shulou Tech Info MySQL