Get the App
SLTechnology News&Howtos  ›  Development  › 

An example Analysis of the scope of variables in bash Loop

Shulou Source: shulou.com Published: 2022-06-03 06:08:04 11月17日 Update

Editor to share with you the scope of variables in the bash cycle example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Let's take a look at a piece of wrong code first.

#! / bin/bash

For x in `ls * /. * /. * `

Cat $x | do echo $x

While read line

Do

C = `echo $line | awk-F' (''{print $1}'`)

# echo $c

ITunes $((iComplec))

Done

Echo

Done

Echo $I

What's wrong with the above code?

The pipe after cat will prevent the value of I from being incremented by 1.

The right way:

The code is as follows:

#! / bin/bash

For x in `ls * /. * /. * `

Do echo $x

While read line

Do

C = `echo $line | awk-F' (''{print $1}'`)

# echo $c

ITunes $((iComplec))

Done

Tags: Code articles functions variables examples scope analysis loops content not much most methods more knowledge pipelines industries information information channels errors problems Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Apple Shulou Information NVidia MariaDB