[SOLVED] Bash Script Returns Triple The Results Then It Would If Doing It Manually, Why?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 {} \;;
Last edited by MensaWater; 04-18-2018 at 10:46 AM.
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.
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 ...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.