LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 01-22-2010, 09:27 AM   #1
KayakJim
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
done
}

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

I would appreciate any guidance on completing this.

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

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

Code:
for .....; do
  EXT="${file##*.}"
  if [[ ! "$EXT" == ".sh" ]]; then
    ... your code ...
  else
    echo ".sh file detected"
  fi
done
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, 09:53 AM   #3
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

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


Reply



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 07:09 PM
Bash script to access all files in a directory shinni Programming 5 04-24-2009 03:46 PM
bash script to check how many files in directory guest Programming 8 01-31-2009 11:55 PM
Traversing files in a given directory using a bash script kaujot Programming 7 03-07-2008 12:39 PM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM

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

All times are GMT -5. The time now is 04:54 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