1

I have very little understanding of bash scripting. I need to recurse through a folder and its subfolders, identify png files, pass them to an external linux application and capture the output of that application in a text file with the text file's first name being same as the file that was passed to the external program. Also the txt file has to be created in the same folder as the png file.

After searching through SO and googling I have come up with the following script

#!/bin/sh
cd folder1/folder2

find . -maxdepth 4 -iname '*.png' -type f |while read line; do

    fullfilename="${line##*/}";     
    base="${fullfilename%.*}";
        ext="${fullfilename##*.}";
        mypath="${line%/*}";
    #echo $fullfilename;
    #echo $base;
    #echo $ext;
    #echo $mypath;
    #echo $mypath/$base.txt;
    #echo $line;

    myexternalprogram -x $line > $mypath/$base.txt;
    # -x is a switch reqd by the external program
done

Several of the subfolders of folder2 have white spaces in their names e.g. Sub Folder 12

Running the above gives an error

...line 16: $mypath/$base.txt. ambigous redirect

Just to check i removed the redirect portion on line 16. I got the error

./subfolder1/subfolder2/Sub is an unknown extension

thrown by myexternal program obviously because it stuck at the white spaces in subfolder name.

Can anyone help me out please? What am I doing wrong?

Kevin
  • 53,822
  • 15
  • 101
  • 132
firaq pasto
  • 91
  • 2
  • 8

3 Answers3

1

Try putting quotes around the file names:

myexternalprogram -x "$line" > "$mypath/$base.txt";
SSteve
  • 10,550
  • 5
  • 46
  • 72
  • ...line16:"$mypath/$base.txt": ambigous redirect error – firaq pasto Jun 06 '13 at 17:47
  • The quotes are the fix for spaces in the path. The ambiguous redirect is caused by something else, e.g. misspelling the variable name. – SSteve Jun 06 '13 at 18:10
  • I just did a test and that worked for me so double-check the spelling of `$mypath/$base.txt` and also echo the value to make sure it's what you expect. – SSteve Jun 06 '13 at 18:18
0

Just quote "$line" to get rid of the whitespace problem.

blue
  • 2,683
  • 19
  • 29
0

Solved. Thanks to everyone who took time to read and help me.

It was the quotes i.e. quotes which i get when i type from my keyboard seem to be different from the quotes that i get when copy paste from SO. The lines fullfilename="${line##/}"; base="${fullfilename%.}"; ext="${fullfilename##*.}";

had been copy pasted from different SO posts / googled blogs. (Remember i have very little undertanding of bash :D ) These lines seem to have a different type of quote than the line mypath="${line%/*}"; which i had typed from my keyboard. I seem to have only one type of quotes on my keyboard which is ". Even though it looks same on openoffice word processor, opening my bash file in gedit threw up the difference!! Hence I copied the quotes from fullfilename line and pasted them into my mypath line and Viola!!

Can anyone enlighten me how come there are two types of quotes? I have never encountered this before.

firaq pasto
  • 91
  • 2
  • 8