Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to replace shell variables

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to carry out shell variable replacement, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

#! / bin/bash

#--

#-choose 10 multiples of 3, starting with a given natural number

#--

#-enter

Read-p "Please enter a natural number:" Input

If [$Input-gt 0]; then

Echo "you entered a natural number, and the multiples of the 10 3s from this natural number are"

Else

Echo "what you entered is not a natural number, the program exited abnormally."

Exit

Fi

#-process and output

Count=0

Num=$Input

While [$Count-lt 10]

Do

Let Num2=$Num%3

If [$Num2-eq 0]; then

Echo "$Num"

Let Count++

Fi

Let Num++

Done

This program modifies the eighth line one space less than 0 at a time.

Zq@zq-py:~$. / xuanshu.sh

Please enter a natural number: 6

You are entering a natural number, and the multiples of the 10 3s starting from the natural number are

six

nine

twelve

fifteen

eighteen

twenty-one

twenty-four

twenty-seven

thirty

thirty-three

Zq@zq-py:~$. / xuanshu.sh

Please enter a natural number: 0

You entered a non-natural number and the program exited unexpectedly.

Zq@zq-py:~$. / xuanshu.sh

Please enter a natural number:-7

You entered a non-natural number and the program exited unexpectedly.

Zq@zq-py:~$. / xuanshu.sh

Please enter a natural number: 6.6

. / xuanshu.sh: line 8: [: 6.6: integer expression required

You entered a non-natural number and the program exited unexpectedly.

The substitution of shell variable of shell function

Two forms of expression of shell function

1. Function name () eg: hello () [echo "hello zq"]

2.function function name () eg:function hello () [echo "hello zq"]

The use of shell function

When using the shell function, you only need to write the name of the function eg: hello

#! / bin/bash

Zq ()

{

Echo "zq nihao"

}

Function zqq ()

{

Echo "zqq nihao"

}

Zq

Zqq

~

Zq nihao

Zqq nihao

#! / bin/bash

Zq ()

{

Echo "zq nihao"

}

Function zqq ()

{

Echo "$1 Hello"

}

Zq

Zqq China

#! / bin/bash

Zq ()

{

Echo "zq nihao"

}

Function zqq ()

{

Echo "$1 Hello"

}

Zq

Zqq $1 can specify variables when running this script

Substitution of shell variable

Default replacement: ${Var:-value}

If the Var variable is not set, the temporary setting is not value eg:$ {Day:-Sun}

Mandatory replacement: ${Var: + value}

If the Var variable has been set, it is temporarily set to value eg:$ {Day: + Sun}

Zq@zq-py:~$ echo ${Day:-sunday}

Sunday

Zq@zq-py:~$ echo $Day

Zq@zq-py:~$ echo ${Day:monday}

Zq@zq-py:~$ Day=saturday

Zq@zq-py:~$ echo ${Day:+monday}

Monday

Zq@zq-py:~$ echo $Day

Saturday

Default assignment replaces ${Var: = value}

If the Var variable is not set, it is permanently set to value eg:$ {Today:=Sun}

Zq@zq-py:~$ unset Today

Zq@zq-py:~$ echo $Today

Zq@zq-py:~$ echo ${Today:=friday}

Friday

Zq@zq-py:~$ echo $Today

Friday

Zq@zq-py:~$ echo $Today

Friday

Zq@zq-py:~$ unset Today

Zq@zq-py:~$ echo Today

Today

Default error replaces ${Var:? String}

If the Var variable is not set, the error output is string eg:$ {Torrow:? Date error}

Zq@zq-py:~$ Torrow

Torrow: command not found

Zq@zq-py:~$ echo Torrow

Torrow

Zq@zq-py:~$ echo ${Torrow:? "no set value"}

Bash: Torrow: no set value

Zq@zq-py:~$ Torrow=sunday

Zq@zq-py:~$ echo ${Torrow:? "no set value"}

Sunday

Zq@zq-py:~$

Replace ${Var/pattern/string} with a single match

If the Var variable matching pattern exists, the output of a single replacement is string

Eg:$ {zq/za/zb}

Global match replacement ${Var//pattern/string}

If the Var variable matches pattern multiple times, the output of all substitutions is string

Eg:$ {zq//za/zb}

Zq@zq-py:~$ echo $Torrow

Sunday

Zq@zq-py:~$ echo ${Torrow/sun/aun}

Aunday

Zq@zq-py:~$ echo $Torrow

Sunday

Zq@zq-py:~$ Torrow=$ {Torrow} sunday

Zq@zq-py:~$ echo $Torrow

Sundaysunday

Zq@zq-py:~$ echo ${Torrow/sun/aun}

Aundaysunday

Zq@zq-py:~$ echo ${Torrow/aun/aun}

Sundaysunday

Zq@zq-py:~$ echo ${Torrow//sun/aun}

Aundayaunday

Zq@zq-py:~$

The above is how to replace shell variables. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report