LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script that reads files in directory and processes them (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-that-reads-files-in-directory-and-processes-them-784030/)

KayakJim 01-22-2010 09:27 AM

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

i92guboj 01-22-2010 09:52 AM

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.

quanta 01-22-2010 09:53 AM

shopt -s extglob
ls !(*.sh)


All times are GMT -5. The time now is 06:51 PM.