[SLUG] sh scripting help
Hi Sluggers, Got a problem writing a shell script in sh. What it's supposed to do is look at a base path, get a directory listing and for each directory there it needs to concat the base path with the dir name and run a cat * |grep sometext on each. I can get a directory listing by doing a ls -l |cut -b 47-57 and that gives a list of directories but I cannot get either the path concatination working or the do for each bit working. Are there any sh guru's out there who could point me in the right direction code wise? (nb it has to be /bin/sh as it's the only shell the system uses. otherwise I'd write a little perl script that could do this with ease). Cheers, Paul The generation of random numbers is too important to be left to chance. -- anon. -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://lists.slug.org.au/listinfo/slug
Re: [SLUG] sh scripting help
On Thu, May 17, 2001 at 12:19:51AM +1000, Paul Robinson wrote: Got a problem writing a shell script in sh. What it's supposed to do is look at a base path, get a directory listing and for each directory there it needs to concat the base path with the dir name and run a cat * |grep sometext on each. I'm not sure if I understand the problem and I don't know what else you have available, but doesn't find . -type f -print | xargs grep sometext do what you want (and you can omit the '.' and -print for GNU find)? I can get a directory listing by doing a ls -l |cut -b 47-57 This looks full of magic numbers, but I assume it works for you. On my Linux box, this gives 16 22:13 i 16 22:14 l 16 22:13 l 16 22:13 l 16 22:13 m 16 22:13 m 16 22:13 m etc which is possibly because my username and group are longer than yours. But still, if portability is important this could be significant. and that gives a list of directories but I cannot get either the path concatination working or the do for each bit working. Is this what you mean? for i in `ls -l |cut -b 47-57` do directory=$basepath$i cat $directory/* | grep sometext done Maybe helpful, maybe not. Cheers, Malcolm -- I don't have a solution, but I admire your problem. -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://lists.slug.org.au/listinfo/slug
Re: [SLUG] sh scripting help
I've probably got the wrong end of the stick at this time of night, er, morning but something like find /basepath -type f -exec grep -H sometext {} \; would grep all files in directories under /basepath and print out filenames and lines that matched. Brad. On Thu, 17 May 2001 00:19, Paul Robinson [EMAIL PROTECTED] wrote: Hi Sluggers, Got a problem writing a shell script in sh. What it's supposed to do is look at a base path, get a directory listing and for each directory there it needs to concat the base path with the dir name and run a cat * |grep sometext on each. I can get a directory listing by doing a ls -l |cut -b 47-57 and that gives a list of directories but I cannot get either the path concatination working or the do for each bit working. Are there any sh guru's out there who could point me in the right direction code wise? (nb it has to be /bin/sh as it's the only shell the system uses. otherwise I'd write a little perl script that could do this with ease). Cheers, Paul The generation of random numbers is too important to be left to chance. -- anon. -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://lists.slug.org.au/listinfo/slug -- -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://lists.slug.org.au/listinfo/slug
Re: [SLUG] sh scripting help
This one time, at band camp, Paul Robinson said: Got a problem writing a shell script in sh. What it's supposed to do is look at a base path, get a directory listing and for each directory there it needs to concat the base path with the dir name and run a cat * |grep sometext on each. You're not re-implementing rgrep, are you? :) I can get a directory listing by doing a ls -l |cut -b 47-57 and that gives a list of directories but I cannot get either the path concatination working or the do for each bit working. instead of the cut -b, look at setting space as your delimiter and cutting based on field number, or better yet, use awk which treats contiguous whitespace as a single delimiter. In any case, I have this feeling you're trying to do what rgrep already does, so have a look at it before you go any further. -- jamesw Jaq what's wrong with the default? :) jdub It is poopie. -- SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/ More Info: http://lists.slug.org.au/listinfo/slug