LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash Script Returns Triple The Results Then It Would If Doing It Manually, Why? (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-returns-triple-the-results-then-it-would-if-doing-it-manually-why-4175627924/)

JockVSJock 04-18-2018 10:37 AM

Bash Script Returns Triple The Results Then It Would If Doing It Manually, Why?
 
Please review the following script and let me know what the issue is.

Code:

#!/bin/bash

DIRS="/usr/lib /lib /usr/lib64 /lib64";

for DIR in $DIRS;
 do find $DIRS -type f -perm +022 -exec stat -c %a:%n {} \;;
done

It will find files, however the output is triple Vs is I do it from the CLI, one at a time.

Code:

find /usr/lib -type f -perm +022 -exec stat -c %a:%n {} \
find /lib -type f -perm +022 -exec stat -c %a:%n {} \
find /usr/lib64 -type f -perm +022 -exec stat -c %a:%n {} \
find /lib64 -type f -perm +022 -exec stat -c %a:%n {} \

The above will output the files with the incorrect permissions only once.

thanks

MensaWater 04-18-2018 10:42 AM

Change:
Code:

do find $DIRS -type f -perm +022 -exec stat -c %a:%n {} \;;
to:
Code:

do find $DIR -type f -perm +022 -exec stat -c %a:%n {} \;;
You're accidentally telling the find command to act on each the directories in first usage each time because you use original variable $DIRS. Your loop is designed to do a single directory on each iteration and that is what you are setting as $DIR.

Alternatively you could avoid the for loop altogehter by just doing:
Code:

#!/bin/bash
DIRS="/usr/lib /lib /usr/lib64 /lib64";
find $DIRS -type f -perm +022 -exec stat -c %a:%n {} \;;


JockVSJock 04-18-2018 12:11 PM

Simple syntax error, thanks.

I still get confused on the for statement. The i is a variable and the other part if what we are looping thru, correct?

snowman81 04-18-2018 12:21 PM

$DIRS is everything found where $DIR is each item in the list.

MensaWater 04-18-2018 01:22 PM

Quote:

Originally Posted by JockVSJock (Post 5844736)
I still get confused on the for statement. The i is a variable and the other part if what we are looping thru, correct?

People often use "i" ($i) in for loops (i = iteration maybe?) but you can use any variable name you want. In yours you used DIRS for the list of directories then just DIR where others might have used i for each iteration. No reason not to use DIR ($DIR) instead of i ($i) other than typing. You could have used BILLYBOB ($BILLYBOB) and it would have worked so its all a matter of preferences.

I prefer to put names on variables because it gives me a clue later in long scripts what the variable relates to. I probably woulnd't have chosen DIRS and DIR given how close in name they are. I might have made first variable DIRLIST and the other DIR (or EACHDIR) to differentiate them but again its just a matter of preference. As shown, you can do what you want with DIRS and DIR.

chrism01 04-18-2018 08:52 PM

I agree with MensaWater; always go out of your way to make the 'list' var and 'item' var names sufficiently different that you won't get stung like that :)
You'll thank us later ;)

As a general rule I almost never use just $i or equiv as loop vars any more.
For one thing you may have >1 loop in same file or set of files ...


All times are GMT -5. The time now is 10:19 PM.