LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-05-2020, 12:54 PM   #1
velocityvgc
LQ Newbie
 
Registered: Apr 2020
Posts: 2

Rep: Reputation: Disabled
Performing script repeatedly on all files meeting a criterion


I want to perform a script on any number of files that do not have a .done suffix. In this case, my files have some values that I want to source and pass to a script.

My script looks something like this
Code:
source #files that don't have .done suffix

/home/username/script_to_call #pass values from file above
How do I
1) source files that don't have a .done suffix
2) repeatedly perform /home/username/script_to_call on each file

Note that script_to_call is meant to only take in one file's values at a time (not all files' values at once). I want to perform script_to_call once for each file that does not have the .done suffix, essentially looping through all the sourced files.
 
Old 04-05-2020, 01:13 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,378
Blog Entries: 3

Rep: Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772
Welcome. That's rather a sparse script. :/

One way to approach the task you describe would involve the find utility. See "man find" and scroll down to the -not, -name, and -print options. Then when you have it showing the results you would like, try adding in the -exec option.
 
Old 04-05-2020, 01:13 PM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,688

Rep: Reputation: Disabled
Finding Files > Name > Base Name Patterns
Finding Files > Combining Primaries With Operators
Actions > Run Commands > Single File
Code:
find -type f -not -name \*.done -execdir script_to_call {} \;
@Turbocapitalist beat me to it.

Last edited by shruggy; 04-05-2020 at 01:23 PM.
 
Old 04-05-2020, 02:15 PM   #4
velocityvgc
LQ Newbie
 
Registered: Apr 2020
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
Finding Files > Name > Base Name Patterns
Finding Files > Combining Primaries With Operators
Actions > Run Commands > Single File
Code:
find -type f -not -name \*.done -execdir script_to_call {} \;
@Turbocapitalist beat me to it.
Thank you! Does this method source the files in the same way source does?

Also, I need to pass these sourced values to script_to_call with flags like
Code:
-flag $value1
, where in this scheme do I implement that?
 
Old 04-05-2020, 03:22 PM   #5
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,688

Rep: Reputation: Disabled
Quote:
Originally Posted by velocityvgc View Post
Does this method source the files in the same way source does?
No, it doesn't. I'm afraid I misunderstood what you wanted. You should write a wrapper script, pass a file name as parameter to it, source this file from inside it, then call script_to_call with prepared flags and parameters. This wrapper_script is what you're calling in the find command:
Code:
find -type f -not -name \*.done -execdir wrapper_script {} \;
Alternatively, you may try invoking sh -c in the -execdir action of find:
Code:
find ... -execdir sh -c '. "$0"; ...' {} \;

Last edited by shruggy; 04-05-2020 at 03:36 PM.
 
Old 04-05-2020, 05:06 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,842

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
An alternative to find is a for loop over all files, and in the loop skip the unwanted ones
Code:
for fn in *
do
  # skip if *.done
  case $fn in (*.done) continue;; esac
  echo "do something with $fn"
done
 
  


Reply

Tags
shell



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
Help! - All users must appear as the user [Zeno] when performing operations on the share on an NFS share ZenoxRedHat Linux - Newbie 3 10-20-2018 04:11 AM
Using Shell Script - How to schedule - meeting/its invite in MS Outlook 2010 Meeting sangal_ak04 General 1 02-10-2012 09:32 PM
[SOLVED] Error When Performing "make all" : /usr/bin/ld: -f may not be used without -shared limh Linux - Software 6 08-28-2009 08:39 PM
Criterion for a boot loader like GRUB zoharl Linux - General 5 12-08-2007 08:58 PM
Criterion Aptos pcb layout program barrys Linux - Software 0 07-29-2004 07:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:14 AM.

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