0

I'm having trouble pointing to a specific file containing part of the string of another file in another directory.

If you look at the following command, lets say I have a file abc.foo in ./A, I need to apply a function by using abc_extendedname1.jpeg which is in ./B

for file in ./A/*; 

do echo $file;
function $file -opt ./B/${file%.foo}_extendedname1.jpeg ./B/${file%.foo}_extendedname2.jpeg;

done

Any help would be greatly appreciated!

jww
  • 97,681
  • 90
  • 411
  • 885
marc
  • 2,037
  • 9
  • 24
  • 32
  • 3
    FYI, `function` is a reserved keyword. Saying `my_function` would be clearer. – Charles Duffy Jun 07 '13 at 14:59
  • so basically you are trying to extract the basename w/o extension: http://stackoverflow.com/questions/2664740/extract-file-basename-without-path-and-extension-in-bash – doubleDown Jun 07 '13 at 16:31

2 Answers2

2
for file in ./A/*; do
  basename=${file##*/}
  basename_noext=${basename%.*}
  echo "$file"
  your_function "$file" -out \
    "./B/${basename_noext}_extendedname1.jpeg" \
    "./B/${basename_noext}_extendedname2.jpeg"
done
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • You're a life saver! Works beautifully. So I take it my mistake was in using $file without removing the directory info? – marc Jun 07 '13 at 15:10
  • Pretty much, yes. By the way, next time, try using `set -x` within your script (or `bash -x` to start it) to see what your script is doing wrong. (For bigger scripts it's worth also learning how to use `PS4` to include line numbers and such in that trace, but... well, baby steps). – Charles Duffy Jun 07 '13 at 15:58
-1

try

export d="`pwd`"
find ./A/*|while read file
do
    echo "$file"
    if [ -f  "$d/B/${file}_extendedname1.jpeg" ]
    then
        echo "$d/B/${file}_extendedname1.jpeg" found
    fi
done
Axel Amthor
  • 10,980
  • 1
  • 25
  • 44
  • 1
    Needs more quotes -- right now, this won't correctly handle filenames with spaces. – Charles Duffy Jun 07 '13 at 15:02
  • 1
    ...and backticks are more or less deprecated, use `$(...)` instead – Fredrik Pihl Jun 07 '13 at 15:03
  • And it will break if the working directory (not just a filename) contains spaces, and [pipelining into a loop will break as soon as it's called upon to increment a variable (or something else with side effects)](http://mywiki.wooledge.org/BashPitfalls#grep_foo_bar_.7C_while_read_-r.3B_do_.28.28count.2B-.2B-.29.29.3B_done), and the semicolon after the first `echo` is useless and looks like a PHP coder's work, and the `export ` is unnecessary, and `$d` is unnecessary and can be replaced by `.` (making the backquotes unnecessary), and it's lucky that's all I can see because I'm out of characters. – michaelb958--GoFundMonica Jun 07 '13 at 15:23
  • ...also, using `read` instead of `read -r` means that backspaces inside the filenames are interpreted instead of read literally. And using `find`'s default `-print` behavior means that filenames with newlines in their names won't work right (yup, they're legal on UNIX), and... so forth. – Charles Duffy Jun 07 '13 at 15:59