LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 04-18-2018, 10:37 AM   #1
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,415
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
Question 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

Last edited by JockVSJock; 04-18-2018 at 10:39 AM.
 
Old 04-18-2018, 10:42 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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 {} \;;

Last edited by MensaWater; 04-18-2018 at 10:46 AM.
 
1 members found this post helpful.
Old 04-18-2018, 12:11 PM   #3
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,415

Original Poster
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
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?
 
Old 04-18-2018, 12:21 PM   #4
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282

Rep: Reputation: 30
$DIRS is everything found where $DIR is each item in the list.
 
Old 04-18-2018, 01:22 PM   #5
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by JockVSJock View Post
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.
 
1 members found this post helpful.
Old 04-18-2018, 08:52 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,175

Rep: Reputation: 2683Reputation: 2683Reputation: 2683Reputation: 2683Reputation: 2683Reputation: 2683Reputation: 2683Reputation: 2683Reputation: 2683Reputation: 2683Reputation: 2683
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 ...
 
1 members found this post helpful.
  


Reply

Tags
bash, find


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk script returns a table. need results of 3 rd cell threezerous Linux - Newbie 4 01-31-2017 10:03 AM
How to end the bash script using commands in bash not manually by pressing ctrl+c Sanpreet Singh Linux - Newbie 1 07-03-2013 01:04 PM
Grep in bash script returns "No such file or directory", works manually gizza23 Programming 7 02-25-2010 04:37 PM
BASH Script, command line, same commands, different returns (need help). SilversleevesX Linux - Newbie 4 02-04-2010 03:46 PM
Bash script that returns the size of a file spank Linux - General 6 01-19-2006 03:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration