LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash script for specific file names (https://www.linuxquestions.org/questions/programming-9/bash-script-for-specific-file-names-928519/)

arman 02-09-2012 12:56 PM

bash script for specific file names
 
Dear all,

I need all the files in the specific directory to be checked. I need a bash script to put the files ending with "_extracted" in a variable, files ending with ".roi" in another variable.

I'm going to use each of the above mentioned variables in a custom command at the end of the script.

Any input is greatly appreciated

TB0ne 02-09-2012 01:00 PM

Quote:

Originally Posted by arman (Post 4598278)
Dear all,
I need all the files in the specific directory to be checked. I need a bash script to put the files ending with "_extracted" in a variable, files ending with ".roi" in another variable.

I'm going to use each of the above mentioned variables in a custom command at the end of the script. Any input is greatly appreciated

Ok..post what you've written, and we can help you. Otherwise, you can check out one of the MANY bash scripting guides you can find on Google, like this one:
http://tldp.org/LDP/abs/html/

Lots of examples in there to get you started.

arman 02-09-2012 01:06 PM

#!/bin/bash

for file in path/*
if [$file == *_extracted]
then
extracted_file =$file
else
if [$file== *.roi]
then
roi_file=$file
fi
fi

done;

millgates 02-09-2012 02:07 PM

Code:

  for file in path/*
  if [$file == *_extracted]

You take all files in path and then check if they end with _extracted. Why don't you just create a glob that matches the files you need?

Code:

for file in path/*_extracted
Actually, then you won't even need the loop.
Btw, you're missing a do keyword after the glob.
Also, after the opening bracket a space is needed, as well as before the closing one:

Code:

  if [ $file == *_extracted ]
Code:

extracted_file =$file
the space before the '=' must not be there. (the spaces in bash script are pretty confusing sometimes, aren't they?)

Oh, and you can also use the elif keyword to get rid the somewhat ugly if; else if ; fi; fi construct.

Nominal Animal 02-10-2012 04:05 AM

If you want to support all possible file names, and optionally scan in subdirectories too, use something like
Code:

# List of subtrees to search for
# (this defaults to all command-line parameters, if used in a script)
trees=("$@")

# Make sure locale does not affect file name handling
OLD_LC_ALL="$LC_ALL" OLD_LANG="$LANG"
LC_ALL=C LANG=C

# Array containing all files ending with _extracted into list1
list1=()
while read -rd "" file ; do
    list1[${#list1[@]}]="$file"
done < <( find "${trees[@]}" -maxdepth 1 -type f -name '*_extracted' -print0 )

# Array containing all files ending with .roi into list2
list2=()
while read -rd "" file ; do
    list2[${#list2[@]}]="$file"
done < <( find "${trees[@]}" -maxdepth 1 -type f -name '*.roi' -print0 )

# Restore locale
LANG="$OLD_LANG" LC_ALL="$OLD_LC_ALL"

If you want files in subdirectories too, just omit the -maxdepth 1 parameters to the find commands.

This uses ASCII NULs as separators. Since the Linux kernel uses them as well to indicate the end of a string (say a pathname), this will work for all possible file names.

The locale must be set to POSIX (LANG=C LC_ALL=C) because in UTF-8 locales, non-UTF-8 sequences (like say filenames using cp1252 character set) are an error and cause the commands to abort. Using the POSIX locale makes sure all file names are considered just opaque cookies, no matter what characters in which charset the names might contain.

A funny side note I just noticed:

Starting with an empty array, list=(), the following two lines are equivalent, and append $new as a new element to the array:
Code:

list=("${list[@]}" "$new")
list[${#list[@]}]="$new"

The latter one is several magnitudes faster. On my machine, the former one takes 330 times longer to run for 2000 files!

Hope this helps,

arman 02-10-2012 04:10 AM

thanks everybody for useful suggestions. I appreciate your time


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