LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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,038

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
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: 100Reputation: 100
shopt -s extglob
ls !(*.sh)
 
  


Reply


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 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


All times are GMT -5. The time now is 09:56 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration