Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

arrays & whitespace

Status
Not open for further replies.

tomjermy

IS-IT--Management
Aug 21, 2003
13
GB
Hi,

I have an unexpected bash scripting problem.

I'm writing a script in bash which finds files named SETUP.DTA. The prog. searches through a fs, and produces a list of their locations.

I assign an array variable to the first few letters in the file and then get the program to spit out the path location, and the file name. The program looks like this;

Code:
#!/bin/sh

Array=(`find /home/thispath&subdirs -name SETUP.DTA`)
index=0

element_count=${#Array[@]}

while [ "$index" -lt "$element_count" ] # the -lt switch must mean is not equal to (the 0 index value)

do

        var=`cut -b 2-53 "${Array[$element_count]}"
        echo $var "     is in   " ${Array[$element_count]}

        let "element_count = $element_count - 1"

done

And the output looks like this:

[output]
[root@lxf 23:41:57 /scripts/sage] cat tek-tips\ example
cut: : No such file or directory
is in
co1 is in /home/tom/thispath&subdirs/7/SETUP.DTA
ac02 is in /home/tom/thispath&subdirs/8/SETUP.DTA
co6324 is in /home/tom/thispath&subdirs/9/SETUP.DTA
bunghole is in /home/tom/thispath&subdirs/1/SETUP.DTA
sdf is in /home/tom/thispath&subdirs/10/SETUP.DTA
asd is in /home/tom/thispath&subdirs/11/SETUP.DTA
sdjg is in /home/tom/thispath&subdirs/12/SETUP.DTA
rog is in /home/tom/thispath&subdirs/13/SETUP.DTA
sdf is in /home/tom/thispath&subdirs/14/SETUP.DTA
psjiodf is in /home/tom/thispath&subdirs/15/SETUP.DTA
s;dofih is in /home/tom/thispath&subdirs/16/SETUP.DTA
sd;ofh is in /home/tom/thispath&subdirs/2/SETUP.DTA
sdf[ p is in /home/tom/thispath&subdirs/3/SETUP.DTA
rt is in /home/tom/thispath&subdirs/4/SETUP.DTA
cox is in /home/tom/thispath&subdirs/5/SETUP.DTA
[/output]

I have 2 issues –
1. I do not know how to resolve the
[output]
cut: : No such file or directory
is in
[/output]

ie the first 2 lines of the output. The variable "${Array[$0]}" outputs a proper directory path which should not cause cut to have a problem.


2. Some of the subdirs’s pathnames have white space in them. I cannot make tr replace the “ “ with a [wink] “\ “ for the life of me.

If you want a set of the data to experiment on then please let me know, and I will provide you with one.

Please help me, I am truly stuck.

Many thanks,
Tom Jermy
 
You need to echo the array content to cut.
End of problem.
 
Not entirely... the array that you are asking me to cut is a path to a file. If I echo the array content, it just cuts out the path, not the content of the file.

I also need to get issue 2 sorted, so please, if anyone can help I'd really appreciate that.

Cheers!
Tom J
 
error 1 is due to the fact that altough the count line returns for example 9 items on the array these are numbered from 0-8.
so the code should be
------
while [ "$index" -lt "$element_count" ] # the -lt switch must mean is not equal to (the 0 index value)

do
var=`cut -b 2-53 "${Array[$element_count - 1]}"`
echo $var " is in " ${Array[$element_count - 1]}

let "element_count = $element_count - 1"
done
------

as for the other I could not yet figure it out. and it is puzzling me!!


As for the use of "cut", are all the files a 1 liner only or you do not mind to have every line of each file being outputed?


Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
The file that the cut command reads is recognised by "file" as a binary file. It contains characters which "break my pipe". The file contains characters (inc. control ones) which look messy and cannot be displayed with the path name.

The reason I'm doing this is to provide a linux intranet at the company I work at. It needs to be readable by the end users ;)

Thanks lots for your help Frederico

Tom J
 
try like the following.

#!/bin/sh

find . -name SETUP.DTA -fprint list


while read listfile;

do

var=`cut -b 2 "$listfile"`
echo $var " is in " echo "$listfile"

done < list


Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
Try using a for loop instead of while....

#!/bin/sh

for File in `find /home/thispath&subdirs -name SETUP.DTA`
do
var=`cut -b 2-53 &quot;$File&quot;`
echo $var &quot; is in &quot; $File
done


 
That works a treat, thanks very much all.
Tom
 
I have a program that after initiating a set of commands, will requires the user to enter some text. After which the user will press Ctrl-Z to save and end the program. How do I incorporate the Ctrl-Z into my program so that I can do this automatically rather than manually, since it's a huge set of text to be entered.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top