Date: Tue, 09 Feb 1999 04:43:47 +1000 From: Greg Black <gjb@comkey.com.au> To: root@isis.dynip.com Cc: ben@scientia.demon.co.uk, freebsd-questions@FreeBSD.ORG Subject: Re: Help About Shell Script Message-ID: <19990208184347.13321.qmail@alpha.comkey.com.au> In-Reply-To: <199902080041.DAA21348@isis.dynip.com> of Mon, 08 Feb 1999 03:41:47 %2B0300 References: <199902080041.DAA21348@isis.dynip.com>
next in thread | previous in thread | raw e-mail | index | archive | help
Warning: this is fairly long, but it does include a proper illustration of a solution further down. > >> What if we wanted to tke the variables i and j > >> from a file. > > > > That would depend what format the file was. At the simplest level, with > > two files called "current_i" and "current_j", containing the numbers > > alone, you can read that like so: > > > > i=$(cat current_i) > > j=$(cat current_j) > That's too simple, but kinda reduntant, they are actually 4 loops > inside one-another, can't think of creating 4 files each containing an > integer, this won't be programming, its carpenting. Indeed. > > If the file contains something like > > > > i=5 > > j=18 > > > > you could probably do > > > > eval $(cat name_of_the_file) > > did not get this one, you mean ; > > i= $(cat name_of_the_file) ??? No, he meant what he wrote. Think about it. > if that's what you mean, then you solved my problem. I don't think so, because it's absurd. > > I'd recommend using Perl if you go much more complex. > > Ya, That's aa coooool thing to learn, I have seen what perl scripts can > do. But I think its very extensible language, I mean I'll be 65 years > before I reach the bottom of it, is that so, I'm ready to learn new > things, and always wish to learn the devls such as perl, tcl, tk, but > I'm kinda afraid that they'd be difficult for me to learn. This is sad. Perl is worth avoiding (for reasons that I'm happy to elaborate on, but won't do here). For complex multi-user GUI-based applications that need to be developed quickly, you can't go past a combination of Python for the scripting stuff, Tk for the GUI stuff (via Python's tkinter module) and (if it involves data) PostgreSQL for a database engine. But, to return to the question (which has got very muddy now), there's no reason at all to use anything more complex than a standard shell script for the kind of stuff that has been discussed in this thread. Here's an example -- it's a little bit contrived since it was whipped up for this illustration, but it's real enough to make some kind of sense. Say we have a data file with records stored one per line with names and addresses like this: ---------- Smith,James Alan Will,25 Wilson St,West Gosford,NSW,2456,02-9876-5432 Jones,Mary-Anne,23/567 Harrison Ave,Indooroopilly,Qld,4068,07-3456-7890 Anderson-Smythe,Jim,106 Veronica St,Camberwell,Vic,3126,03-9988-7766 ---------- Simple enough: 7 fields per record -- Surname, First Name(s), Street, Suburb, State, Postcode, Phone No -- each field separated by a comma and the records in any order. Now, say we want to print the data neatly formatted, sorted by surname, then we can use this trivial shell script: ---------- #!/bin/sh sort -t , addr.dat | ( IFS=", " while read surname firstname street suburb state pcode telno ; do printf "%8s: %s %s\n" Name $firstname $surname printf "%8s: %s\n" Address $street printf "%8s %s %s %s\n" "" $suburb $state $pcode printf "%8s: %s\n\n" Phone $telno done ) ---------- Save the data in a file called addr.dat and run the script and you'll get: Name: Jim Anderson-Smythe Address: 106 Veronica St Camberwell Vic 3126 Phone: 03-9988-7766 Name: Mary-Anne Jones Address: 23/567 Harrison Ave Indooroopilly Qld 4068 Phone: 07-3456-7890 Name: James Alan Will Smith Address: 25 Wilson St West Gosford NSW 2456 Phone: 02-9876-5432 If you wanted them sorted by postcode rather than surname, you would change the sort command to key on the 6th field like this: sort -t , -k 6 addr.dat None of this is rocket science and none of it needs anything more complex than the standard shell and standard unix commands that have been around for years. So go and play with this and see how it works, and empower yourself with the wonderful programming environment that you get for free in FreeBSD. -- Greg Black <gjb@acm.org> To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19990208184347.13321.qmail>