Re: shell script question
On Mon, 2003-01-20 at 08:38, Erik Price wrote: > [erikprice@host:/home/erikprice]$ for i in `ls`; do "`which du` -khs > $i"; done [ ... ] > > As you can see in the above, I've had to put `which du` in the "do" > section (because for some reason the "du" command isn't found if I don't > specify an absolute path), and the filename arguments passed to the "do" > section aren't found. If I just type "du -khs bak" then it works fine. Get rid of those double quotes! :) for i in `ls`; do `which du` -khs $i; done Ben ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss
Re: shell script question
Ben Boulanger wrote: On Mon, 2003-01-20 at 08:38, Erik Price wrote: [erikprice@host:/home/erikprice]$ for i in `ls`; do "`which du` -khs $i"; done [ ... ] As you can see in the above, I've had to put `which du` in the "do" section (because for some reason the "du" command isn't found if I don't specify an absolute path), and the filename arguments passed to the "do" section aren't found. If I just type "du -khs bak" then it works fine. Get rid of those double quotes! :) Strange -- I didn't think that the double quotes would cause problem -- they do allow expansion don't they? (It's single quotes that don't?) At any rate, thanks guys. Erik ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss
Re: shell script question
On Mon, 20 Jan 2003, Erik Price wrote: =>Hi, => =>I am probably overlooking something obvious but it seems that when I try =>to execute a command-line "for" loop, the "do command" part is not =>executed from the current directory. Is that normal? Here is what I mean: => =>[erikprice@host:/home/erikprice]$ for i in `ls`; do "`which du` -khs =>$i"; done =>bash: /bin/du -khs bak: No such file or directory =>bash: /bin/du -khs bin: No such file or directory =>bash: /bin/du -khs cvs: No such file or directory =>bash: /bin/du -khs dev: No such file or directory =>bash: /bin/du -khs docs: No such file or directory =>bash: /bin/du -khs down: No such file or directory =>bash: /bin/du -khs ip-up: No such file or directory =>bash: /bin/du -khs java: No such file or directory =>bash: /bin/du -khs mail: No such file or directory =>bash: /bin/du -khs pub: No such file or directory =>bash: /bin/du -khs public_html: No such file or directory =>bash: /bin/du -khs tmp: No such file or directory => =>As you can see in the above, I've had to put `which du` in the "do" =>section (because for some reason the "du" command isn't found if I don't =>specify an absolute path), and the filename arguments passed to the "do" =>section aren't found. If I just type "du -khs bak" then it works fine. => =>My shell is bash 2.05 and I set my $PATH in ~/.bashrc (Gentoo Linux). => =>Thanks, => =>Erik The quoting is wrong. Do this instead: for i in `ls`; do `which du` -khs $i; done The way you had the double quotes caused the shell to think that the command to be executed would be the output of which concatenated with -khs followed by the filename. So it was trying to find a single file to execute called /bin/du -khs bak But for the life of me I can't figure out why you are running which in the first place. -- -Time flies like the wind. Fruit flies like a banana. Stranger things have - -happened but none stranger than this. Does your driver's license say Organ -Donor?Black holes are where God divided by zero. Listen to me! We are all- -individuals! What if this weren't a hypothetical question? [EMAIL PROTECTED] ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss
Re: shell script question
Steven W. Orr wrote: But for the life of me I can't figure out why you are running which in the first place. Well, now that I've stopped using the double quotes it seems that the shell can find "du" on its own after all!! ;) Erik ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss
Re: shell script question
On Mon, 20 Jan 2003, at 8:38am, [EMAIL PROTECTED] wrote: > for i in `ls`; do "`which du` -khs $i"; done First, a pet peeve of mine: Use for i in * ; instead of for i in `ls` ; Both generate a list of filenames, but the later needless invokes an external process to do so. The shell is perfectly capable of generating a file list all by itself. :) Now, on to your real problem. The for loop is basically correct. If we look at the part inside the "do ... done" construct, we see that this command will be executed: "`which du` -khs $i" It is essential to understand that shell parsing is built upon splitting a command line up into "words", separated by spaces. Each word is then interpreted individually. Placing a series of characters into single or double quotes turns that series of characters into a single word. Since the first word in an otherwise normal command line is interpreted as the command to run, the system will treat the entire above construct as the name of a command to run. Inside double quotes, certain expansions (in this case, variable and back-quote expansion) are still done. The output of which du is used to replace the back-quote construct. And the $i is replaced with the loop value for each iteration of the for loop. So, after expansion, the command might look like this: "/bin/du -khs bak" Since that is enclosed in double-quotes, the shell is going to consider it a single word, and look for a command with that exact string for a name (spaces and all). Obviously, such a command does not exist, so you get the errors you see. A corrected version of your original might be: for i in * ; do du -khs "$i" ; done The loop variable is placed in double-quotes to make sure the "du" command sees it as a single word. Otherwise, if one of your directories contained spaces or other shell meta-characters, it would get mangled by the shell before it got passed to "du". The rest of the command ("du" and "-khs") you want parsed as individual words so they get handled by the shell as the command to run and said command's arguments, respectively. Hope this helps, -- Ben Scott <[EMAIL PROTECTED]> | The opinions expressed in this message are those of the author and do not | | necessarily represent the views or policy of any other person, entity or | | organization. All information is provided without warranty of any kind. | ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss
Re: shell script question
[EMAIL PROTECTED] wrote: First, a pet peeve of mine: Use for i in * ; instead of for i in `ls` ; I always forget that it can be done that way, but in fact, in this case I was ls'ing a diff't directory. I just posted a simplified version to the list. Also, I recall from an earlier discussion on this list that "find" is generally better to use in situations like this than "ls" but "ls" takes less arguments, and I just wanted to quickly get an estimate of a directory's disk usage, not write a true full-blown shell script. I'll try to remember that I can glob instead. A corrected version of your original might be: for i in * ; do du -khs "$i" ; done The loop variable is placed in double-quotes to make sure the "du" command sees it as a single word. Otherwise, if one of your directories contained spaces or other shell meta-characters, it would get mangled by the shell before it got passed to "du". The rest of the command ("du" and "-khs") you want parsed as individual words so they get handled by the shell as the command to run and said command's arguments, respectively. Good point. Hope this helps, Yes it does. Thank you. Erik ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss
Re: shell script question
[EMAIL PROTECTED] said: >>> Otherwise, if one of your directories contained >>>spaces or other shell meta-characters, it would >>> get mangled by the shell >>>before it got passed to "du". Another damned good reason NOT to have spaces in filenames, as we discussed a week or two ago here on the list. :-) After all, they're filenames, not filesentences ;-) Bayard ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss
Re: shell script question
At 12:36 PM 1/20/2003 -0500, Bayard R. Coolidge wrote: [EMAIL PROTECTED] said: >>> Otherwise, if one of your directories contained >>>spaces or other shell meta-characters, it would >>> get mangled by the shell >>>before it got passed to "du". Another damned good reason NOT to have spaces in filenames, as we discussed a week or two ago here on the list. :-) After all, they're filenames, not filesentences ;-) Bayard ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss I fully agree with you Bayard, but as we all know, anyone who has been doing *nix for some time or who was taught correctly, knows not to use spaces in *nix file names. That is what they made the "_" character for. The problem we run into most of the time is that people who have converted over from Windows are so used to using spaces. It is a habit that people will always have to be broke of. Jeff Kirkland ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss
Re: shell script question
[EMAIL PROTECTED] writes: > It is essential to understand that shell parsing is built upon splitting a > command line up into "words", separated by spaces. Since we're nit-picking, this isn't precisely true. The shell splits command lines into arguments using the characters in $IFS as delimiters. Normally IFS is set to space, tab, and newline, but this isn't always true. (now you know why "secure" shell scripts always set IFS to a known good value...) Regards, --kevin -- Kevin D. Clark / Cetacean Networks / Portsmouth, N.H. (USA) cetaceannetworks.com!kclark (GnuPG ID: B280F24E) alumni.unh.edu!kdc ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss
Re: shell script question
Jefferson Kirkland <[EMAIL PROTECTED]> writes: > ...but as we all know, anyone who has been > doing *nix for some time or who was taught correctly, knows not to use > spaces in *nix file names. Just for the record, I disagree with this, but I don't have a lot of interest in debating this. Regards, --kevin -- Kevin D. Clark / Cetacean Networks / Portsmouth, N.H. (USA) cetaceannetworks.com!kclark (GnuPG ID: B280F24E) alumni.unh.edu!kdc ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss