LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-09-2012, 12:56 PM   #1
arman
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Rep: Reputation: Disabled
Unhappy 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
 
Old 02-09-2012, 01:00 PM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,470

Rep: Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538
Quote:
Originally Posted by arman View Post
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.
 
1 members found this post helpful.
Old 02-09-2012, 01:06 PM   #3
arman
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
#!/bin/bash

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

done;
 
Old 02-09-2012, 02:07 PM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
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.
 
1 members found this post helpful.
Old 02-10-2012, 04:05 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
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,
 
1 members found this post helpful.
Old 02-10-2012, 04:10 AM   #6
arman
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
thanks everybody for useful suggestions. I appreciate your time
 
  


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
[SOLVED] Bash script to parse a file to get a set of line between a specific characters venkatrg Linux - Newbie 5 12-24-2010 06:55 AM
how to get the specific text from a txt file in bash script deepakdeore2004 Programming 8 04-30-2010 06:35 AM
Little bash script and file to give long (or any) directories short names WardXmodem Programming 1 11-23-2009 12:24 AM
bash:output file names from shell script to vi sickboy Linux - Newbie 6 10-14-2004 03:40 AM
script #!/bin/bash, problem with space in file names existent Linux - General 3 06-17-2004 08:13 AM


All times are GMT -5. The time now is 05:27 AM.

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