Re: shell script problem
In freebsd-questions Digest, Vol 447, Issue 1, Message: 13 On Sun, 23 Dec 2012 18:48:12 +0100 Dh?nin Jean-Jacques > 2012/12/23 Polytropon > > #!/bin/sh > > > > cat foo.txt | while read LINE1 > > do > > cat bar.txt | while read LINE2 > > do > > if [ "$LINE1" = "$LINE2" ]; then > > sw="1" > > echo "Current value of sw is : " $sw > > > * ps -l | grep $$ * > # see subshell here Yes indeed. > > break > > fi > > done > > > > * echo " Process: " $$* > # And the parent Yep. > > echo "Value of sw is : " $sw > > if [ "$sw" = "0" ]; then > > echo "DO SOMETHING!" > > fi > > sw="0" > > done > > > > I suggest : > > -%><- > > #!/bin/sh > > cat foo.txt | while read LINE1 > do > echo 'One' > $$tmp > cat bar.txt |while read LINE2 > do > if [ "$LINE1" = "$LINE2" ]; then > echo 'ok' > $$tmp > break > fi > done > > if [ `cat $$tmp` = "One" ]; then > echo "One !" > fi > > if [ `cat $$tmp` = "ok" ]; then > echo "ok !" > fi > done Or, to avoid subshell(s) created in pipeline(s), and subsequent loss of variables set in the subshell(s) to their parents, rather than using: cat foo.txt | while read LINE1 [..] cat bar.txt | while read LINE2 [..] done [..] done you can use: while read LINE1 [..] while read LINE2 [..] done < bar.txt [..] done < foo.txt cheers, Ian ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: shell script problem
2012/12/23 Polytropon > > #!/bin/sh > > cat foo.txt | while read LINE1 > do > cat bar.txt | while read LINE2 > do > if [ "$LINE1" = "$LINE2" ]; then > sw="1" > echo "Current value of sw is : " $sw > * ps -l | grep $$ * # see subshell here > break > fi > done > * echo " Process: " $$* # And the parent > echo "Value of sw is : " $sw > if [ "$sw" = "0" ]; then > echo "DO SOMETHING!" > fi > sw="0" > done > I suggest : -%><- #!/bin/sh cat foo.txt | while read LINE1 do echo 'One' > $$tmp cat bar.txt |while read LINE2 do if [ "$LINE1" = "$LINE2" ]; then echo 'ok' > $$tmp break fi done if [ `cat $$tmp` = "One" ]; then echo "One !" fi if [ `cat $$tmp` = "ok" ]; then echo "ok !" fi done Best regards - (°> Dhénin Jean-Jacques / ) 48, rue de la Justice 78300 Poissy ^^ dhe...@gmail.com - ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: shell script problem
2012/12/23 Polytropon > On Sun, 23 Dec 2012 10:34:34 +0100, Polytropon wrote: > > First, the lines with "read" have to be: > > > > cat /foo/bar.txt | while read $LINE1 > > > > cat /foo/bar/foo/bar.txt | while read $LINE2 > > > > Reason: $LINE1 and $LINE2 will be evaluated here, they are "empty > > string", causing "read" to throw an error. > > Excuse me - I made a mistake! Of course those two lines > have to be: > > cat /foo/bar.txt | while read LINE1 > > and > > cat /foo/bar/foo/bar.txt | while read LINE2 > > The $ infront of the variable names have to be removed. > The variable _name_, not its content, has to be provided > to "read" as a parameter. > > The script so far: > > > > #!/bin/sh > > cat foo.txt | while read LINE1 > * echo "Pid Process: " $$* > do > cat bar.txt | while read LINE2 > do > if [ "$LINE1" = "$LINE2" ]; then > sw="1" > echo "Current value of sw is : " $sw > *ps -ax |grep bar * > break > fi > done > echo "Value of sw is : " $sw > if [ "$sw" = "0" ]; then > echo "DO SOMETHING!" > fi > sw="0" > done > Has you can see, "pipe" make a subshell and sw is lost. Hope this help - (°> Dhénin Jean-Jacques / ) 48, rue de la Justice 78300 Poissy ^^ dhe...@gmail.com - ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: shell script problem
On Sun, 23 Dec 2012 09:57:02 + Matthew Seaman wrote: > Hmmm I'd just like to draw your attention to the comm(1) program, > which lets you find lines common to two files, or only in one or other > of a pair of inputs, very easily. The only slight gotcha is that the > input files have to be sorted. For which purpose the sort program is most useful. -- Steve O'Hara-Smith ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: shell script problem
On 23/12/2012 09:43, Polytropon wrote: > On Sun, 23 Dec 2012 10:34:34 +0100, Polytropon wrote: >> First, the lines with "read" have to be: >> >> cat /foo/bar.txt | while read $LINE1 >> >> cat /foo/bar/foo/bar.txt | while read $LINE2 >> >> Reason: $LINE1 and $LINE2 will be evaluated here, they are "empty >> string", causing "read" to throw an error. > > Excuse me - I made a mistake! Of course those two lines > have to be: > > cat /foo/bar.txt | while read LINE1 > > and > > cat /foo/bar/foo/bar.txt | while read LINE2 > > The $ infront of the variable names have to be removed. > The variable _name_, not its content, has to be provided > to "read" as a parameter. > > The script so far: > > > > #!/bin/sh > > cat foo.txt | while read LINE1 > do > cat bar.txt | while read LINE2 > do > if [ "$LINE1" = "$LINE2" ]; then > sw="1" > echo "Current value of sw is : " $sw > break > fi > done > echo "Value of sw is : " $sw > if [ "$sw" = "0" ]; then > echo "DO SOMETHING!" > fi > sw="0" > done > > Hmmm I'd just like to draw your attention to the comm(1) program, which lets you find lines common to two files, or only in one or other of a pair of inputs, very easily. The only slight gotcha is that the input files have to be sorted. Cheers, Matthew -- Dr Matthew J Seaman MA, D.Phil. PGP: http://www.infracaninophile.co.uk/pgpkey signature.asc Description: OpenPGP digital signature
Re: shell script problem
On Sun, 23 Dec 2012 10:34:34 +0100, Polytropon wrote: > First, the lines with "read" have to be: > > cat /foo/bar.txt | while read $LINE1 > > cat /foo/bar/foo/bar.txt | while read $LINE2 > > Reason: $LINE1 and $LINE2 will be evaluated here, they are "empty > string", causing "read" to throw an error. Excuse me - I made a mistake! Of course those two lines have to be: cat /foo/bar.txt | while read LINE1 and cat /foo/bar/foo/bar.txt | while read LINE2 The $ infront of the variable names have to be removed. The variable _name_, not its content, has to be provided to "read" as a parameter. The script so far: #!/bin/sh cat foo.txt | while read LINE1 do cat bar.txt | while read LINE2 do if [ "$LINE1" = "$LINE2" ]; then sw="1" echo "Current value of sw is : " $sw break fi done echo "Value of sw is : " $sw if [ "$sw" = "0" ]; then echo "DO SOMETHING!" fi sw="0" done -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ... ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: shell script problem
On Sun, 23 Dec 2012 01:05:35 -0800 (PST), Jack Mc Lauren wrote: > Hi all > Please take a look at the script below wich I've wrote : > 1- cat /foo/bar.txt | while read $LINE12- do3- cat /foo/bar/foo/bar.txt | > while read $LINE24- do 5- if [ "$LINE1" = "$LINE2" ]; then6- > sw="1"7- echo "Current value of sw is : " $sw8- > break9- fi10- done11- echo "Value of sw is : " $sw12- > if [ "$sw" = "0" ]; then13- DO SOMETHING14- fi15- sw="0"16- > done This is totally distorted! Allow me to re-arrange it. cat /foo/bar.txt | while read $LINE1 do cat /foo/bar/foo/bar.txt | while read $LINE2 do if [ "$LINE1" = "$LINE2" ]; then sw="1" echo "Current value of sw is : " $sw break fi done echo "Value of sw is : " $sw if [ "$sw" = "0" ]; then DO SOMETHING fi sw="0" done First, the lines with "read" have to be: cat /foo/bar.txt | while read $LINE1 cat /foo/bar/foo/bar.txt | while read $LINE2 Reason: $LINE1 and $LINE2 will be evaluated here, they are "empty string", causing "read" to throw an error. > You probebly guessed what I want to do. But the problem is that > when the value of sw sets to 1 (in the first if statement) and > the loop breaks , the value of sw is not '1' anymore in > " echo "Value of sw is : " $sw " !!! > Thanks in advance ... For testing, I've replaced the $sw=0 line with an "echo" command. I've created two files foo.txt and bar.txt for test, both have one line in common (3rd line in my example data). If I run the script, I get this output: Value of sw is :<- after 1st line (uninitialized) Value of sw is : 0 <- after 2nd line DO SOMETHING! Current value of sw is : 1 <- after 3nd line (common entry) Value of sw is : 0 <- after 4th line DO SOMETHING! Value of sw is : 0 <- after 5th line DO SOMETHING! It seems that the condition $LINE1=$LINE2 properly triggers the "current value" echo command, while all non-common lines trigger the "DO SOMETHING" action. If you indended something else, please elaborate. -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ... ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
shell script problem
Hi all Please take a look at the script below wich I've wrote : 1- cat /foo/bar.txt | while read $LINE12- do3- cat /foo/bar/foo/bar.txt | while read $LINE24- do 5- if [ "$LINE1" = "$LINE2" ]; then6- sw="1"7- echo "Current value of sw is : " $sw8- break9- fi10- done11- echo "Value of sw is : " $sw12- if [ "$sw" = "0" ]; then13- DO SOMETHING14- fi15- sw="0"16- done You probebly guessed what I want to do. But the problem is that when the value of sw sets to 1 (in the first if statement) and the loop breaks , the value of sw is not '1' anymore in " echo "Value of sw is : " $sw " !!! Thanks in advance ... ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: space char shell script problem
At 05:19 AM 8/23/2008, David Banning wrote: I am running into a problem with the space character in filenames. For instance, If I want to run the script; for x in `ls` do echo $x done then filenames that have a space in them ie: "john smith.jpg" are processed by my script as two names, "john" and "smith.jpg". What is the best way to deal with this type of space problem in the shell? I know that file names in quotes solves some problems but I can't tranfer that to my script. Depending on what your script is doing, I would use an intermediate file and awk. Something like: ls >/tmp/mytempfile cat /tmp/mytempfile | awk '{ print $0 }' if you are looking for something special add grep to the mix: cat /tmp/mytempfile | awk '{ print $0 }'|grep -i [some name pattern] rm /tmp/mytempfile You can save the results to another temporary file for more processing, or use awk more to create commandlines to execute in another script file such as: cat /tmp/mytempfile | awk '{ print $0 }'|grep -i [some name pattern] | awk '{printf"cp %s /backup/backupdir\n", $0)}' >/tmp/mycopyscript chmod +x /tmp/mycopyscript /tmp/mycopyscript So depending on what your original script was doing, this method may work for you. -Derek -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: space char shell script problem
Polytropon wrote: Well, it's completely possible to create a file name like: This is my *favourite* photo from "Cats" \ by Bob & Jane / my wife ~ 2008 `musical'.JPG Um... actually you cannot create that as a filename on UFS. There are precisely two characters you cannot use in a filename. The first is ascii NULL, which marks end-of-string. The second, which you've seemingly run afoul of, is the directory separator character '/'. Everything else there is just fine and dandy though. Of course, if you name your files in Ugaritic or Cuneiform or Klingon or any of the other less frequently travelled UTF code pages, you'll need to put some work in to make them display correctly in your shell... Cheers, Matthew -- Dr Matthew J Seaman MA, D.Phil. 7 Priory Courtyard Flat 3 PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate Kent, CT11 9PW signature.asc Description: OpenPGP digital signature
Re: space char shell script problem
On Sat, 23 Aug 2008 10:16:36 -0400, Thomas Dickey <[EMAIL PROTECTED]> wrote: > spaces won't go away, and since they're legal in filenames, one may as > well handle them. Well, it's completely possible to create a file name like: This is my *favourite* photo from "Cats" \ by Bob & Jane / my wife ~ 2008 `musical'.JPG What a fun handling this. :-) Call me old fashioned, but I don't mind making things more complicated than it should be. The space character is the command argument separator, as well as / is the root directory and * is "everything". Applications like xmms can even replace the _ by a space when showing the filename of an mp3 file (given that no ID3 tag is provided). So I avoid spaces generally, and when I get files with spaces, I do convert the names automatically. > A script like > > #!/bin/sh > for x in "$@" > do > echo $x > done > > handles quoting nicely enough (for spaces, anyway). ls will translate > some non-printing characters to printable; the 'find' program is a better > alternative if one must derive the list inside the program. That's correct; find can provide file names including paths and can furthermore explude directories from being in the list (-type f). In your script, $x contains the filename with spaces and should be passed as one value to the program called. -- Polytropon >From Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ... ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: space char shell script problem
On Sat, Aug 23, 2008 at 04:09:57PM +0200, Polytropon wrote: > On Sat, 23 Aug 2008 06:19:42 -0400, David Banning <[EMAIL PROTECTED]> wrote: > > I am running into a problem with the space character in filenames. > > For instance, If I want to run the script; > > > > for x in `ls` > > do > > echo $x > > done > > > > then filenames that have a space in them ie: "john smith.jpg" > > are processed by my script as two names, "john" and "smith.jpg". > > > > What is the best way to deal with this type of space problem in the shell? > > The best way is not to use spaces in filenames; underscores perform > their purpose very well without making things more complicated. :-) spaces won't go away, and since they're legal in filenames, one may as well handle them. > To iterate over files, I would not use `ls`, instead, I would let > the shell do the expansion of * for me, as it has already been > suggested. > > Because the file names x iterates contain spaces, be very (!) careful > to assure that the applications you call with these filenames get > the spaces correctly masked, either put the filename in quotes or > substituts " " by "\ ". You would have won nothing when the application > you call with the filename interpretes it as an argument list with > two elements. A script like #!/bin/sh for x in "$@" do echo $x done handles quoting nicely enough (for spaces, anyway). ls will translate some non-printing characters to printable; the 'find' program is a better alternative if one must derive the list inside the program. > for x in *; do > echo "${x}" > done > > Replace the middle line with any program call you want. > > > -- > Polytropon > >From Magdeburg, Germany > Happy FreeBSD user since 4.0 > Andra moi ennepe, Mousa, ... > ___ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "[EMAIL PROTECTED]" -- Thomas E. Dickey http://invisible-island.net ftp://invisible-island.net ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: space char shell script problem
On Sat, 23 Aug 2008 06:19:42 -0400, David Banning <[EMAIL PROTECTED]> wrote: > I am running into a problem with the space character in filenames. > For instance, If I want to run the script; > > for x in `ls` > do > echo $x > done > > then filenames that have a space in them ie: "john smith.jpg" > are processed by my script as two names, "john" and "smith.jpg". > > What is the best way to deal with this type of space problem in the shell? The best way is not to use spaces in filenames; underscores perform their purpose very well without making things more complicated. :-) To iterate over files, I would not use `ls`, instead, I would let the shell do the expansion of * for me, as it has already been suggested. Because the file names x iterates contain spaces, be very (!) careful to assure that the applications you call with these filenames get the spaces correctly masked, either put the filename in quotes or substituts " " by "\ ". You would have won nothing when the application you call with the filename interpretes it as an argument list with two elements. for x in *; do echo "${x}" done Replace the middle line with any program call you want. -- Polytropon >From Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ... ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: space char shell script problem
RW writes: > > I am running into a problem with the space character in filenames. > > For instance, If I want to run the script; > > > > for x in `ls` > > do > > echo $x > > done > > for x in * There's the (poorly documented, IMO) IFS (internal field separator) shell variable. It's a string, normally set to space and tab; set it to newline and good things can happen. Robert Huff ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: space char shell script problem
On Sat, 23 Aug 2008 06:19:42 -0400 David Banning <[EMAIL PROTECTED]> wrote: > I am running into a problem with the space character in filenames. > For instance, If I want to run the script; > > for x in `ls` > do > echo $x > done for x in * ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
space char shell script problem
I am running into a problem with the space character in filenames. For instance, If I want to run the script; for x in `ls` do echo $x done then filenames that have a space in them ie: "john smith.jpg" are processed by my script as two names, "john" and "smith.jpg". What is the best way to deal with this type of space problem in the shell? I know that file names in quotes solves some problems but I can't tranfer that to my script. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: OT: shell script problem
On Thu, Feb 20, 2003 at 03:14:22PM -0600, Brian Henning wrote: > i have this script with one input file and i keep getting the error: > /files_???/19980527/???/myname # sh doc_id.sh input.txt > /files_???/19980527/???/: No such file or directory > /files_???/19980527/???/ > > sh doc_id.sh > - > #! /bin/sh -x > > INPUT=$1 > > for i in `cat $INPUT`; do > echo "ls -1 ${i}" > RES=`ls -1 ${i}` > done > exit 0; > > > input.txt > > /files_???/19980527/???/ > > but when i run the command ls /files_???/19980527/???/ the files are > found... > > any suggestions? Yes, change cat to echo. Have fun! Greg -- Grzegorz Czaplinski <[EMAIL PROTECTED]> "The Power to Serve, Right for the Power Users!" - http://www.FreeBSD.org/ Fingerprint: EB77 E19D CFA2 5736 810F 847C A70F A275 2489 469F msg20208/pgp0.pgp Description: PGP signature
OT: shell script problem
i have this script with one input file and i keep getting the error: /files_???/19980527/???/myname # sh doc_id.sh input.txt /files_???/19980527/???/: No such file or directory /files_???/19980527/???/ sh doc_id.sh - #! /bin/sh -x INPUT=$1 for i in `cat $INPUT`; do echo "ls -1 ${i}" RES=`ls -1 ${i}` done exit 0; input.txt /files_???/19980527/???/ but when i run the command ls /files_???/19980527/???/ the files are found... any suggestions? brian To Unsubscribe: send mail to [EMAIL PROTECTED] with "unsubscribe freebsd-questions" in the body of the message