(Shell script) variables inside loop get reset.

Solution In Progress - Updated -

Issue

  • Running the following code:
files=0
directories=0
other=0
ls -A /some/directory | while read entry
do
   [ -f $entry ] && files=$((files+1)) && continue
   [ -d $entry ] && $=$((directories+1)) && continue
   other=$((other+1))
done
echo Files = $files.
echo Directories = $directories.
echo Other = $other

always results in:

Files = 0.
Directories = 0.
Other = 0.

no matter which direcory is used in place of "/some/directory" in the above example.

Environment

  • Red Hat Enterprise Linux (all)

  • ksh

  • bash

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content