Re: [opensuse] bash - why doesn't the tldp bash beginners guide for loop example work? Anders Johansson wrote: > On Thursday 14 February 2008 05:41:23 David C. Rankin wrote: >> Listmates, >> >> 	I have run into a bash problem with a simple for loop I don't >> understand and would like input from the gurus to determine if the tldp >> documentation is in error or if it is me. Safe money says it's me, but >> here is the situation. >> >> 	I am using example 9.1.2.2. from the following link to construct a >> simple for loop to extract a directory full of zip files: >> >> http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_01.html >> >> 	The example give is: >> >> [nezyq@octarine ~/html] cat html2php.sh >> #!/bin/bash >> # specific conversion script for my html files to php >> LIST="$(ls *.html)" >> for i in "$LIST"; do >>       NEWNAME=$(ls "$i" | sed -e 's/html/php/') >>       cat beginfile > "$NEWNAME" >>       cat "$i" | sed -e '1,25d' | tac | sed -e '1,21d'| tac >> "$NEWNAME" >>       cat endfile >> "$NEWNAME" >> done >  > This is actually in a tutorial? Wow >  > for i in *; do >    NEWNAME=${i%html}.php >    echo beginfile > "$NEWNAME" >    sed -e '1,25d......... >  >  >> 	So I created the following: >> >> #!/bin/bash >> LIST="$(ls *.zip)" >> for i in "$LIST"; do >>          unzip $i -d >> /mnt/nemesis-cfg/home/samba/computer/software/fonts/extract >>          echo "Unzipped: $i to ../extract" >> done >> exit 0 >  > for i in *.zip; do >    unzip "$i" -d /mnt/nemesis-cfg/home/samba/computer/software/fonts/extract > done >  > You want to have the quotes around $i to guard against spaces in file names >  > Anders Anders, 	I found the quotes around "$i" do not work for managing filenames with  spaces. The filenames, spaces and all, are pulled in by the LIST="$(ls  *.zip)". Quoted or unquoted, it makes no difference, because $i is just  the next element in the list. Example: goodfile 001.jpg exists in the  list as: goodfile 001.jpg 	for i $list; do 		echo "i = $1" 	done gives: i = goodfile i = 001.jpg regardless. 	Stumbling around, it seems if you include AFS='\\n' it will keep the  loop from breaking on spaces by specifying that it only break on  newlines and not spaces. So filenames with spaces (all my wife's digital  pictures are) this is the only thing I have found to work without  extensive character and replacing spaces with '_': 	AFS='\\n' 	for i $list; do 	Telling the loop not to break on spaces with AFS seems to work like a  charm. Are there any better ways? 		echo "i = $1" 	done --  David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com --  >From lcvbdk@stonehenge.com  Sat Apr 26 15:49:32 2008 To: "Chas. Owens"  Cc: soiofdvxg@perl.org,  "\\"Rodrick Brown\\""  Subject: Re: variable help From: lcvbdk@stonehenge.com (Randal L. Schwartz) Date: Tue, 04 Mar 2008 13:51:08 -0800 Message-ID: <86r6eqqhtf.fsf@blue.stonehenge.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii >>>>> "Chas" == Chas Owens  writes: Chas> I cannot disagree with this sentiment more.  Bad answers offer the Chas> chance to teach more than one person at a time.  People should not be Chas> afraid to post answers to this list; however, it is a good idea to Chas> test your answers before posting. OK, then at least *TEST*.  Thank you. --  Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095   Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! --cennjmffhdndjplemlim Content-Type: message/rfc822 Content-Disposition: inline; filename="beginners_98994.ezm" >From esiphi.azkpp@pa-sport.com  Sat Apr 26 15:49:32 2008 Subject: RE: variable help MIME-Version: 1.0 Content-Type: text/plain; 	charset="us-ascii" Content-Transfer-Encoding: quoted-printable Date: Tue, 4 Mar 2008 21:59:30 -0000 Message-ID:  From: "Andrew Curry"  To: , 	 True enough however please don't be so rude, people should be free to post the answers they think if only to be corrected and learn, and not be scared off by know by flippant replies. You are correct it will return a false positive as will $x=3D~/^[0-9]*$/ for that matter. But its an ultra simple solution for a simple question I don't know if the person is loading from a file etc... Even perldoc -q "whether a scalar is a number" states Assuming that you don't care about IEEE notations like "NaN"      or "Infinity", you probably just want to use a regular      expression.         if (/\\D/)            { print "has nondigits\\n" }         if (/^\\d+$/)         { print "is a whole number\\n" }         if (/^-?\\d+$/)       { print "is an integer\\n" }         if (/^[+-]?\\d+$/)    { print "is a +/- integer\\n" }         if (/^-?\\d+\\.?\\d*$/) { print "is a real number\\n" }         if (/^-?(?:\\d+(?:\\.\\d*)?|\\.\\d+)$/) { print "is a decimal number\\n" }         if (/^([+-]?)(?=3D\\d|\\.\\d)\\d*(\\.\\d*)?([Ee]([+-]?\\d+))?$/)                              { print "a C float\\n" } Yes you can use Scalar::Util but most people just want a simple test. -----Original Message----- From: lcvbdk@stonehenge.com [mailto:lcvbdk@stonehenge.com]=20 Sent: 04 March 2008 21:45 To: soiofdvxg@perl.org Subject: Re: variable help >>>>> ""Andrew" =3D=3D "Andrew Curry"  = writes: "Andrew> If you are not 100% sure then you can also do if ($x = =3D~/^\\d*$/) for an "Andrew> actual integer i.e. a number in the positive whole set of numbers { "Andrew> 1,2,3....}  or negative whole numbers -1,-2,-3.... or 0.=20 This also fails, declaring "123\\n" as a match, and thus being "an integer".  False positive. Please check your answers before answering on beginners next time. --=20 Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095   Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! --=20 To unsubscribe, e-mail: piqgklqwx-cmtsrlfpiqg@perl.org For additional commands, e-mail: ccorkgnfl-lqzv@perl.org http://learn.perl.org/ This e-mail is from the PA Group.  For more information, see = www.thepagroup.com. This e-mail may contain confidential information. =20 Only the addressee is permitted to read, copy, distribute or otherwise = use this email or any attachments. =20 If you have received it in error, please contact the sender immediately. = =20 Any opinion expressed in this e-mail is personal to the sender and may = not reflect the opinion of the PA Group. Any e-mail reply to this address may be subject to interception or = monitoring for operational reasons or for lawful business practices. --cennjmffhdndjplemlim Content-Type: message/rfc822 Content-Disposition: inline; filename="beginners_98995.ezm" >From ahlg.rwhsv@gmail.com  Sat Apr 26 15:49:32 2008 Message-ID: <58ce48dc0803041409l38d31622x4116198fa36386f4@mail.gmail.com> Date: Tue, 4 Mar 2008 17:09:11 -0500 From: "Chas. Owens"  To: "Randal L. Schwartz"  Subject: Re: variable help Cc: soiofdvxg@perl.org MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Tue, Mar 4, 2008 at 4:44 PM, Randal L. Schwartz  wrote: > >>>>> ""Andrew" == "Andrew Curry"  writes: > >  "Andrew> If you are not 100% sure then you can also do if ($x =~/^\\d*$/) for an >  "Andrew> actual integer i.e. a number in the positive whole set of numbers { >  "Andrew> 1,2,3....}  or negative whole numbers -1,-2,-3.... or 0. > >  This also fails, declaring "123\\n" as a match, and thus being >  "an integer".  False positive. > >  Please check your answers before answering on beginners next time. snip That depends on what the current definition of integer is.  If by integer the original poster meant treated like an integer by Perl then "123\\n" can be seen as an integer.  Of course, in that case "123foo" is also an integer and would fail the check (false negative).  In any case, it is important to be very clear in what you mean, and the best way to be clear is to use a regex like /\\A[0-9]+\\z/ or a pseudo-regex like "composed of only the characters 0 through 9 one or more times". --  Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. --cennjmffhdndjplemlim Content-Type: message/rfc822 Content-Disposition: inline; filename="beginners_98998.ezm" >From zuroeq@users.sourceforge.net  Sat Apr 26 15:49:32 2008 Message-ID:  Date: Tue, 4 Mar 2008 18:14:18 -0500 From: yitzle  To: "Chas. Owens"  Subject: Re: variable help Cc: "Randal L. Schwartz" , soiofdvxg@perl.org MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Andrew has got a good point. The perldoc page says:    if (/^\\d+$/) { print "is a whole number\\n" } which would indicate that the author of the perldoc page believes "123\\n" should classify as a number, while "123foo" shouldn't, even though Perl sometimes treats "123foo" as a number. I was going to write that I think it makes sense to consider "123\\n" a number and "123foo" not a number, but I suppose it does depend on each person's situation, definition and requirements. I guess that makes the perldoc page somewhat ambiguous. --cennjmffhdndjplemlim Content-Type: message/rfc822 Content-Disposition: inline; filename="beginners_98999.ezm" >From hfcog+qhuo@isolution.nl  Sat Apr 26 15:49:32 2008 Message-ID: <20080304233111.23233.qmail@lists.develooper.com> To: soiofdvxg@perl.org From: "Dr.Ruud"  Subject: Re: variable help Date: Wed, 5 Mar 2008 00:27:55 +0100 MIME-Version: 1.0 Content-Type: text/plain; 	charset="iso-8859-1" Content-Transfer-Encoding: 7bit godql.lmebe@t-systems.com schreef: > How to find out the specific variable contains integer value. In my > script ,variable is storing some value but I want to find out whether > that value is string or integer. In Perl, data types are like scalar, array, hash. Operator types are like numeric, string. $ perl -wle '$x="7.5e2"; print $x; print 0+$x; print "0"+$x' 7.5e2 750 750 --  Affijn, Ruud "Gewoon is een tijger." --cennjmffhdndjplemlim Content-Type: message/rfc822 Content-Disposition: inline; filename="beginners_99000.ezm" >From kejhtdbhqaeky@intergate.com  Sat Apr 26 15:49:33 2008 Date: Tue, 04 Mar 2008 20:06:35 -0500 From: MK  Subject: Re: variable help To: soiofdvxg@perl.org Message-Id: <1204679195l.2614l.0l@akashiraffee.enthusiasm> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; DelSp=Yes; Format=Flowed Content-Transfer-Encoding: quoted-printable On 03/04/2008 03:41:44 PM, yitzle wrote: -> I'd use a RegEx and test to see if the string is made up entirely of -> integers. ->   print "The variable containing $p is an interger\\n" if ($p =3D~ -> /^[0-9]+$/); yitzle would seem to have the most foolproof solution.  The only =20 problem would be if $p=3D"+1" which i would consider +1 an integer. incidentally /^\\d+$/ amounts to the same thing. --cennjmffhdndjplemlim Content-Type: message/rfc822 Content-Disposition: inline; filename="beginners_99001.ezm" >From ahlg.rwhsv@gmail.com  Sat Apr 26 15:49:33 2008 Message-ID: <58ce48dc0803041729o3e331fabj80762395e1896cfe@mail.gmail.com> Date: Tue, 4 Mar 2008 20:29:20 -0500 From: "Chas. Owens"  To: MK  Subject: Re: variable help Cc: soiofdvxg@perl.org MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Tue, Mar 4, 2008 at 8:06 PM, MK  wrote: > On 03/04/2008 03:41:44 PM, yitzle wrote: > >  -> I'd use a RegEx and test to see if the string is made up entirely of > > -> integers. >  ->   print "The variable containing $p is an interger\\n" if ($p =~ >  -> /^[0-9]+$/); > >  yitzle would seem to have the most foolproof solution.  The only >  problem would be if $p="+1" which i would consider +1 an integer. > >  incidentally /^\\d+$/ amounts to the same thing. snip Nope, due to addition of Unicode support in recent versions of Perl it will also match "\\x{1814}" the Mongolian digit 4.  The \\d character class is not the same as [0-9], it matches all number characters, including those in other scripts.  If you want to only 0,1,2,3,4,5,6,7,8,9 you need to say [0-9]. perl -le 'print "\\x{1814}" =~ /\\A\\d+\\z/ ? "t" : "f"' --  Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. --cennjmffhdndjplemlim Content-Type: message/rfc822 Content-Disposition: inline; filename="beginners_99002.ezm" >From zuroeq@users.sourceforge.net  Sat Apr 26 15:49:33 2008 Message-ID:  Date: Tue, 4 Mar 2008 22:23:15 -0500 From: yitzle  To: "Chas. Owens"  Subject: Re: variable help Cc: MK , soiofdvxg@perl.org MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Tue, Mar 4, 2008 at 8:29 PM, Chas. Owens  wrote: >  Nope, due to addition of Unicode support in recent versions of Perl it >  will also match "\\x{1814}" the Mongolian digit 4.  The \\d character >  class is not the same as [0-9], it matches all number characters, >  including those in other scripts.  If you want to only >  0,1,2,3,4,5,6,7,8,9 you need to say [0-9]. > >  perl -le 'print "\\x{1814}" =~ /\\A\\d+\\z/ ? "t" : "f"' >  -- >  Chas. Owens More interesting, at least for me, would be   perl -le 'print "\\x{1814}"' What do I need to make that print out "properly"? --cennjmffhdndjplemlim Content-Type: message/rfc822 Content-Disposition: inline; filename="beginners_99003.ezm" >From ahlg.rwhsv@gmail.com  Sat Apr 26 15:49:33 2008 Message-ID: <58ce48dc0803041935w64ebefc1rf5d1e180081714bc@mail.gmail.com> Date: Tue, 4 Mar 2008 22:35:55 -0500 From: "Chas. Owens"  To: yitzle  Subject: Re: variable help Cc: MK , soiofdvxg@perl.org MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Tue, Mar 4, 2008 at 10:23 PM, yitzle  wrote: > On Tue, Mar 4, 2008 at 8:29 PM, Chas. Owens  wrote: >  >  Nope, due to addition of Unicode support in recent versions of Perl it >  >  will also match "\\x{1814}" the Mongolian digit 4.  The \\d character >  >  class is not the same as [0-9], it matches all number characters, >  >  including those in other scripts.  If you want to only >  >  0,1,2,3,4,5,6,7,8,9 you need to say [0-9]. >  > >  >  perl -le 'print "\\x{1814}" =~ /\\A\\d+\\z/ ? "t" : "f"' >  >  -- >  >  Chas. Owens > >  More interesting, at least for me, would be >   perl -le 'print "\\x{1814}"' >  What do I need to make that print out "properly"? A terminal that can handle UTF-8. You may need to put this in your profile #fix UTF-8 support export LC_CTYPE=en_US.UTF-8 #vim needs this to swtich it from Latin1 to UTF-8 export PERL_UNICODE=SDL     #Makes Perl use UTF-8 for IO You might also need the right fonts.  Try this instead (I think CIRCLED DIGIT ONE is more commonly supported): perl -le 'print "\\x{2460}"' --  Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. --cennjmffhdndjplemlim Content-Type: message/rfc822 Content-Disposition: inline; filename="beginners_99004.ezm" >From zuroeq@users.sourceforge.net  Sat Apr 26 15:49:33 2008 Message-ID:  Date: Tue, 4 Mar 2008 23:18:25 -0500 From: yitzle  To: "Chas. Owens"  Subject: Re: variable help Cc: MK , soiofdvxg@perl.org MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Tue, Mar 4, 2008 at 10:35 PM, Chas. Owens  wrote: >  A terminal that can handle UTF-8. > >  You may need to put this in your profile >  #fix UTF-8 support >  export LC_CTYPE=en_US.UTF-8 #vim needs this to swtich it from Latin1 to UTF-8 >  export PERL_UNICODE=SDL     #Makes Perl use UTF-8 for IO > >  You might also need the right fonts.  Try this instead (I think >  CIRCLED DIGIT ONE is more commonly supported): > >  perl -le 'print "\\x{2460}"' The CIRCLED DIGIT ONE worked without the exports. The "Mongolian digit 4" gives me a square even with the exports. Interesting... Thanks!