Did you know LQ has a Linux Hardware Compatibility List?
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 01-22-2010, 10:27 AM   #1
LQ Newbie
Registered: Jan 2010
Location: Hendersonville, TN
Distribution: Ubuntu, Gentoo
Posts: 1

Rep: Reputation: 0
Bash script that reads files in directory and processes them

I am trying to create a function within my .bashrc that will process all of the files that do not end with .sh within a directory and execute them.

The following is what I have so far. I am missing a way of excluding files that end with .sh though.

function startall {
for file in /etc/init.d/*.; do
"${file}" start

function stopall {
for file in /etc/init.d/*.; do
"${file}" stop

I would appreciate any guidance on completing this.

Old 01-22-2010, 10:52 AM   #2
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,052

Rep: Reputation: 379Reputation: 379Reputation: 379Reputation: 379
You can always add a check inside the loop. Something like:

for .....; do
  if [[ ! "$EXT" == ".sh" ]]; then
    ... your code ...
    echo ".sh file detected"
Note that the string mangling stuff ${...##.*} is a bashism (bash-only stuff), just keep that in mind. If you need to port this to some other shell some day you will need to use something like sed to do that instead.
Old 01-22-2010, 10:53 AM   #3
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 100Reputation: 100
shopt -s extglob
ls !(*.sh)


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
Loop through all files in a directory. Bash/Perl script? Nzo Linux - Newbie 9 12-09-2009 08:09 PM
Bash script to access all files in a directory shinni Programming 5 04-24-2009 04:46 PM
bash script to check how many files in directory guest Programming 8 02-01-2009 12:55 AM
Traversing files in a given directory using a bash script kaujot Programming 7 03-07-2008 01:39 PM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-23-2008 12:25 AM

All times are GMT -5. The time now is 06:10 AM.

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