Re: .sh script code to determine IPv4 or IPv6
I have some scripts that do fairly crude IPv4/6 validation testing. It is generally assumed that the input is coming from someone who knows what they are doing, but even the best of us have fat fingers sometimes :) Having standardized routines for something like this is great! Thanks, -Markham On 13-08-06 11:45 AM, Teske, Devin wrote: > On Aug 6, 2013, at 10:39 AM, Polytropon wrote: > >> On Tue, 6 Aug 2013 16:50:37 +, Teske, Devin wrote: >>> And yes... to clarify... the port is a mirror of what's in 9.x base. >>> (however, see my recent notes in a separate reply; TL;DR: port is >>> 9.x only; proceed only if you know you don't care about the dialog(1) >>> aspects of the library code). >> I think it should be relatively unproblematic to fetch the >> port and only use the subroutines "as is", even if it's just >> for educational purposes. :-) >> > Right. > > Just a warning though, what is "fetched" in ports is actually in the format > of what's in HEAD (read: not in the format of what gets installed). > > For example, there are things that end up in /usr/share/bsdconfig that aren't > in the "bsdconfig/share/" source directory (e.g., all the stuff under > /usr/share/bsdconfig/networking is under the source directory > "bsdconfig/networking/share"). This may be counter-intuitive from an > "exploratory" view if looking at the source directory (what's fetched by > ports). > > And since the port Makefile will prevent you from turning that fetch'ed > source directory into an installed software (putting things where they end > up), it might be easier to grab this pre-built package that I stashed... > > http://druidbsd.sf.net/download/bsdconfig/bsdconfig-0.9.0.tbz > > Because then you can say "pkg_add" and everything will be in the right place > (/usr/share/bsdconfig/ will be flush with everything and you won't have to > hunt-and-peck through the source with a "maintainers" view). ___ 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: .sh script code to determine IPv4 or IPv6
On Aug 6, 2013, at 10:39 AM, Polytropon wrote: > On Tue, 6 Aug 2013 16:50:37 +, Teske, Devin wrote: >> And yes... to clarify... the port is a mirror of what's in 9.x base. >> (however, see my recent notes in a separate reply; TL;DR: port is >> 9.x only; proceed only if you know you don't care about the dialog(1) >> aspects of the library code). > > I think it should be relatively unproblematic to fetch the > port and only use the subroutines "as is", even if it's just > for educational purposes. :-) > Right. Just a warning though, what is "fetched" in ports is actually in the format of what's in HEAD (read: not in the format of what gets installed). For example, there are things that end up in /usr/share/bsdconfig that aren't in the "bsdconfig/share/" source directory (e.g., all the stuff under /usr/share/bsdconfig/networking is under the source directory "bsdconfig/networking/share"). This may be counter-intuitive from an "exploratory" view if looking at the source directory (what's fetched by ports). And since the port Makefile will prevent you from turning that fetch'ed source directory into an installed software (putting things where they end up), it might be easier to grab this pre-built package that I stashed... http://druidbsd.sf.net/download/bsdconfig/bsdconfig-0.9.0.tbz Because then you can say "pkg_add" and everything will be in the right place (/usr/share/bsdconfig/ will be flush with everything and you won't have to hunt-and-peck through the source with a "maintainers" view). -- Devin _ The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you. ___ 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: .sh script code to determine IPv4 or IPv6
On Tue, 6 Aug 2013 16:50:37 +, Teske, Devin wrote: > And yes... to clarify... the port is a mirror of what's in 9.x base. > (however, see my recent notes in a separate reply; TL;DR: port is > 9.x only; proceed only if you know you don't care about the dialog(1) > aspects of the library code). I think it should be relatively unproblematic to fetch the port and only use the subroutines "as is", even if it's just for educational purposes. :-) -- 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: .sh script code to determine IPv4 or IPv6
On Aug 6, 2013, at 9:43 AM, Polytropon wrote: > On Tue, 06 Aug 2013 10:20:05 -0600, markham breitbach wrote: >> On 13-08-03 8:04 AM, Teske, Devin wrote: >>> Actually, there's /usr/share/bsdconfig/media/tcpip.subr >>> >>> >> I don't seem to have that (FreeBSD 8.3-RELEASE). >> Where would I get that from? > > Maybe from sysutils/bsdconfig in the ports collection? > I have not checked if this specific subroutine file is > part of the port... > Ah, Polytropon beat me ;D And yes... to clarify... the port is a mirror of what's in 9.x base. (however, see my recent notes in a separate reply; TL;DR: port is 9.x only; proceed only if you know you don't care about the dialog(1) aspects of the library code). -- Devin > > -- > Polytropon > Magdeburg, Germany > Happy FreeBSD user since 4.0 > Andra moi ennepe, Mousa, ... _ The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you. ___ 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: .sh script code to determine IPv4 or IPv6
On Aug 6, 2013, at 9:20 AM, markham breitbach wrote: > On 13-08-03 8:04 AM, Teske, Devin wrote: >> Actually, there's /usr/share/bsdconfig/media/tcpip.subr >> >> > I don't seem to have that (FreeBSD 8.3-RELEASE). Where would I get that > from? > > It's in up-coming 9.2-R (and present 9.2-* snapshots leading up to 9.2-R). You can snatch a copy of the code by installing sysutils/bsdconfig from the ports tree. However, the port is marked (correctly-so) as requiring FreeBSD 9.0 or higher. But don't let that stop you... the only reason it's marked as requiring 9.0 is because 9.0 brings in a new dialog(1) implementation. However, if you're interested in the TCP validation code... that will work on any release. It's only the dialog(1) stuff that won't work on 8.x or older. Luckily, it'll be pretty easy to avoid the land-mines. All functions starting with f_dialog_* should be avoided on 8.x or older. So here's the latest package to download (in case you're unsuccessful in getting the port to behave -- afterall, it may just balk at you for not running 9.x): fetch http://druidbsd.sourceforge.net/download/bsdconfig/bsdconfig-0.9.0.tbz That's a FreeBSD package. You can download it and say (as root): pkg_add bsdconfig-0.9.0.tbz Just be forewarned (again), on 8.x or older, executing "bsdconfig" will have widely unexpected results (it won't eat your homework, but it may or may not actually *run*). However, doing the above 2-step (fetch & pkg_add) will bring in the files you're looking for and give you the functionality you're wanting on 8.x. -- Devin P.S. I really *can't* make the dialog(1) stuff backward compatible with 8.x's (or any older's) version of dialog(1). The new `cdialog' variant that was brought into 9.x to replace the aging dialog(1)/libdialog pair is has a *lot* of functionality that I depend on. It could potentially take months to make bsdconfig *fully* backward compatible with 8.x. For now, it's safe if you just use the libraries and avoid any/all functions beginning with "f_dialog_". _ The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you. ___ 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: .sh script code to determine IPv4 or IPv6
On Tue, 06 Aug 2013 10:20:05 -0600, markham breitbach wrote: > On 13-08-03 8:04 AM, Teske, Devin wrote: > > Actually, there's /usr/share/bsdconfig/media/tcpip.subr > > > > > I don't seem to have that (FreeBSD 8.3-RELEASE). > Where would I get that from? Maybe from sysutils/bsdconfig in the ports collection? I have not checked if this specific subroutine file is part of the port... -- 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: .sh script code to determine IPv4 or IPv6
On 13-08-03 8:04 AM, Teske, Devin wrote: > Actually, there's /usr/share/bsdconfig/media/tcpip.subr > > I don't seem to have that (FreeBSD 8.3-RELEASE). Where would I get that from? ___ 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: .sh script code to determine IPv4 or IPv6
On Aug 3, 2013, at 5:04 AM, Robert Huff wrote: > > Fbsd8 writes: > >> I have a .sh script that I need to determine if the entered IP >> address is IPv4 or IPv6. >> >> Is there some .sh command that does this? > > Not that I know of. > But ... how hard can it be to figure out whether it uses '.' or > ':'? > Actually, there's /usr/share/bsdconfig/media/tcpip.subr Function family: f_validate_ipaddr6 $ipv6_addr # Should be complete; I digested multiple RFCs on IPv6 f_validate_ipaddr $ipv4_addr [$netmask] # optional netmask to validate IP is within doubly-valid f_validate_hostname $hostname # To RFC specifications 952 and 1123 But if you need to prompt the user to enter a value and then validate it, the above functions return meaningful exit status for determining what's wrong with their entry (why did it fail specification, for example). To help decode the exit status, the functions you want to use are: # In /usr/share/bsdconfig/networking/ipaddr.subr Function family: f_dialog_iperror $status $ipv4_addr f_dialog_ip6error $status $ipv6_addr As is implied with the "_dialog_" in their name, they take the $? exit status from the previously mentioned f_validate_*() functions and display a dialog(1) error appropriate to what's wrong. For example, you might see: ERROR! One or more individual octets within the IPv4 address\n(separated by dots) contains one or more invalid characters.\nOctets must contain only the characters 0-9.\n\nInvalid IP Address: %s or ERROR! The IP address entered has either too few (less than 3), too\nmany (more than 8), or not enough segments, separated by colons.\n\nInvalid IPv6 Address: %s And then, in the same function family above (as the *ip[6]error()): f_dialog_vaildate_ipaddr $ipv4_addr f_dialog_validate_ipaddr6 $ipv6_addr These are like: f_validate_ipaddr $ipv4_addr f_validate_ipaddr6 $ipv6_addr Except as implied by the extra "_dialog_" in their name, they will actually run f_validate_* and then f_dialog_ip[6]error() for you with the result. Finally, last, but not least... The process of actually *getting* the values has been simplified too. In the same family function (as f_dialog_ip[6]error and f_dialog_validate_ipaddr[6]()) is: f_dialog_input_ipaddr $interface $ipaddr # $interface is displayed in the prompt text # $ipaddr is used as default text in the input box If user doesn't press escape or select cancel, $ipaddr will hold the users entry. This function validates, displays errors, and is an all-around solution if you need to prompt the user to enter the info and only proceed if they enter a valid entry (the above function is IPv4 centric and supports CIDR notation). The IPv6 version of the latter (f_dialog_input_ipaddr6) does not yet exist. I'm getting there. For now, if you need to prompt for an entry that could be IPv6, use the generic f_dialog_input() routine and sanitize it with the aforementioned API. -- Cheers, Devin _ The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you. ___ 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: .sh script code to determine IPv4 or IPv6
On Aug 3, 2013, at 4:30 AM, Fbsd8 wrote: > I have a .sh script that I need to determine if the entered IP address > is IPv4 or IPv6. > > Is there some .sh command that does this? > In RELENG_9, soon to be released 9.2-R: === FILE: wis === #!/bin/sh DEVICE_SELF_SCAN_ALL= . /usr/share/bsdconfig/media/tcpip.subr if f_validate_ipaddr6 "$1"; then echo "Hey, nice IPv6 addr, great job!" elif f_validate_ipaddr "$1"; then echo "Hey, nice IPv4 addr; smiles" elif f_validate_hostname "$1"; then echo "Hey, nice hostname" else echo "What on Earth wast, _that_?!" exit 1 fi === END FILE === dte...@scribe9.vicor.com ~ $ ./wis ::1 Hey, nice IPv6 addr, great job! dte...@scribe9.vicor.com ~ $ ./wis 0::1 Hey, nice IPv6 addr, great job! dte...@scribe9.vicor.com ~ $ ./wis 0:::1 What on Earth wast, _that_?! dte...@scribe9.vicor.com ~ $ ./wis 1.2.3.4 Hey, nice IPv4 addr; smiles dte...@scribe9.vicor.com ~ $ ./wis 0.2.3.4 Hey, nice IPv4 addr; smiles dte...@scribe9.vicor.com ~ $ ./wis 256.2.3.4 Hey, nice hostname dte...@scribe9.vicor.com ~ $ ./wis foo.bar.com Hey, nice hostname dte...@scribe9.vicor.com ~ $ ./wis abc-123 Hey, nice hostname dte...@scribe9.vicor.com ~ $ ./wis abc_123 What on Earth wast, _that_?! -- Cheers, Devin _ The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you. ___ 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"
.sh script code to determine IPv4 or IPv6
Fbsd8 writes: > I have a .sh script that I need to determine if the entered IP > address is IPv4 or IPv6. > > Is there some .sh command that does this? Not that I know of. But ... how hard can it be to figure out whether it uses '.' or ':'? Robert Huff ___ 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"
.sh script code to determine IPv4 or IPv6
I have a .sh script that I need to determine if the entered IP address is IPv4 or IPv6. Is there some .sh command that does this? Thanks ___ 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: check variable content size in sh script
I say this from a FreeBSD context. It may entirely be possible that a Linux distro uses bash in /bin/sh Yes. For most (all?) linux distros as well as osx, /bin/sh is actually bash. When I say "emulation mode" I mean running a script with a "#!/bin/sh" header on a system that doesn't have a real copy of sh. Whatever shell ends up running the script is effectively emulating sh's environment, at least in my mind. Bash is well known for not complaining when you use bash-specific features in a script which uses a "#!/bin/sh" header. This trips up many a programmer and causes script failures on systems where sh is not actually bash in disguise. This is why I question some things as to whether they're *really* valid pure sh syntax and not something that just happens to work in whatever shell is pretending to be sh (which I thought was tcsh on this machine I just did that test on, but on second look maybe not). __ it has a certain smooth-brained appeal ___ 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: check variable content size in sh script
On May 18, 2013, at 5:06 PM, Teske, Devin wrote: > > On May 18, 2013, at 4:54 PM, Quartz wrote: > >> >>> #foo works with sh >> >> Is it actually part of the official spec though is what I'm wondering, or is >> it a case of other shells not rejecting 'advanced' statements when running >> in emulation. >> > > Shells don't have an "emulation mode". The shell supports what it supports, > and no shell that I've ever used had an "emulation mode" to act like another > shell. > I say this from a FreeBSD context. It may entirely be possible that a Linux distro uses bash in /bin/sh -- and it's entirely possible that bash may act differently if ARGV[0] is /bin/sh. But I wouldn't call this emulation. I'd call it standardization. When bash is invoked with an ARGV[0] of bash or "{anything}/bash", it will act "more like bash" and "less like standardized bourne shell, aka POSIX compliant /bin/sh" (notice I didn't insert the name of any other shell, like ash, dash, etc. but instead I said "like [a] standardized bourne shell". That is to say that bash will more strictly adhere to POSIX bourne shell standards when ARGV[0] is /bin/sh versus "bash". Even when bash clamps down on the bash-specific features if/when ARGV[0] is /bin/sh… you still have access to constructs such as ${#foo}. All that being said… csh/tcsh has no such "standards comliancy mode" (what you called emulation). In fact… csh/tcsh don't follow the POSIX standards (or if it does… *extremely* loosely; see "handling of file descriptors" below in replied-to text). -- Devin > Maybe you're referring to as "emulation" is actually the invocation line of > the shell script. > > Make no mistake… when you change the invocation (first) line of a shell > script from: > > #/bin/sh > > to: > > #/bin/tcsh > > You are not instructing a shell to "emulate" anything, you are actually using > a different shell. > > sh != tcsh != bash != ash != dash != zsh > > Your script will use the shell that is written in the innovation line and the > features you get are respective to which shell you choose. > > > >> >>> At least FreeBSD's implementation of sh (which is ash, I think) >>> supports the # functionality. >> >> The reason I say all this is that my copy of tcsh (on this not-freebsd >> machine) *doesn't* work with this when in sh emulation. >> > > Get the idea that "csh" or "tcsh" are *anything* like sh out of your mind. > > Further, you almost *never* want to do any serious shell programming in csh > or tcsh. > > Why? Because csh and tcsh have an incomplete programming spec. Most notably > are the way that it handles pipe data and the standard-output/error file > descriptors. Specifically, you cannot throw away stdout while keeping stderr. > This short-coming may not be noticeable to all programmers that choose > csh/tcsh, but if you want to do any serious programming, you'll eventually > hit those limitations and be forced to move to a real shell (real in the > sense that it has a complete programming specification). > > I personally never recommend csh/tcsh as a scripting language… but I can see > the benefit that certain constructs (like "repeat N cmd") have, purely for > their simplicity (and readability for *very* short scripts). To challenge > myself on this topic, I routinely try and cross-port very complex shell > scripts to csh, and while I can do math with "let" and I can other things > that a *normal* scripting language should allow… I invariably end up running > away screaming in frustration. > > Again, > > csh != tcsh != sh != bash != ash != dash != zsh > -- > Devin > > _ > The information contained in this message is proprietary and/or confidential. > If you are not the intended recipient, please: (i) delete the message and all > copies; (ii) do not disclose, distribute or use the message in any manner; > and (iii) notify the sender immediately. In addition, please be aware that > any message addressed to our domain is subject to archiving and review by > persons other than the intended recipient. Thank you. > ___ > 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" _ The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "fre
Re: check variable content size in sh script
On May 18, 2013, at 4:54 PM, Quartz wrote: > >> #foo works with sh > > Is it actually part of the official spec though is what I'm wondering, or is > it a case of other shells not rejecting 'advanced' statements when running in > emulation. > Shells don't have an "emulation mode". The shell supports what it supports, and no shell that I've ever used had an "emulation mode" to act like another shell. Maybe you're referring to as "emulation" is actually the invocation line of the shell script. Make no mistake… when you change the invocation (first) line of a shell script from: #/bin/sh to: #/bin/tcsh You are not instructing a shell to "emulate" anything, you are actually using a different shell. sh != tcsh != bash != ash != dash != zsh Your script will use the shell that is written in the innovation line and the features you get are respective to which shell you choose. > >> At least FreeBSD's implementation of sh (which is ash, I think) >> supports the # functionality. > > The reason I say all this is that my copy of tcsh (on this not-freebsd > machine) *doesn't* work with this when in sh emulation. > Get the idea that "csh" or "tcsh" are *anything* like sh out of your mind. Further, you almost *never* want to do any serious shell programming in csh or tcsh. Why? Because csh and tcsh have an incomplete programming spec. Most notably are the way that it handles pipe data and the standard-output/error file descriptors. Specifically, you cannot throw away stdout while keeping stderr. This short-coming may not be noticeable to all programmers that choose csh/tcsh, but if you want to do any serious programming, you'll eventually hit those limitations and be forced to move to a real shell (real in the sense that it has a complete programming specification). I personally never recommend csh/tcsh as a scripting language… but I can see the benefit that certain constructs (like "repeat N cmd") have, purely for their simplicity (and readability for *very* short scripts). To challenge myself on this topic, I routinely try and cross-port very complex shell scripts to csh, and while I can do math with "let" and I can other things that a *normal* scripting language should allow… I invariably end up running away screaming in frustration. Again, csh != tcsh != sh != bash != ash != dash != zsh -- Devin _ The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you. ___ 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: check variable content size in sh script
By default, there is no bash on FreeBSD, Right right... I know this, but forgot what list I was on :) It doesn't help that I always install bash first thing on any freebsd box or it get's installed automatically as part of pc-bsd anyway. __ it has a certain smooth-brained appeal ___ 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: check variable content size in sh script
#foo works with sh Is it actually part of the official spec though is what I'm wondering, or is it a case of other shells not rejecting 'advanced' statements when running in emulation. At least FreeBSD's implementation of sh (which is ash, I think) supports the # functionality. The reason I say all this is that my copy of tcsh (on this not-freebsd machine) *doesn't* work with this when in sh emulation. __ it has a certain smooth-brained appeal ___ 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: check variable content size in sh script
On May 18, 2013, at 9:06 AM, Polytropon wrote: > On Sat, 18 May 2013 11:58:30 -0400, Quartz wrote: >> newfoo=${foo:0:51} >>> >>> That works for bash, not sh. >> >> Ok granted, but I don't think that ${#foo} is straight sh either, so I >> assumed "things bash/tcsh/ksh/whatever accept when running in sh >> emulation" were ok. > > By default, there is no bash on FreeBSD, and therefor no emulation > and "implicit features". :-) > > At least FreeBSD's implementation of sh (which is ash, I think) dash actually -- the Debian Almquist shell (descendent of NetBSD's ash). -- Devin > supports the # functionality. From "man sh": > > ${#parameter} > String Length. The length in characters of the value of > parameter. > > And: > > ${parameter#word} > Remove Smallest Prefix Pattern. The word is expanded to produce > a pattern. The parameter expansion then results in parameter, > with the smallest portion of the prefix matched by the pattern > deleted. > > Check the chapter "Parameter Expansion" for more surprising > things that are supported by ye olde /bin/sh. :-) > > > > -- > 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" _ The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you. ___ 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: check variable content size in sh script
On Sat, 18 May 2013 11:58:30 -0400, Quartz wrote: > > >> newfoo=${foo:0:51} > >> > > > > That works for bash, not sh. > > Ok granted, but I don't think that ${#foo} is straight sh either, so I > assumed "things bash/tcsh/ksh/whatever accept when running in sh > emulation" were ok. By default, there is no bash on FreeBSD, and therefor no emulation and "implicit features". :-) At least FreeBSD's implementation of sh (which is ash, I think) supports the # functionality. From "man sh": ${#parameter} String Length. The length in characters of the value of parameter. And: ${parameter#word} Remove Smallest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest portion of the prefix matched by the pattern deleted. Check the chapter "Parameter Expansion" for more surprising things that are supported by ye olde /bin/sh. :-) -- 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: check variable content size in sh script
#foo works with sh On May 18, 2013 10:58:30 AM Quartz wrote: >> newfoo=${foo:0:51} >> > > That works for bash, not sh. Ok granted, but I don't think that ${#foo} is straight sh either, so I assumed "things bash/tcsh/ksh/whatever accept when running in sh emulation" were ok. __ it has a certain smooth-brained appeal ___ 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" ___ 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: check variable content size in sh script
newfoo=${foo:0:51} That works for bash, not sh. Ok granted, but I don't think that ${#foo} is straight sh either, so I assumed "things bash/tcsh/ksh/whatever accept when running in sh emulation" were ok. __ it has a certain smooth-brained appeal ___ 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: check variable content size in sh script
However, if the OP wanted to actually truncate $FOO to 51 characters: NEWFOO=$( echo "$FOO" | awk -v max=51 '{print substr($0,0,max)}' ) You don't need all that for a simple truncation/substring, you can do it with a direct assignment: newfoo=${foo:0:51} The three params here are "variable", "start position" and "length". __ it has a certain smooth-brained appeal ___ 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: check variable content size in sh script
On 05/18/2013 10:09 AM, Quartz wrote: However, if the OP wanted to actually truncate $FOO to 51 characters: NEWFOO=$( echo "$FOO" | awk -v max=51 '{print substr($0,0,max)}' ) You don't need all that for a simple truncation/substring, you can do it with a direct assignment: newfoo=${foo:0:51} That works for bash, not sh. -- Tim Daneliuk tun...@tundraware.com PGP Key: http://www.tundraware.com/PGP/ ___ 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: check variable content size in sh script
On May 16, 2013, at 9:27 AM, Teske, Devin wrote: On May 16, 2013, at 9:06 AM, Teske, Devin wrote: On May 16, 2013, at 8:28 AM, Tim Daneliuk wrote: On 05/16/2013 10:08 AM, Joe wrote: Hello Have script that has max size on content in a variable. How to code size less than 51 characters? FOO="Some string you want to check length of" FOOLEN=`echo $FOO | wc | awk '{print $3}'` Uh, without forking to 2 separate programs… FOOLEN=${#FOO} You can then use $FOOLEN in a conditional. However, if the OP wanted to actually truncate $FOO to 51 characters: NEWFOO=$( echo "$FOO" | awk -v max=51 '{print substr($0,0,max)}' ) However, if you want to handle the case of $FOO containing newlines (and you want the newline to count toward the max), then this instead would do the trick: NEWFOO=$( echo "$FOO" | awk -v max=51 ' { len = length($0) max -= len print substr($0,0,(max > 0 ? len : max + len)) if ( max < 0 ) exit max-- }' ) For fun, I decided to expand on the solution I provided immediately above… turning it into a function that you might be a little more familiar with: snprintf() { local __var_to_set="$1" __size="$2" shift 2 # var_to_set/size eval "$__var_to_set"=\$\( printf \"\$@\" \| awk -v max=\"\$__size\" \'' { len = length($0) max -= len print substr($0,0,(max > 0 ? len : max + len)) if ( max < 0 ) exit max-- }'\' \) } Example usage: FOO=$( printf "abc\n123\n" ) snprintf NEWFOO 6 "%s" "$FOO" echo "NEWFOO=[$NEWFOO] len=[${#NEWFOO}]" Produces: NEWFOO=[abc 12] len=[6] Hopefully this should help some folks. I figured I'd help as many folks as I can… http://svnweb.freebsd.org/base?view=revision&revision=250701 Added it to my string processing library. Lots of other useful functions in there. -- Cheers, Devin $NEWFOO, even if multi-line, will be limited to 51-bytes (adjust max=51 accordingly for other desired-lengths). Newlines are preserved. Last, but not least, if you want to be able to handle multi-line values but only want to return the first line up-to N bytes (using 51 as the OP used): NEWFOO=$( echo "$FOO" | awk -v max=51 '{ print substr($0,0,max); exit }' ) If $FOO had multiple lines, $NEWFOO will have only the first line (and it will be truncated to 51 bytes or less). -- Devin _ The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you. ___ 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: check variable content size in sh script
On May 16, 2013, at 9:06 AM, Teske, Devin wrote: > > On May 16, 2013, at 8:28 AM, Tim Daneliuk wrote: > >> On 05/16/2013 10:08 AM, Joe wrote: >>> Hello >>> >>> Have script that has max size on content in a variable. >>> How to code size less than 51 characters? >>> >> >> FOO="Some string you want to check length of" >> FOOLEN=`echo $FOO | wc | awk '{print $3}'` >> > > Uh, without forking to 2 separate programs… > > FOOLEN=${#FOO} > > >> You can then use $FOOLEN in a conditional. >> > > > However, if the OP wanted to actually truncate $FOO to 51 characters: > > > NEWFOO=$( echo "$FOO" | awk -v max=51 '{print substr($0,0,max)}' ) > > > However, if you want to handle the case of $FOO containing newlines (and you > want the newline to count toward the max), then this instead would do the > trick: > > > NEWFOO=$( echo "$FOO" | awk -v max=51 ' > { > len = length($0) > max -= len > print substr($0,0,(max > 0 ? len : max + len)) > if ( max < 0 ) exit > max-- > }' ) > For fun, I decided to expand on the solution I provided immediately above… turning it into a function that you might be a little more familiar with: snprintf() { local __var_to_set="$1" __size="$2" shift 2 # var_to_set/size eval "$__var_to_set"=\$\( printf \"\$@\" \| awk -v max=\"\$__size\" \'' { len = length($0) max -= len print substr($0,0,(max > 0 ? len : max + len)) if ( max < 0 ) exit max-- }'\' \) } Example usage: FOO=$( printf "abc\n123\n" ) snprintf NEWFOO 6 "%s" "$FOO" echo "NEWFOO=[$NEWFOO] len=[${#NEWFOO}]" Produces: NEWFOO=[abc 12] len=[6] Hopefully this should help some folks. -- Devin > > $NEWFOO, even if multi-line, will be limited to 51-bytes (adjust max=51 > accordingly for other desired-lengths). Newlines are preserved. > > Last, but not least, if you want to be able to handle multi-line values but > only want to return the first line up-to N bytes (using 51 as the OP used): > > > NEWFOO=$( echo "$FOO" | awk -v max=51 '{ print substr($0,0,max); exit }' ) > > > If $FOO had multiple lines, $NEWFOO will have only the first line (and it > will be truncated to 51 bytes or less). > -- > Devin _ The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you. ___ 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: check variable content size in sh script
On 05/16/2013 10:45 AM, Dan Nelson wrote: In the last episode (May 16), Tim Daneliuk said: On 05/16/2013 10:08 AM, Joe wrote: Hello Have script that has max size on content in a variable. How to code size less than 51 characters? FOO="Some string you want to check length of" FOOLEN=`echo $FOO | wc | awk '{print $3}'` You can then use $FOOLEN in a conditional. Much better way: FOO="Some string you want to check length of" FOOLEN=${#FOO} D'Oh, you're right ... what was I thinking ... -- Tim Daneliuk tun...@tundraware.com PGP Key: http://www.tundraware.com/PGP/ ___ 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: check variable content size in sh script
On May 16, 2013, at 8:28 AM, Tim Daneliuk wrote: > On 05/16/2013 10:08 AM, Joe wrote: >> Hello >> >> Have script that has max size on content in a variable. >> How to code size less than 51 characters? >> > > FOO="Some string you want to check length of" > FOOLEN=`echo $FOO | wc | awk '{print $3}'` > Uh, without forking to 2 separate programs… FOOLEN=${#FOO} > You can then use $FOOLEN in a conditional. > However, if the OP wanted to actually truncate $FOO to 51 characters: NEWFOO=$( echo "$FOO" | awk -v max=51 '{print substr($0,0,max)}' ) However, if you want to handle the case of $FOO containing newlines (and you want the newline to count toward the max), then this instead would do the trick: NEWFOO=$( echo "$FOO" | awk -v max=51 ' { len = length($0) max -= len print substr($0,0,(max > 0 ? len : max + len)) if ( max < 0 ) exit max-- }' ) $NEWFOO, even if multi-line, will be limited to 51-bytes (adjust max=51 accordingly for other desired-lengths). Newlines are preserved. Last, but not least, if you want to be able to handle multi-line values but only want to return the first line up-to N bytes (using 51 as the OP used): NEWFOO=$( echo "$FOO" | awk -v max=51 '{ print substr($0,0,max); exit }' ) If $FOO had multiple lines, $NEWFOO will have only the first line (and it will be truncated to 51 bytes or less). -- Devin _ The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you. ___ 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: check variable content size in sh script
something like this: #!/bin/sh if [ $# -lt 1 ] ; then echo "put a nickel in the slot, pal!" exit 1; fi NUMCHARS=`echo $1 | wc -m` if [ $NUMCHARS -lt 51 ] ; then echo "You input "$NUMCHARS" characters." exit 0 else echo "whoa sailor I can't take all that!" exit 1 fi On 13-05-16 9:08 AM, Joe wrote: > Hello > > Have script that has max size on content in a variable. > How to code size less than 51 characters? > > Thanks > ___ > 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" ___ 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: check variable content size in sh script
In the last episode (May 16), Tim Daneliuk said: > On 05/16/2013 10:08 AM, Joe wrote: > > Hello > > > > Have script that has max size on content in a variable. > > How to code size less than 51 characters? > > > > FOO="Some string you want to check length of" > FOOLEN=`echo $FOO | wc | awk '{print $3}'` > > You can then use $FOOLEN in a conditional. Much better way: FOO="Some string you want to check length of" FOOLEN=${#FOO} -- Dan Nelson dnel...@allantgroup.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: check variable content size in sh script
On 05/16/2013 10:08 AM, Joe wrote: Hello Have script that has max size on content in a variable. How to code size less than 51 characters? FOO="Some string you want to check length of" FOOLEN=`echo $FOO | wc | awk '{print $3}'` You can then use $FOOLEN in a conditional. -- Tim Daneliuk tun...@tundraware.com PGP Key: http://www.tundraware.com/PGP/ ___ 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"
check variable content size in sh script
Hello Have script that has max size on content in a variable. How to code size less than 51 characters? Thanks ___ 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: sh script & files
Polytropon wrote: On Wed, 20 Feb 2013 07:40:08 -0500, Fbsd8 wrote: # write to file > "${file}" I'm thinking the file is never closed so on power failure I loose the contents of the file. How would I code a command to close the file? The file is closed when the write operation has been finished. You can use the "sync" command to flush pending writes to the file (as writing is handled asynchronously by the system). When the program that writes to the file exits, it will close the file it's writing to. This depends on the command you're using infront of >, as the command you've shown will simply generate a null file (file with no actual content). The problem is there may be a great time lag between reading the file and the writing to the file, IE: days or months depending on how long between host reboots. I would just like to close the file right after the file gets read. Is there a way to code that? ___ 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: sh script & files
On Wed, 20 Feb 2013 07:40:08 -0500, Fbsd8 wrote: > # write to file > > "${file}" > > I'm thinking the file is never closed so on power failure I loose the > contents of the file. > > How would I code a command to close the file? The file is closed when the write operation has been finished. You can use the "sync" command to flush pending writes to the file (as writing is handled asynchronously by the system). When the program that writes to the file exits, it will close the file it's writing to. This depends on the command you're using infront of >, as the command you've shown will simply generate a null file (file with no actual content). -- 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"
sh script & files
I noticed that when power fails I loose the contents of some files I am using in a sh script. I read and write the file this way file="/usr/local/etc/filename" # read file . "${file}" # write to file > "${file}" I'm thinking the file is never closed so on power failure I loose the contents of the file. How would I code a command to close the file? Thanks ___ 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: sh script ?
>I get this message [: 10.0.10.21: bad number on this code > >[ "${saved_ip}" -eq "${used_ip}" ] && echo "good match" > >Both variables have valid ip addresses in them. >Why does it think the variable content is a number and not text? >What am I doing wrong? My guess would be that you didn't read the man page for the "test" (sometimes spelled "[" ) command. ___ 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: [Bulk] Re: sh script ?
On Thu, 24 Jan 2013 16:16:54 -0500 Fbsd8 wrote: > Robert Bonomi wrote: > >> From owner-freebsd-questi...@freebsd.org Thu Jan 24 12:11:42 2013 > >> Date: Thu, 24 Jan 2013 13:07:40 -0500 > >> From: Fbsd8 > >> To: FreeBSD questions > >> Subject: sh script ? > >> > >> I get this message [: 10.0.10.21: bad number on this code > >> > >> [ "${saved_ip}" -eq "${used_ip}" ] && echo "good match" > >> > >> Both variables have valid ip addresses in them. > >> Why does it think the variable content is a number and not text? > > > > Why?? BECAUSE YOU TOld IT TO. > > > > RTFM applies. Specifically 'man test'. > > > > You need to either read some books on BASIC shell programming or > > take a course or two on that subject. > > > > This may sound harsh, but you will save yourself a -lot- of future > > aggravation with some structured education. It will also assist > > you in getting maximum value from the manpages. > > > > Programming _is_ an art-form. You have to train yourself to think > > the way the machine does. When it complains about 'something', it > > is *AlMOST*ALWAYS* correct, and something you -think- is correct > > is actually wrong. the hard thing to learn in troubleshooting > > problems is to set aside what you "know" is correct, and look for > > anything that could possibly cause the complaint. Remember, you're > > looking for something "impossible". > > > > > Quit fishing for a flame. > > You know nothing about my background and years of experience. > I have forgotten more about IT and programming than you will > ever learn in a life time. > > You already read the 3 other reply posts and there was no need to > say what you did. You added nothing to the info in the thread. > The thread was basically ended already. > > This is not the first time you have replied to posts in > this condescending manner. > > I am putting you on notice, your manner and tone is not acceptable on > this list. Please take more time to consider your replies before posting > again. > > Any further reply from you about this will be considered as flaming and > ignored. > > > > > ___ > 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" Well said. ___ 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: sh script ?
Robert Bonomi wrote: From owner-freebsd-questi...@freebsd.org Thu Jan 24 12:11:42 2013 Date: Thu, 24 Jan 2013 13:07:40 -0500 From: Fbsd8 To: FreeBSD questions Subject: sh script ? I get this message [: 10.0.10.21: bad number on this code [ "${saved_ip}" -eq "${used_ip}" ] && echo "good match" Both variables have valid ip addresses in them. Why does it think the variable content is a number and not text? Why?? BECAUSE YOU TOld IT TO. RTFM applies. Specifically 'man test'. You need to either read some books on BASIC shell programming or take a course or two on that subject. This may sound harsh, but you will save yourself a -lot- of future aggravation with some structured education. It will also assist you in getting maximum value from the manpages. Programming _is_ an art-form. You have to train yourself to think the way the machine does. When it complains about 'something', it is *AlMOST*ALWAYS* correct, and something you -think- is correct is actually wrong. the hard thing to learn in troubleshooting problems is to set aside what you "know" is correct, and look for anything that could possibly cause the complaint. Remember, you're looking for something "impossible". Quit fishing for a flame. You know nothing about my background and years of experience. I have forgotten more about IT and programming than you will ever learn in a life time. You already read the 3 other reply posts and there was no need to say what you did. You added nothing to the info in the thread. The thread was basically ended already. This is not the first time you have replied to posts in this condescending manner. I am putting you on notice, your manner and tone is not acceptable on this list. Please take more time to consider your replies before posting again. Any further reply from you about this will be considered as flaming and ignored. ___ 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: sh script ?
> From owner-freebsd-questi...@freebsd.org Thu Jan 24 12:11:42 2013 > Date: Thu, 24 Jan 2013 13:07:40 -0500 > From: Fbsd8 > To: FreeBSD questions > Subject: sh script ? > > I get this message [: 10.0.10.21: bad number on this code > > [ "${saved_ip}" -eq "${used_ip}" ] && echo "good match" > > Both variables have valid ip addresses in them. > Why does it think the variable content is a number and not text? Why?? BECAUSE YOU TOld IT TO. RTFM applies. Specifically 'man test'. You need to either read some books on BASIC shell programming or take a course or two on that subject. This may sound harsh, but you will save yourself a -lot- of future aggravation with some structured education. It will also assist you in getting maximum value from the manpages. Programming _is_ an art-form. You have to train yourself to think the way the machine does. When it complains about 'something', it is *AlMOST*ALWAYS* correct, and something you -think- is correct is actually wrong. the hard thing to learn in troubleshooting problems is to set aside what you "know" is correct, and look for anything that could possibly cause the complaint. Remember, you're looking for something "impossible". ___ 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: sh script ?
On Thu, Jan 24, 2013 at 1:07 PM, Fbsd8 wrote: > I get this message [: 10.0.10.21: bad number on this code > >[ "${saved_ip}" -eq "${used_ip}" ] && echo "good match" > > Both variables have valid ip addresses in them. > Why does it think the variable content is a number and not text? > What am I doing wrong? The -eq operator is used to compare integers. When comparing strings, use the == operator. -- Take care Rick Miller ___ 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: sh script ?
Use = for string comparison with the [ built-in and -eq for numerical comparison. -- Devin On Jan 24, 2013, at 10:07 AM, Fbsd8 wrote: > I get this message [: 10.0.10.21: bad number on this code > > [ "${saved_ip}" -eq "${used_ip}" ] && echo "good match" > > Both variables have valid ip addresses in them. > Why does it think the variable content is a number and not text? > What am I doing wrong? > > Thanks for your help > ___ > 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" _ The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you. ___ 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"
sh script ?
I get this message [: 10.0.10.21: bad number on this code [ "${saved_ip}" -eq "${used_ip}" ] && echo "good match" Both variables have valid ip addresses in them. Why does it think the variable content is a number and not text? What am I doing wrong? Thanks for your help ___ 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: sh script code to get file size.
> From owner-freebsd-questi...@freebsd.org Fri Jan 18 17:30:31 2013 > Date: Fri, 18 Jan 2013 18:26:54 -0500 > From: Fbsd8 > To: FreeBSD questions > Subject: sh script code to get file size. > > In a script in am working on I need to find out the allocated > size of a sparse file. > The only command that comes to mind is "ls -lh" > The "du -h" command is not appropriate because it will show > the occupied size and not the allocated size. > > I don't know how to parse out to the position in the output of that > "ls -lh" command to pickup the file size value. ls -lh procmail.log |awk '{print $5}' ___ 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: sh script code to get file size.
On Fri, 18 Jan 2013, Fbsd8 wrote: In a script in am working on I need to find out the allocated size of a sparse file. The only command that comes to mind is "ls -lh" The "du -h" command is not appropriate because it will show the occupied size and not the allocated size. I don't know how to parse out to the position in the output of that "ls -lh" command to pickup the file size value. Is there some other way to do this? To parse it out, I've used something like: $ ls -lh npviewer.bin.core | cut -d \ -f 9 186M After the backslash are two spaces: one being the space that's being escaped to make it the delimiter, the other to separate the options. The number after the '-f' determines which "field" of the output is displayed, which may vary. HTH. -- Chris Hill ch...@monochrome.org ** [ Busy Expunging ] ___ 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: sh script code to get file size.
Chris Hill wrote: On Fri, 18 Jan 2013, Fbsd8 wrote: In a script in am working on I need to find out the allocated size of a sparse file. The only command that comes to mind is "ls -lh" The "du -h" command is not appropriate because it will show the occupied size and not the allocated size. I don't know how to parse out to the position in the output of that "ls -lh" command to pickup the file size value. Is there some other way to do this? To parse it out, I've used something like: $ ls -lh npviewer.bin.core | cut -d \ -f 9 186M After the backslash are two spaces: one being the space that's being escaped to make it the delimiter, the other to separate the options. The number after the '-f' determines which "field" of the output is displayed, which may vary. HTH. Yes that works real nice. Thanks to all who replied. ___ 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: sh script code to get file size.
In a script in am working on I need to find out the allocated size of a sparse file. The only command that comes to mind is "ls -lh" The "du -h" command is not appropriate because it will show the occupied size and not the allocated size. I don't know how to parse out to the position in the output of that "ls -lh" command to pickup the file size value. Is there some other way to do this? reza wrote: > Does this work for you > > $ ls -lh | awk '{print $5}' > > 132B > 0B > 3.8k > 512B > 3.9k > 512B > 512B > 14M > 512B > > Thanks that works for me. ___ 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: sh script code to get file size.
On 18/01/2013 23:26, Fbsd8 wrote: > In a script in am working on I need to find out the allocated > size of a sparse file. > The only command that comes to mind is "ls -lh" > The "du -h" command is not appropriate because it will show > the occupied size and not the allocated size. > > I don't know how to parse out to the position in the output of that > "ls -lh" command to pickup the file size value. > > Is there some other way to do this? cut or awk spring to mind. I'm sure there are more :) (23:31:48 <~>) 0 jhary@ostracod $ ls -lh Cisco-vpn-Profiles.zip | cut -f 5 -w 16k (23:32:15 <~>) 0 jhary@ostracod $ ls -lh Cisco-vpn-Profiles.zip | awk '{print $5}' 16k (23:32:25 <~>) 0 jhary@ostracod $ ls -lh Cisco-vpn-Profiles.zip -rw-r--r-- 1 jhary wheel16k 11 Jul 2004 Cisco-vpn-Profiles.zip (23:32:32 <~>) 0 jhary@ostracod $ Vince > ___ > 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" ___ 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: sh script code to get file size.
In the last episode (Jan 18), Fbsd8 said: > In a script in am working on I need to find out the allocated > size of a sparse file. > The only command that comes to mind is "ls -lh" > The "du -h" command is not appropriate because it will show > the occupied size and not the allocated size. > > I don't know how to parse out to the position in the output of that > "ls -lh" command to pickup the file size value. > > Is there some other way to do this? For a single file, you can get the exact filesize with "stat -f %z filename". For the human-friendly version, "du -hA" will work. -- Dan Nelson dnel...@allantgroup.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"
sh script code to get file size.
In a script in am working on I need to find out the allocated size of a sparse file. The only command that comes to mind is "ls -lh" The "du -h" command is not appropriate because it will show the occupied size and not the allocated size. I don't know how to parse out to the position in the output of that "ls -lh" command to pickup the file size value. Is there some other way to do this? ___ 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: sh script problem with capturing return code
Lowell Gilbert wrote: Fbsd8 writes: Lowell Gilbert wrote: This fixes the problem that was bothering you, but the interactions of different features are complicated, and many of them are documented in fairly loose language. Is that the diplomatic way of saying the manpage for mtree sucks, and leaves a great deal to be desired? Not really; I was looking at the code at the time I wrote that. To be fair, I'm not sure what I would *expect* for some of the possible interactions. When it comes to testing your patch sure I would like to. But here again I have a problem. For security reasons I can not use source code to install or update any operating system. I have no sources on my system to compile from. I use the fresh install method from a downloaded disc1.iso burned to cdrom. I was thinking you could extend the shell scripts using your existing mtree executable. Well I have already played with all the mtree(8) options you are interested in -u, -U, -q, -d, and different combinations of them including the specification keywords of "nochange", and "ignore", and in all cases the return code is always zero even when the test case should result in a return code of 1 or 2. I had no requirements to use the "optional" keyword, but after the 15+ combinations I ran I see no reason to expect the "optional", keyword to change the results I got. ___ 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: sh script problem with capturing return code
Fbsd8 writes: > Lowell Gilbert wrote: >> This fixes the problem that was bothering you, but the interactions of >> different features are complicated, and many of them are documented in >> fairly loose language. >> > Is that the diplomatic way of saying the manpage for mtree sucks, and > leaves a great deal to be desired? Not really; I was looking at the code at the time I wrote that. To be fair, I'm not sure what I would *expect* for some of the possible interactions. > When it comes to testing your patch sure I would like to. But here > again I have a problem. For security reasons I can not use source code > to install or update any operating system. I have no sources on my > system to compile from. I use the fresh install method from a > downloaded disc1.iso burned to cdrom. I was thinking you could extend the shell scripts using your existing mtree executable. ___ 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: sh script problem with capturing return code
Fbsd8 writes: > Let me be sure I understand you correctly. > Your saying you tested the NetBSD version of mtree that was committed > into 10.0 head and it also has the bug we found? No. The port is not complete. All I did was a code inspection. ___ 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: sh script problem with capturing return code
Lowell Gilbert wrote: Fbsd8 writes: Lowell Gilbert wrote: Lowell Gilbert writes: I think it's a real bug, and the test cases don't cover "extra" elements at all. Now I just have to figure out the right fix. I'm pretty sure that the fix is just to set rval on jumping to the "extra" tag in vwalk() in src/usr.sbin/mtree/verify.c. But my hot water heater just exploded, so I may not look at code for a few more days. Hello Lowell, Thank you very much. I was going crazy trying different combinations of options and script logic. I want to thank you for taking my problem seriously and taking the time to do your own test cases to verify my findings. Now that I know it's a genuine bug in mtree, I can make my plans accordingly. Any fix to the mtree utility will take some time to filter down to a regular RELEASE. Maybe 9.2 or the big jump to 10.0 by the end of 2013. I will leave it up to you the file a PR on this and follow through. Thanks again, people like you are what makes this questions list so valuable and FreeBSD such a great OS. You did outstanding work. Actually, in retrospect I think it was pretty trivial. I think the following patch is the right fix for the problem, although I am not completely certain. Index: /usr/src/usr.sbin/mtree/verify.c === --- /usr/src/usr.sbin/mtree/verify.c(revision 245177) +++ /usr/src/usr.sbin/mtree/verify.c(working copy) @@ -149,6 +149,7 @@ } (void)fts_set(t, p, FTS_SKIP); } + rval = MISMATCHEXIT; (void)fts_close(t); if (sflag) warnx("%s checksum: %lu", fullpath, (unsigned long)crc_total); This fixes the problem that was bothering you, but the interactions of different features are complicated, and many of them are documented in fairly loose language. > Is that the diplomatic way of saying the manpage for mtree sucks, and leaves a great deal to be desired? Would you be interested in extending the test suite for this program? There are some tests in /usr/src/usr.sbin/mtree/test/, but they don't cover your issue. I'm particularly concerned with interactions between mtree(8) options like -u, -U, -q, -d, and specification keywords like "optional", "nochange", and "ignore". I would feel more comfortable if someone else wrote up new test cases (because programmers generally don't -- can't -- test their own blind spots), although I'll certainly do it if no one else does. I haven't submitted a PR yet, but I'll do so as soon as I've translated my test case into a form that can be used in the PR. Lowell here is my situation, I support a port and needed a method to interrogate the content of a small directory tree for correctness before processing it's content. I first developed a coding logic using mtree for this purpose, but that brought to light this mtree bug which is the subject of this post. Since my port only uses utilities that are contained in the base system of a RELEASE I could not very well wait for any patches to mtree to filter down into a RELEASE before updating my port. So instead I developed a coding logic using diff that does the same thing I was doing with mtree but diff issues the correct return codes. I have since updated my port in the ports system. When it comes to testing your patch sure I would like to. But here again I have a problem. For security reasons I can not use source code to install or update any operating system. I have no sources on my system to compile from. I use the fresh install method from a downloaded disc1.iso burned to cdrom. This way I know beyond a shadow of a doubt I have an un-compromised, clean, virgin system to start with and nothing present or left over from previous running systems that can be used to compromise the system. Now if you would use gzip to compress the binary patched version of mtree you have been testing with and email it directly to me offline of this list, then sure I will test it inside a jail and even play around with the other option flags your interested in and provide you feed back off list. Joe ___ 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: sh script problem with capturing return code
Lowell Gilbert wrote: Adam Vande More writes: On Wed, Jan 9, 2013 at 7:23 PM, Lowell Gilbert < freebsd-questions-lo...@be-well.ilk.org> wrote: Lowell Gilbert writes: I think it's a real bug, and the test cases don't cover "extra" elements at all. Now I just have to figure out the right fix. I'm pretty sure that the fix is just to set rval on jumping to the "extra" tag in vwalk() in src/usr.sbin/mtree/verify.c. But my hot water heater just exploded, so I may not look at code for a few more days. I think they are importing NetBSD's updated mtree, perhaps already fixed there. It isn't. Which means I probably should submit fixes to multiple places. Although NetBSD's changes seem fairly minor to me, from a quick look. The merge should not be difficult, no matter how it's approached. Let me be sure I understand you correctly. Your saying you tested the NetBSD version of mtree that was committed into 10.0 head and it also has the bug we found? ___ 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: sh script problem with capturing return code
Adam Vande More writes: > On Wed, Jan 9, 2013 at 7:23 PM, Lowell Gilbert < > freebsd-questions-lo...@be-well.ilk.org> wrote: > >> Lowell Gilbert writes: >> >> > I think it's a real bug, and the test cases don't cover "extra" elements >> > at all. Now I just have to figure out the right fix. >> >> I'm pretty sure that the fix is just to set rval on jumping to the >> "extra" tag in vwalk() in src/usr.sbin/mtree/verify.c. >> >> But my hot water heater just exploded, so I may not look at code for a >> few more days. > > > I think they are importing NetBSD's updated mtree, perhaps already fixed > there. It isn't. Which means I probably should submit fixes to multiple places. Although NetBSD's changes seem fairly minor to me, from a quick look. The merge should not be difficult, no matter how it's approached. ___ 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: sh script problem with capturing return code
Fbsd8 writes: > Lowell Gilbert wrote: >> Lowell Gilbert writes: >> >>> I think it's a real bug, and the test cases don't cover "extra" elements >>> at all. Now I just have to figure out the right fix. >> >> I'm pretty sure that the fix is just to set rval on jumping to the >> "extra" tag in vwalk() in src/usr.sbin/mtree/verify.c. >> >> But my hot water heater just exploded, so I may not look at code for a >> few more days. > > Hello Lowell, > Thank you very much. > I was going crazy trying different combinations of options and script > logic. I want to thank you for taking my problem seriously and taking > the time to do your own test cases to verify my findings. Now that I > know it's a genuine bug in mtree, I can make my plans accordingly. Any > fix to the mtree utility will take some time to filter down to a > regular RELEASE. Maybe 9.2 or the big jump to 10.0 by the end of > 2013. I will leave it up to you the file a PR on this and follow > through. > Thanks again, people like you are what makes this questions list so > valuable and FreeBSD such a great OS. You did outstanding work. Actually, in retrospect I think it was pretty trivial. I think the following patch is the right fix for the problem, although I am not completely certain. Index: /usr/src/usr.sbin/mtree/verify.c === --- /usr/src/usr.sbin/mtree/verify.c(revision 245177) +++ /usr/src/usr.sbin/mtree/verify.c(working copy) @@ -149,6 +149,7 @@ } (void)fts_set(t, p, FTS_SKIP); } + rval = MISMATCHEXIT; (void)fts_close(t); if (sflag) warnx("%s checksum: %lu", fullpath, (unsigned long)crc_total); This fixes the problem that was bothering you, but the interactions of different features are complicated, and many of them are documented in fairly loose language. Would you be interested in extending the test suite for this program? There are some tests in /usr/src/usr.sbin/mtree/test/, but they don't cover your issue. I'm particularly concerned with interactions between mtree(8) options like -u, -U, -q, -d, and specification keywords like "optional", "nochange", and "ignore". I would feel more comfortable if someone else wrote up new test cases (because programmers generally don't -- can't -- test their own blind spots), although I'll certainly do it if no one else does. I haven't submitted a PR yet, but I'll do so as soon as I've translated my test case into a form that can be used in the PR. [As an upside, I've learned about the fts_ family, which I hadn't really looked at before.] Be well. ___ 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: sh script problem with capturing return code
On Wed, Jan 9, 2013 at 7:23 PM, Lowell Gilbert < freebsd-questions-lo...@be-well.ilk.org> wrote: > Lowell Gilbert writes: > > > I think it's a real bug, and the test cases don't cover "extra" elements > > at all. Now I just have to figure out the right fix. > > I'm pretty sure that the fix is just to set rval on jumping to the > "extra" tag in vwalk() in src/usr.sbin/mtree/verify.c. > > But my hot water heater just exploded, so I may not look at code for a > few more days. I think they are importing NetBSD's updated mtree, perhaps already fixed there. -- Adam Vande More ___ 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: sh script problem with capturing return code
Lowell Gilbert wrote: Lowell Gilbert writes: I think it's a real bug, and the test cases don't cover "extra" elements at all. Now I just have to figure out the right fix. I'm pretty sure that the fix is just to set rval on jumping to the "extra" tag in vwalk() in src/usr.sbin/mtree/verify.c. But my hot water heater just exploded, so I may not look at code for a few more days. Hello Lowell, Thank you very much. I was going crazy trying different combinations of options and script logic. I want to thank you for taking my problem seriously and taking the time to do your own test cases to verify my findings. Now that I know it's a genuine bug in mtree, I can make my plans accordingly. Any fix to the mtree utility will take some time to filter down to a regular RELEASE. Maybe 9.2 or the big jump to 10.0 by the end of 2013. I will leave it up to you the file a PR on this and follow through. Thanks again, people like you are what makes this questions list so valuable and FreeBSD such a great OS. You did outstanding work. Joe ___ 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: sh script problem with capturing return code
Lowell Gilbert writes: > I think it's a real bug, and the test cases don't cover "extra" elements > at all. Now I just have to figure out the right fix. I'm pretty sure that the fix is just to set rval on jumping to the "extra" tag in vwalk() in src/usr.sbin/mtree/verify.c. But my hot water heater just exploded, so I may not look at code for a few more days. ___ 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: sh script problem with capturing return code
Lowell Gilbert writes: > It's not; ignore my example. The extra directory was under the ignored > directory, so it's testing the right properties. I think if I create the > new subdirectory under the other main directory, it would be right. Confirmed. The test case now looks like this: mkdir etc home mtree -c -d > ../out mtree -d < ../out echo $? echo "That was the first: should be 0." mkdir etc/temp mtree -u -d < ../out echo $? echo "That was the second: should be 2." sed -i "" 's/^\(home[ ]*\)\(.*\)$/\1ignore/' ../out mtree -d < ../out echo $? echo "That was the third: should be 0." I think it's a real bug, and the test cases don't cover "extra" elements at all. Now I just have to figure out the right fix. ___ 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: sh script problem with capturing return code
Lowell Gilbert writes: > and I think the problem you're having is that the second "echo $?" > should be 2, although the others are correct at 0. Is that correct? It's not; ignore my example. The extra directory was under the ignored directory, so it's testing the right properties. I think if I create the new subdirectory under the other main directory, it would be right. ___ 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: sh script problem with capturing return code
Fbsd8 writes: > So the question remains, why is mtree giving a return of zero when it > finds directories on the target that are not in the spec file? Okay, I had a hard time figuring out your examples, but I think I've got an independent repeatable test case for the problem. mkdir etc home mtree -c -d > ../out mtree -d < ../out echo $? mkdir home/temp mtree -d < ../out echo $? sed -i "" 's/^\(home[ ]*\)\(.*\)$/\1ignore/' ../out mtree -d < ../out echo $? The results are: 5045] (lowell-desk) temp> mkdir etc home [5046] (lowell-desk) temp> mtree -c -d > ../out [5047] (lowell-desk) temp> mtree -d < ../out [5048] (lowell-desk) temp> echo $? 0 [5049] (lowell-desk) temp> mkdir home/temp [5050] (lowell-desk) temp> mtree -d < ../out home/temp extra [5051] (lowell-desk) temp> echo $? 0 [5052] (lowell-desk) temp> sed -i "" 's/^\(home[ ]*\)\(.*\)$/\1ignore/' ../out [5053] (lowell-desk) temp> mtree -d < ../out [5054] (lowell-desk) temp> echo $? 0 and I think the problem you're having is that the second "echo $?" should be 2, although the others are correct at 0. Is that correct? One difference from your example is that you're using '-u'. I'm not sure why you're doing that, but it doesn't affect the bug. Yours, Lowell -- http://be-well.ilk.org/~lowell/ ___ 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: sh script problem with capturing return code
Lowell Gilbert wrote: Fbsd8 writes: Polytropon wrote: On Tue, 08 Jan 2013 16:30:49 -0500, Fbsd8 wrote: I can not get the return code from mtree to control the displaying of a error message. The mtree at the end of the script does function correctly because I can tell from the printed output. When mtree prints comments saying "extra" that means the directory being read does not match the specification file. return code should be Not equal to zero. And when they do match IE: no mtree comments printed, that should be a return code of zero. I tried 2 different ways to capture the return code to no joy. What I am doing wrong? #! /bin/sh flavor="/a/mtree.std" echo "flavor = ${flavor}" /bin/cat << EOF | /set type=dir uname=root gname=wheel . etc ignore .. rootignore .. usr homeignore .. local etc ignore .. .. .. .. EOF mtree -d -u -p "${flavor}" || \ echo "Error invalid directories in flavor ${flavor}." #mtree -d -p "${flavor}" #[ $? -eq 0 ] || \ # echo "Error invalid directories in flavor ${flavor}." echo "return = $?" It seems that returning 0 is correct in a case as you mentioned. The manual at "man mtree" states: EXIT STATUS The mtree utility exits 0 on success, and >0 if an error occurs. Question: What _is_ an error here? If you use the source Luke at /usr/src/usr.sbin/mtree/mtree.c, you could find out what combination of options plus circumstances found at runtime could trigger an exit status != 0. The main() functions finishes with exit(status); where status is either set by functions mtree_specspec() or mtree_verifyspec(), or manually to 0 when -U is provided and MISMATCHEXIT (is 2) is encountered. Again from the manual: -uSame as -U except a status of 2 is returned if the file hierarchy did not match the specification. -UModify the owner, group, permissions, and modification time of existing files to match the specification and create any missing directories or symbolic links. User, group and permissions must all be specified for missing directories to be created. Corrected mismatches are not considered errors. However, you're not using -U, but -u, so the last sentence of the description above should be relevant: No error per se, even though the status code should be 2. Well I just tested with -U -u together no joy. Tested with -U no joy. My read of the above is -u should cause a return code of 2 when the file hierarchy does not match the specification. I don't want -U because I am not modifying any content of the directory tree mtree is looking at. I just don't get the point your trying to make. Oh the other hand are you saying the script code is correct to capture the return code but using wrong options with mtree ? It works fine for me: [5023] (lowell-desk) temp> touch foo [5024] (lowell-desk) temp> mtree -c > ../out [5025] (lowell-desk) temp> if (mtree < ../out ) ; then echo yes ; else echo no ; fi yes [5026] (lowell-desk) temp> touch foo [5027] (lowell-desk) temp> if (mtree < ../out ) ; then echo yes ; else echo no ; fi foo changed modification time expected Wed Jan 9 02:42:31 2013 found Wed Jan 9 02:42:47 2013 no [5028] (lowell-desk) temp> echo $? 0 [5029] (lowell-desk) temp> mtree < ../out foo changed modification time expected Wed Jan 9 02:42:31 2013 found Wed Jan 9 02:42:47 2013 [5030] (lowell-desk) temp> echo $? 2 [5031] (lowell-desk) temp> touch temp [5032] (lowell-desk) temp> mtree -u < ../out . changed modification time expected Wed Jan 9 02:42:37 2013 found Wed Jan 9 02:49:52 2013 modified foo changed modification time expected Wed Jan 9 02:42:31 2013 found Wed Jan 9 02:42:47 2013 modified temp extra [5033] (lowell-desk) temp> echo $? 2 This is exactly what I would expect, and what you said you weren't getting. You didn't show what you ran your script on, or what the results were, so I can't tell you what you're doing wrong -- but compare my example to yours and I'm sure you'll be able to figure it out. Good luck. Your example is testing for file changes. The mtree spec file I posted and the mtree -d shows I am checking just at the directory level. The "ignore" option on the spec file means "ignore any file hierarchy below this file". That coupled with the -d option that says "ignore everything except directory type files". So what I want to get is; apply that spec file to the target directory checking that the spec directories are present in the target and any sub-directories on those spec directories are ignored on the target, IE; is not an error condition. All files in spec directory tree and the target directory tree are ignored. Any directories in the target directory tree not in the spec file are errors and set the return code to non-zero. The spec and target directory trees look like this.
Re: sh script problem with capturing return code
Fbsd8 writes: > Polytropon wrote: >> On Tue, 08 Jan 2013 16:30:49 -0500, Fbsd8 wrote: >>> I can not get the return code from mtree to control >>> the displaying of a error message. >>> >>> The mtree at the end of the script does function correctly >>> because I can tell from the printed output. >>> >>> When mtree prints comments saying "extra" that means the directory >>> being read does not match the specification file. return code >>> should be Not equal to zero. >>> >>> And when they do match IE: no mtree comments printed, that should >>> be a return code of zero. >>> >>> I tried 2 different ways to capture the return code to no joy. >>> What I am doing wrong? >>> >>> #! /bin/sh >>> flavor="/a/mtree.std" >>> echo "flavor = ${flavor}" >>> /bin/cat << EOF | >>> /set type=dir uname=root gname=wheel >>> . >>> etc ignore >>> .. >>> rootignore >>> .. >>> usr >>> homeignore >>> .. >>> local >>> etc ignore >>> .. >>> .. >>> .. >>> .. >>> EOF >>> >>> mtree -d -u -p "${flavor}" || \ >>> echo "Error invalid directories in flavor ${flavor}." >>> >>> #mtree -d -p "${flavor}" >>> #[ $? -eq 0 ] || \ >>> # echo "Error invalid directories in flavor ${flavor}." >>> echo "return = $?" >> >> It seems that returning 0 is correct in a case as you mentioned. >> The manual at "man mtree" states: >> >> EXIT STATUS >> The mtree utility exits 0 on success, and >0 if an error occurs. >> >> Question: What _is_ an error here? >> >> If you use the source Luke at /usr/src/usr.sbin/mtree/mtree.c, >> you could find out what combination of options plus circumstances >> found at runtime could trigger an exit status != 0. >> >> The main() functions finishes with exit(status); where status >> is either set by functions mtree_specspec() or mtree_verifyspec(), >> or manually to 0 when -U is provided and MISMATCHEXIT (is 2) is >> encountered. >> >> Again from the manual: >> >> -uSame as -U except a status of 2 is returned if the file hierarchy >>did not match the specification. >> >> -UModify the owner, group, permissions, and modification time of >>existing files to match the specification and create any missing >>directories or symbolic links. User, group and permissions must >>all be specified for missing directories to be created. Corrected >>mismatches are not considered errors. >> >> However, you're not using -U, but -u, so the last sentence of >> the description above should be relevant: No error per se, >> even though the status code should be 2. >> >> >> >> > Well I just tested with -U -u together no joy. > Tested with -U no joy. > > My read of the above is -u should cause a return code of 2 when the > file hierarchy does not match the specification. I don't want -U > because I am not modifying any content of the directory tree mtree is > looking at. > > I just don't get the point your trying to make. > > Oh the other hand are you saying the script code is correct to capture > the return code but using wrong options with mtree ? It works fine for me: [5023] (lowell-desk) temp> touch foo [5024] (lowell-desk) temp> mtree -c > ../out [5025] (lowell-desk) temp> if (mtree < ../out ) ; then echo yes ; else echo no ; fi yes [5026] (lowell-desk) temp> touch foo [5027] (lowell-desk) temp> if (mtree < ../out ) ; then echo yes ; else echo no ; fi foo changed modification time expected Wed Jan 9 02:42:31 2013 found Wed Jan 9 02:42:47 2013 no [5028] (lowell-desk) temp> echo $? 0 [5029] (lowell-desk) temp> mtree < ../out foo changed modification time expected Wed Jan 9 02:42:31 2013 found Wed Jan 9 02:42:47 2013 [5030] (lowell-desk) temp> echo $? 2 [5031] (lowell-desk) temp> touch temp [5032] (lowell-desk) temp> mtree -u < ../out . changed modification time expected Wed Jan 9 02:42:37 2013 found Wed Jan 9 02:49:52 2013 modified foo changed modification time expected Wed Jan 9 02:42:31 2013 found Wed Jan 9 02:42:47 2013 modified temp extra [5033] (lowell-desk) temp> echo $? 2 This is exactly what I would expect, and what you said you weren't getting. You didn't show what you ran your script on, or what the results were, so I can't tell you what you're doing wrong -- but compare my example to yours and I'm sure you'll be able to figure it out. Good luck. ___ 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: sh script problem with capturing return code
Polytropon wrote: On Tue, 08 Jan 2013 16:30:49 -0500, Fbsd8 wrote: I can not get the return code from mtree to control the displaying of a error message. The mtree at the end of the script does function correctly because I can tell from the printed output. When mtree prints comments saying "extra" that means the directory being read does not match the specification file. return code should be Not equal to zero. And when they do match IE: no mtree comments printed, that should be a return code of zero. I tried 2 different ways to capture the return code to no joy. What I am doing wrong? #! /bin/sh flavor="/a/mtree.std" echo "flavor = ${flavor}" /bin/cat << EOF | /set type=dir uname=root gname=wheel . etc ignore .. rootignore .. usr homeignore .. local etc ignore .. .. .. .. EOF mtree -d -u -p "${flavor}" || \ echo "Error invalid directories in flavor ${flavor}." #mtree -d -p "${flavor}" #[ $? -eq 0 ] || \ # echo "Error invalid directories in flavor ${flavor}." echo "return = $?" It seems that returning 0 is correct in a case as you mentioned. The manual at "man mtree" states: EXIT STATUS The mtree utility exits 0 on success, and >0 if an error occurs. Question: What _is_ an error here? If you use the source Luke at /usr/src/usr.sbin/mtree/mtree.c, you could find out what combination of options plus circumstances found at runtime could trigger an exit status != 0. The main() functions finishes with exit(status); where status is either set by functions mtree_specspec() or mtree_verifyspec(), or manually to 0 when -U is provided and MISMATCHEXIT (is 2) is encountered. Again from the manual: -uSame as -U except a status of 2 is returned if the file hierarchy did not match the specification. -UModify the owner, group, permissions, and modification time of existing files to match the specification and create any missing directories or symbolic links. User, group and permissions must all be specified for missing directories to be created. Corrected mismatches are not considered errors. However, you're not using -U, but -u, so the last sentence of the description above should be relevant: No error per se, even though the status code should be 2. Well I just tested with -U -u together no joy. Tested with -U no joy. My read of the above is -u should cause a return code of 2 when the file hierarchy does not match the specification. I don't want -U because I am not modifying any content of the directory tree mtree is looking at. I just don't get the point your trying to make. Oh the other hand are you saying the script code is correct to capture the return code but using wrong options with mtree ? ___ 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: sh script problem with capturing return code
On Tue, 08 Jan 2013 16:30:49 -0500, Fbsd8 wrote: > > I can not get the return code from mtree to control > the displaying of a error message. > > The mtree at the end of the script does function correctly > because I can tell from the printed output. > > When mtree prints comments saying "extra" that means the directory being > read does not match the specification file. return code should be Not > equal to zero. > > And when they do match IE: no mtree comments printed, that should be a > return code of zero. > > I tried 2 different ways to capture the return code to no joy. > What I am doing wrong? > > #! /bin/sh > flavor="/a/mtree.std" > echo "flavor = ${flavor}" > /bin/cat << EOF | > /set type=dir uname=root gname=wheel > . > etc ignore > .. > rootignore > .. > usr > homeignore > .. > local > etc ignore > .. > .. > .. > .. > EOF > > mtree -d -u -p "${flavor}" || \ > echo "Error invalid directories in flavor ${flavor}." > > #mtree -d -p "${flavor}" > #[ $? -eq 0 ] || \ > # echo "Error invalid directories in flavor ${flavor}." > echo "return = $?" It seems that returning 0 is correct in a case as you mentioned. The manual at "man mtree" states: EXIT STATUS The mtree utility exits 0 on success, and >0 if an error occurs. Question: What _is_ an error here? If you use the source Luke at /usr/src/usr.sbin/mtree/mtree.c, you could find out what combination of options plus circumstances found at runtime could trigger an exit status != 0. The main() functions finishes with exit(status); where status is either set by functions mtree_specspec() or mtree_verifyspec(), or manually to 0 when -U is provided and MISMATCHEXIT (is 2) is encountered. Again from the manual: -uSame as -U except a status of 2 is returned if the file hierarchy did not match the specification. -UModify the owner, group, permissions, and modification time of existing files to match the specification and create any missing directories or symbolic links. User, group and permissions must all be specified for missing directories to be created. Corrected mismatches are not considered errors. However, you're not using -U, but -u, so the last sentence of the description above should be relevant: No error per se, even though the status code should be 2. -- 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"
sh script problem with capturing return code
I can not get the return code from mtree to control the displaying of a error message. The mtree at the end of the script does function correctly because I can tell from the printed output. When mtree prints comments saying "extra" that means the directory being read does not match the specification file. return code should be Not equal to zero. And when they do match IE: no mtree comments printed, that should be a return code of zero. I tried 2 different ways to capture the return code to no joy. What I am doing wrong? #! /bin/sh flavor="/a/mtree.std" echo "flavor = ${flavor}" /bin/cat << EOF | /set type=dir uname=root gname=wheel . etc ignore .. rootignore .. usr homeignore .. local etc ignore .. .. .. .. EOF mtree -d -u -p "${flavor}" || \ echo "Error invalid directories in flavor ${flavor}." #mtree -d -p "${flavor}" #[ $? -eq 0 ] || \ # echo "Error invalid directories in flavor ${flavor}." echo "return = $?" ___ 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: command to strip suffix in .sh script
On Thu, Jun 03, 2010 at 11:11:16PM +0700, Anh Ky Huynh wrote: > On Wed, 02 Jun 2010 20:25:36 -0400 Vinny wrote: > > On 06/02/2010 04:30, Matthew Seaman wrote: > > > On 02/06/2010 09:24:01, Matthias Apitz wrote: > > > > Aiza wrote: > > > > > > > > > I have this code > > [snip] > > > > $ echo 'archivename-201006021514.34.tar.gz' | sed 's/-.*$//' > > > > > > archive_name=${fromarchive%-*} > > > > Thanks Matthew, that's really neat. It took me a long time to find > > the correct google incantation to find the documentation for that. > > ( bourne shell pattern-matching notation ) > > In fact I know about that from Bash's documents:) IMHO, there are more > Bash's documents than Bourne's ones. Kids today. ;-) No need for Magick Google Incantations: man sh | less -p 'Parameter Expansion$' man bash | less -p 'Parameter Expansion$' -- George ___ 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: command to strip suffix in .sh script
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 03/06/2010 17:11:16, Anh Ky Huynh wrote: > On Wed, 02 Jun 2010 20:25:36 -0400 > Vinny wrote: >> On 06/02/2010 04:30, Matthew Seaman wrote: >>> archive_name=${fromarchive%-*} >> Thanks Matthew, that's really neat. It took me a long time >> to find the correct google incantation to find the documentation >> for that. ( bourne shell pattern-matching notation ) > In fact I know about that from Bash's documents:) IMHO, there are > more Bash's documents than Bourne's ones. Most of what I know about sh programming is due to close reading of the sh(1) man page and studying examples of periodic and rc scripts and the like. Quantity of documentation really is no substitute for quality. Cheers, Matthew - -- Dr Matthew J Seaman MA, D.Phil. 7 Priory Courtyard Flat 3 PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate JID: matt...@infracaninophile.co.uk Kent, CT11 9PW -BEGIN PGP SIGNATURE- Version: GnuPG/MacGPG2 v2.0.14 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkwH3hkACgkQ8Mjk52CukIwTMACePjyFyiXiEV4pAAOWPZDYI9Zy ApYAn1s91AGLUYwVinrQjSWoxPLp893D =3jcR -END PGP SIGNATURE- ___ 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: command to strip suffix in .sh script
On Wed, 02 Jun 2010 20:25:36 -0400 Vinny wrote: > On 06/02/2010 04:30, Matthew Seaman wrote: > > -BEGIN PGP SIGNED MESSAGE- > > Hash: SHA1 > > > > On 02/06/2010 09:24:01, Matthias Apitz wrote: > >> El dÃa Wednesday, June 02, 2010 a las 04:15:22PM +0800, Aiza > >> escribió: > >> > >>> I have this code > [snip] > >> $ echo 'archivename-201006021514.34.tar.gz' | sed 's/-.*$//' > > > > archive_name=${fromarchive%-*} > > > > Thanks Matthew, that's really neat. It took me a long time > to find the correct google incantation to find the documentation > for that. ( bourne shell pattern-matching notation ) In fact I know about that from Bash's documents:) IMHO, there are more Bash's documents than Bourne's ones. Cheers, -- Anh Ky Huynh ___ 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: command to strip suffix in .sh script
On 06/02/2010 04:30, Matthew Seaman wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 02/06/2010 09:24:01, Matthias Apitz wrote: El dÃa Wednesday, June 02, 2010 a las 04:15:22PM +0800, Aiza escribió: I have this code [snip] $ echo 'archivename-201006021514.34.tar.gz' | sed 's/-.*$//' archive_name=${fromarchive%-*} Thanks Matthew, that's really neat. It took me a long time to find the correct google incantation to find the documentation for that. ( bourne shell pattern-matching notation ) Sure is nice to learn something new everyday. ___ 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: command to strip suffix in .sh script
On Wed, 02 Jun 2010 16:15:22 +0800, Aiza wrote: > I have this code > > archive_name=`echo -n "${fromarchive}" | tr -c '[:alnum:]' _` > > ` is the key under Esc key and ' key is next to enter key. > > fromarchive value is archivename-201006021514.34.tar.gz > > I want to strip the suffix -201006021514.34.tar.gz from the archivename. > > The archivename can be upper and lower case letters interspersed with _ > > > Do I have syntax problem with the code? I get no error on it. > > Do I have the tr command coded correctly? > > Or should I be using something else instead of tr command? Maybe "cut" can help: % echo 'archivename-201006021514.34.tar.gz' | cut -d '-' -f 1 archivename Cutting -f 2 will give you the remaining component. % echo 'archivename-201006021514.34.tar.gz' | cut -d '-' -f 2 201006021514.34.tar.gz To cut off the suffix (.tar.gz), consider using "basename". -- 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: command to strip suffix in .sh script
On Wed, Jun 2, 2010 at 3:24 AM, Matthias Apitz wrote: > El día Wednesday, June 02, 2010 a las 04:15:22PM +0800, Aiza escribió: > > > I have this code > > > > archive_name=`echo -n "${fromarchive}" | tr -c '[:alnum:]' _` > > > > ` is the key under Esc key and ' key is next to enter key. > > > > fromarchive value is archivename-201006021514.34.tar.gz > > > > I want to strip the suffix -201006021514.34.tar.gz from the archivename. > > > > The archivename can be upper and lower case letters interspersed with _ > > > > > > Do I have syntax problem with the code? I get no error on it. > > > > Do I have the tr command coded correctly? > > > > Or should I be using something else instead of tr command? > > $ echo 'archivename-201006021514.34.tar.gz' | sed 's/-.*$//' > > One more echo 'archivename-201006021514.34.tar.gz' | awk -F - '{print $1}' -- Adam Vande More ___ 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: command to strip suffix in .sh script
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 02/06/2010 09:24:01, Matthias Apitz wrote: > El día Wednesday, June 02, 2010 a las 04:15:22PM +0800, Aiza escribió: > >> I have this code >> >> archive_name=`echo -n "${fromarchive}" | tr -c '[:alnum:]' _` >> >> ` is the key under Esc key and ' key is next to enter key. >> >> fromarchive value is archivename-201006021514.34.tar.gz >> >> I want to strip the suffix -201006021514.34.tar.gz from the archivename. >> >> The archivename can be upper and lower case letters interspersed with _ >> >> >> Do I have syntax problem with the code? I get no error on it. >> >> Do I have the tr command coded correctly? >> >> Or should I be using something else instead of tr command? > > $ echo 'archivename-201006021514.34.tar.gz' | sed 's/-.*$//' archive_name=${fromarchive%-*} Cheers, Matthew - -- Dr Matthew J Seaman MA, D.Phil. 7 Priory Courtyard Flat 3 PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate JID: matt...@infracaninophile.co.uk Kent, CT11 9PW -BEGIN PGP SIGNATURE- Version: GnuPG/MacGPG2 v2.0.14 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkwGFpoACgkQ8Mjk52CukIz2IQCfRPPnh+1+Y12iY9dz5MeO0P++ 80QAnishOceHvFymn4S9miFK8lemmlHQ =99QV -END PGP SIGNATURE- ___ 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: command to strip suffix in .sh script
El día Wednesday, June 02, 2010 a las 04:15:22PM +0800, Aiza escribió: > I have this code > > archive_name=`echo -n "${fromarchive}" | tr -c '[:alnum:]' _` > > ` is the key under Esc key and ' key is next to enter key. > > fromarchive value is archivename-201006021514.34.tar.gz > > I want to strip the suffix -201006021514.34.tar.gz from the archivename. > > The archivename can be upper and lower case letters interspersed with _ > > > Do I have syntax problem with the code? I get no error on it. > > Do I have the tr command coded correctly? > > Or should I be using something else instead of tr command? $ echo 'archivename-201006021514.34.tar.gz' | sed 's/-.*$//' HIH matthias -- Matthias Apitz t +49-89-61308 351 - f +49-89-61308 399 - m +49-170-4527211 e - w http://www.unixarea.de/ Solidarity with the zionistic pirates of Israel? Not in my name! ¿Solidaridad con los piratas sionistas de Israel? ¡No en mi nombre! ___ 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"
command to strip suffix in .sh script
I have this code archive_name=`echo -n "${fromarchive}" | tr -c '[:alnum:]' _` ` is the key under Esc key and ' key is next to enter key. fromarchive value is archivename-201006021514.34.tar.gz I want to strip the suffix -201006021514.34.tar.gz from the archivename. The archivename can be upper and lower case letters interspersed with _ Do I have syntax problem with the code? I get no error on it. Do I have the tr command coded correctly? Or should I be using something else instead of tr command? Help please. ___ 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: sh script writing help
On Sun, 30 May 2010 13:14:01 +0800, Aiza wrote: > In a .sh type script I have && exerr " very long message gt 250 char" > all on the same line. This is a real pain to edit. > > Is there some code a can use to continue this on the next line so I > can see it on the screen and still have the command function? I tried > \ with no luck. If the message does not have to be a *single* command-line argument of the exerr function, you can split the message in multiple arguments and use '\' for continuation lines, e.g.: echo "This is a very long message" \ "that does not fit in a single" \ "line of text." This might not work if you _have_ to pass the string as a single argument, but even in that case you can use shell substitution to wrap the exerr() function, e.g.: exwrap() { return exerr "$*" } exwrap "This is a very long message" \ "that does not fit in a single" \ "line of text." ___ 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: sh script writing help
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 30/05/2010 07:58:58, Aiza wrote: > Anh Ky Huynh wrote: >> On Sun, 30 May 2010 14:10:36 +0800 >> Aiza wrote: >> >>> Dan Nelson wrote: In the last episode (May 30), Aiza said: > In a .sh type script I have && exerr " very long message gt 250 > char" all on the same line. This is a real pain to edit. > > Is there some code a can use to continue this on the next line > so I can see it on the screen and still have the command > function? I tried \ with no luck. \ should work just fine: $ echo "long line \ split onto two" long line split onto two $ >>> >>> You example works only because the continuation starts at position >>> 1. >>> >>> $ [ -n "${test-name-fowarding}" -o -n "${test-noname}" ] || \ >>> exerr "\ >>> " >>> this is for ease of reading the code but will display with a bunch >>> of spaces in the middle of the sentence. The \ works fine bypassing >>> all white space between code not so for white space between the " >>> ". >>> >>> Is there a coding method to get around this? >> >> Do you try to read your expression from a file? >> exerr `cat /path/to/data` >> >> the contents of /path/to/data are your very long string. A standard means of doing this sort of thing is to use a 'Here' document: if ! [ -n "${test-name-forwarding}" -o -n "${test-noname}" ]; then exerr <<-E_O_EXERR x xx xx xxx E_O_EXER fi The '<<-' operator is a variant on the usual '<<' operator used for this: the only difference is that it strips leading tabs from the lines in the here document. Cheers, Matthew - -- Dr Matthew J Seaman MA, D.Phil. 7 Priory Courtyard Flat 3 PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate JID: matt...@infracaninophile.co.uk Kent, CT11 9PW -BEGIN PGP SIGNATURE- Version: GnuPG/MacGPG2 v2.0.14 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkwCEjQACgkQ8Mjk52CukIzc7gCbBalo5oJcsw+jkXHOm4+8qU3f l2EAn30WwaSL3kTZ1iy3FGkTxQlBZ3pr =WxjG -END PGP SIGNATURE- ___ 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: sh script writing help
Anh Ky Huynh wrote: On Sun, 30 May 2010 14:10:36 +0800 Aiza wrote: Dan Nelson wrote: In the last episode (May 30), Aiza said: In a .sh type script I have && exerr " very long message gt 250 char" all on the same line. This is a real pain to edit. Is there some code a can use to continue this on the next line so I can see it on the screen and still have the command function? I tried \ with no luck. \ should work just fine: $ echo "long line \ split onto two" long line split onto two $ You example works only because the continuation starts at position 1. $ [ -n "${test-name-fowarding}" -o -n "${test-noname}" ] || \ exerr "\ " this is for ease of reading the code but will display with a bunch of spaces in the middle of the sentence. The \ works fine bypassing all white space between code not so for white space between the " ". Is there a coding method to get around this? Do you try to read your expression from a file? exerr `cat /path/to/data` the contents of /path/to/data are your very long string. Regards, Thanks I never though of that solution. ___ 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: sh script writing help
On Sun, 30 May 2010 14:10:36 +0800 Aiza wrote: > Dan Nelson wrote: > > In the last episode (May 30), Aiza said: > >> In a .sh type script I have && exerr " very long message gt 250 > >> char" all on the same line. This is a real pain to edit. > >> > >> Is there some code a can use to continue this on the next line > >> so I can see it on the screen and still have the command > >> function? I tried \ with no luck. > > > > \ should work just fine: > > > > $ echo "long line \ > > split onto two" > > long line split onto two > > $ > > > > > You example works only because the continuation starts at position > 1. > > $ [ -n "${test-name-fowarding}" -o -n "${test-noname}" ] || \ > exerr "\ > " > this is for ease of reading the code but will display with a bunch > of spaces in the middle of the sentence. The \ works fine bypassing > all white space between code not so for white space between the " > ". > > Is there a coding method to get around this? Do you try to read your expression from a file? exerr `cat /path/to/data` the contents of /path/to/data are your very long string. Regards, -- Anh Ky Huynh ___ 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: sh script writing help
Dan Nelson wrote: In the last episode (May 30), Aiza said: In a .sh type script I have && exerr " very long message gt 250 char" all on the same line. This is a real pain to edit. Is there some code a can use to continue this on the next line so I can see it on the screen and still have the command function? I tried \ with no luck. \ should work just fine: $ echo "long line \ split onto two" long line split onto two $ You example works only because the continuation starts at position 1. $ [ -n "${test-name-fowarding}" -o -n "${test-noname}" ] || \ exerr "\ " this is for ease of reading the code but will display with a bunch of spaces in the middle of the sentence. The \ works fine bypassing all white space between code not so for white space between the " ". Is there a coding method to get around this? ___ 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: sh script writing help
In the last episode (May 30), Aiza said: > In a .sh type script I have && exerr " very long message gt 250 char" > all on the same line. This is a real pain to edit. > > Is there some code a can use to continue this on the next line so I can > see it on the screen and still have the command function? I tried \ with > no luck. \ should work just fine: $ echo "long line \ split onto two" long line split onto two $ -- Dan Nelson dnel...@allantgroup.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"
sh script writing help
In a .sh type script I have && exerr " very long message gt 250 char" all on the same line. This is a real pain to edit. Is there some code a can use to continue this on the next line so I can see it on the screen and still have the command function? I tried \ with no luck. thanks ___ 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: sh script difficulties (running parallel functions)
In the last episode (Nov 01), David Naylor said: > Hi, > > I am having a hard time getting (very complex script for me) to work. The > basic idea is that this script runs a bunch of tarkets, many of which are > time consuming but low on resources (such as downloading files). Now if I > run the tarkets all at once (given some dependancy issues) it greatly speeds > up the process (about 5 time speed increase). However I do not know how to > do this using sh... > > Example > > #!/bin/sh > > worker1() { > # Copy some files > } > > worker2() { > # Download some files > } > > worker3() { > # Do something else > } > > . # and so on > > run_jobs() { > worker1 & > worker2 & > worker3 & > # !!! Somehow wait for over workers to finish before continuing !!! > } You can use the "wait" shell builtin to wait for all children. > #Finished > > Furthermore, how can signals be handled such that the signals get > accumulated and once all the other workers have finished the signals get > passed on (appropriately) You mean trapping ^C in the parent so that you can't stop the script until all the workers are done? That's more complicated to do in a shell script. Masking ^C completely is easy using the "trap" builtin , but catching it and then doing a "kill -INT $$" after all your workers have completed is more complicated since the "wait" command will exit when a signal is received, and I don't think it will tell you why it exited (all children done, or got a signal). -- Dan Nelson [EMAIL PROTECTED] ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: sh script difficulties (running parallel functions)
On Thu, 1 Nov 2007, David Naylor wrote: > Hi, > > I am having a hard time getting (very complex script for me) to work. The > basic idea is that this script runs a bunch of tarkets, many of which are > time consuming but low on resources (such as downloading files). Now if I > run the tarkets all at once (given some dependancy issues) it greatly speeds > up the process (about 5 time speed increase). However I do not know how to > do this using sh... > > Example > > #!/bin/sh > > worker1() { > # Copy some files > } > > worker2() { > # Download some files > } > > worker3() { > # Do something else > } > > . # and so on > > run_jobs() { > worker1 & > worker2 & > worker3 & > # !!! Somehow wait for over workers to finish before continuing !!! > } > > #Finished > > Furthermore, how can signals be handled such that the signals get > accumulated and once all the other workers have finished the signals get > passed on (appropriately) The "wait" shell builtin is part of what you're after. You probably will need to trap the signals you're interested in catching. Just a trap 'int=1' INT wait trap - INT if [ "x$int" = x1 ]; then ... ; fi should do it. -- jan grant, ISYS, University of Bristol. http://www.bris.ac.uk/ Tel +44 (0)117 3317661 http://ioctl.org/jan/ There's no convincing English-language argument that this sentence is true. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: sh script difficulties (running parallel functions)
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 11/1/07, David Naylor wrote: > # !!! Somehow wait for over workers to finish before continuing !!! > } > > #Finished > > Furthermore, how can signals be handled such that the signals get > accumulated and once all the other workers have finished the signals get > passed on (appropriately) One simplistic way of doing something like that is to make each worker process write a file out to disk upon completion. Then have a while loop that sleeps for a minute and then checks for the existence of that file and if it finds it, execute other commands. Probably not the cleanest method, but I think you'll find that handling forks and signals in a shell script is more trouble than it's worth. - -- Andy Harrison public key: 0x67518262 -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.5 (GNU/Linux) Comment: http://firegpg.tuxfamily.org iD8DBQFHKgQ1NTm8fWdRgmIRAn7eAKCZthrDzv0j7J6urphY3ohm6bSPZgCeIAt6 vhC2Zxw0ZTxw8eT+NZ/Uktg= =schR -END PGP SIGNATURE- ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
sh script difficulties (running parallel functions)
Hi, I am having a hard time getting (very complex script for me) to work. The basic idea is that this script runs a bunch of tarkets, many of which are time consuming but low on resources (such as downloading files). Now if I run the tarkets all at once (given some dependancy issues) it greatly speeds up the process (about 5 time speed increase). However I do not know how to do this using sh... Example #!/bin/sh worker1() { # Copy some files } worker2() { # Download some files } worker3() { # Do something else } . # and so on run_jobs() { worker1 & worker2 & worker3 & # !!! Somehow wait for over workers to finish before continuing !!! } #Finished Furthermore, how can signals be handled such that the signals get accumulated and once all the other workers have finished the signals get passed on (appropriately) Thank you. David ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
RE: unexpected result from sh script with `date`
On Fri, 2 Feb 2007 22:11:56 +1100, Tigger <[EMAIL PROTECTED]> wrote > Hello, the following simply sh script is outputting unexpected results. > Any idea why? > --script-- > #!/bin/sh > started=`date` > echo "Started at: $started" > echo "Finished : "`date` > exit > --output-- > Started at: Fri Feb 2 22:13:51 EST 2007 > Finished : Fri Feb 2 22:13:51 EST 2007 > --problem-- > Between 'Feb' and '2', there is two spaces on the 'Started at' line, > however the 'Finished' one only has 1 space. > I know this sounds picky, but I was not expecting this at all. It is not a problem with date , it did the same thing both times. The echo command coalesces consecutive blank space characters down to one blank unless they are quoted. If you change the line echo "Finished : "`date` to be echo "Finished : `date`" it will do what you expect. [snip] ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: unexpected result from sh script with `date`
On Fri, Feb 02, 2007 at 01:48:31PM +0100, J65nko wrote: > On 2/2/07, Tigger <[EMAIL PROTECTED]> wrote: > >Hello, the following simply sh script is outputting unexpected results. > >Any idea why? > > > >--script-- > > > >#!/bin/sh > > > >started=`date` > > > >echo "Started at: $started" > >echo "Finished : "`date` > >exit > > > >--output-- > > > >Started at: Fri Feb 2 22:13:51 EST 2007 > >Finished : Fri Feb 2 22:13:51 EST 2007 > > > >--problem-- > > > >Between 'Feb' and '2', there is two spaces on the 'Started at' line, > >however the 'Finished' one only has 1 space. > > > >I know this sounds picky, but I was not expecting this at all. > > > >uname -a > >FreeBSD piglet 6.2-STABLE FreeBSD 6.2-STABLE #0: Fri Jan 19 04:13:20 EST > >2007 [EMAIL PROTECTED]:/usr/obj/usr/src/sys/PIGLET i386 > > The same on OpenBSD here (ksh) > OpenBSD 4.0-current (GENERIC) #1194: Thu Nov 2 16:32:12 MST 2006 >[EMAIL PROTECTED]:/usr/src/sys/arch/i386/compile/GENERIC > > It seems to depend whether the command substitution is within the > quote-delimited string, for 'echo' or outside that string, in other > words on its own. > --- script-- > #!/bin/sh > > started=$(date) > > echo "\$started within \" delimited string for echo" > echo "Started at: $started" > echo "Command substitution \$(date) within \" delimited string for echo" > echo "Finished : $(date)" > echo "Command substitution \$(date) outside \" delimited string for echo" > echo "Finished : "$(date) > echo "Command substitution \`date\` outside \" delimited string for echo" > echo "Finished : "$(date) > --- > Output: > --- > $started within " delimited string for echo > Started at: Fri Feb 2 13:46:07 CET 2007 > Command substitution $(date) within " delimited string for echo > Finished : Fri Feb 2 13:46:07 CET 2007 > Command substitution $(date) outside " delimited string for echo > Finished : Fri Feb 2 13:46:07 CET 2007 > Command substitution `date` outside " delimited string for echo > Finished : Fri Feb 2 13:46:07 CET 2007 > --- > Embedded inside the string there are two spaces between Feb and the 2, > as "stand-alone" there is only one space. > > Strange indeed ;) Not strange at all if think about how 'echo' works and how the arguments are passed to it. If you do a echo `date` then echo will be passed 6 arguments ('Fri' 'Feb' '2' '13:46:07' 'CET' '2007') and print them with a single space between each of them. If you do a echo "`date`" then echo will be passed only a single argument ('Fri Feb 2 13:46:07 CET 2007') which will be printed unmodified. As another example compare the outputs of echo a b c and echo "a b c" -- Erik Trulsson [EMAIL PROTECTED] ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: unexpected result from sh script with `date`
On 2/2/07, Tigger <[EMAIL PROTECTED]> wrote: Hello, the following simply sh script is outputting unexpected results. Any idea why? --script-- #!/bin/sh started=`date` echo "Started at: $started" echo "Finished : "`date` exit --output-- Started at: Fri Feb 2 22:13:51 EST 2007 Finished : Fri Feb 2 22:13:51 EST 2007 --problem-- Between 'Feb' and '2', there is two spaces on the 'Started at' line, however the 'Finished' one only has 1 space. I know this sounds picky, but I was not expecting this at all. uname -a FreeBSD piglet 6.2-STABLE FreeBSD 6.2-STABLE #0: Fri Jan 19 04:13:20 EST 2007 [EMAIL PROTECTED]:/usr/obj/usr/src/sys/PIGLET i386 The same on OpenBSD here (ksh) OpenBSD 4.0-current (GENERIC) #1194: Thu Nov 2 16:32:12 MST 2006 [EMAIL PROTECTED]:/usr/src/sys/arch/i386/compile/GENERIC It seems to depend whether the command substitution is within the quote-delimited string, for 'echo' or outside that string, in other words on its own. --- script-- #!/bin/sh started=$(date) echo "\$started within \" delimited string for echo" echo "Started at: $started" echo "Command substitution \$(date) within \" delimited string for echo" echo "Finished : $(date)" echo "Command substitution \$(date) outside \" delimited string for echo" echo "Finished : "$(date) echo "Command substitution \`date\` outside \" delimited string for echo" echo "Finished : "$(date) --- Output: --- $started within " delimited string for echo Started at: Fri Feb 2 13:46:07 CET 2007 Command substitution $(date) within " delimited string for echo Finished : Fri Feb 2 13:46:07 CET 2007 Command substitution $(date) outside " delimited string for echo Finished : Fri Feb 2 13:46:07 CET 2007 Command substitution `date` outside " delimited string for echo Finished : Fri Feb 2 13:46:07 CET 2007 --- Embedded inside the string there are two spaces between Feb and the 2, as "stand-alone" there is only one space. Strange indeed ;) J65nko ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
unexpected result from sh script with `date`
Hello, the following simply sh script is outputting unexpected results. Any idea why? --script-- #!/bin/sh started=`date` echo "Started at: $started" echo "Finished : "`date` exit --output-- Started at: Fri Feb 2 22:13:51 EST 2007 Finished : Fri Feb 2 22:13:51 EST 2007 --problem-- Between 'Feb' and '2', there is two spaces on the 'Started at' line, however the 'Finished' one only has 1 space. I know this sounds picky, but I was not expecting this at all. uname -a FreeBSD piglet 6.2-STABLE FreeBSD 6.2-STABLE #0: Fri Jan 19 04:13:20 EST 2007 [EMAIL PROTECTED]:/usr/obj/usr/src/sys/PIGLET i386 -Tig ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: Need /bin/sh script help
Garrett Cooper wrote: Hello again all, Just making a series of sh scripts to help automate updating and whatnot of my fileserver (since I am trying to avoid having mistakes occur with my system, and maybe help the community out a bit by providing some decent means of updating their own machines), and I was wondering if anyone could help me out with the following script I've developing (the grep if statements are incorrect..): #!/bin/sh # KC=""; cd /usr/src; if [ -n `grep -e s/KERNCONF=/ /etc/make.conf` ] # want to look for KERNCONF in /etc/make.conf then echo "enter in the kernel conf file full pathname:"; read KERNCONF; KC="KERNCONF=$KERNCONF"; fi if [ -n `grep -e s/NO_CLEAN=*yes*/ /etc/make.conf` ] // want to look for NO_CLEAN in /etc/make.conf -- is this really necessary? then cd sys; echo "cleaning sources" make clean; make cleandir; cd ..; fi echo "building kernel"; make buildkernel $KC; echo "installing kernel"; make installkernel $KC; echo "kernel compile complete. reboot to try new kernel"; TIA, -Garrett Thank you all for the replies and the advice. I still need to fix the grepping, but I hope that any and all problems will be resolved soon once I stick my mind to it. As for what worked and was changed, what didn't, and why I am doing this: What worked/was changed: -Removing C++ style comment. Lol. -Adding in a loop for read so the kernconf variable is not set to null. What didn't work: -Using make -V (it came up with nothing... oddly enough even though the manpage said it would work). -grep call when I didn't redirect stuff to stderr (suppose I should do that). Why I am doing this: If you're probably reading the mailing list from time to time, you'll have noticed I had some issues last week with cvsup and my system sources. I've basically come to the conclusion that the cause for my issue is the fact that I had a cron job which would update my system/kernel sources, as well as my ports, and my system/kernel sources were synced DURING a build, which lead to quite a few issues with the build and installworld, I discovered after rebooting the machine the next morning (no errors were encountered though, which was odd). So, in order to scratch the itch, persay, that exists with cvsup and building ports/compiling system stuff, I have created a script which does 'lock' a process, if noted correctly, with a 'semaphore lockfile'. Maybe a call to ps aux would be better, but I found that dealing with a lockfile system would be a much easier way to solve things. Todo Plan: Add an rc-script or something that will run in single-user mode which will help with mergemaster. People should be at the machine when doing this, and my scripts were originally designed so that a person could just run the script and it would do function X on a timed basis (say as a cron job). These scripts aren't meant to replace any resources; they're just here to help automate junk and ensure that things DO get done properly and things DO get updated, with less typing and command memorizing for the admin (I will properly annotate which chapters in the handbook to use and manpages to read for a more in depth reference of what the script does and how it does it). Furthermore, I plan on possibly coming out with a more up to date installcd setup that would have an 'admin pack' as I would call it, or apache, bind9, perl, mysql, ipfw, samba, etc, properly configured with a set of CGI pages that would help simplify system management for people (I am doing this as a project for the house/apt I'm living in since they need a NAT box with extras). So, that's what I have in mind. Questions, comments :) ? -Garrett ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: Need /bin/sh script help
On Tue, 11 Apr 2006, [EMAIL PROTECTED] wrote: > On Mon, 10 Apr 2006 22:30:32 -0700 Garrett Cooper wrote (my brief response > follows all of his text): > > Just making a series of sh scripts to help automate updating and > whatnot of my fileserver (since I am trying to avoid having mistakes > occur with my system, and maybe help the community out a bit by > providing some decent means of updating their own machines), and I was > wondering if anyone could help me out with the following script I've > developing (the grep if statements are incorrect..): > I see a problem in the line > if [ -n `grep -e s/KERNCONF=/ /etc/make.conf` ] # want to look for > you should have double-quotes around the `grep ... conf` > because it is likely to produce more than one token and so the > [ -n ... ] statement violates the syntax (there should be exactly 1 > token between the -n and the ] , even no token there is an error, the > way that is handled is to quote it. I am writing this quickly without > bringing up my FreeBSD system to check it. Good luck. Or simply use the error status of grep, no "[" invocation: if grep -q ... then ... fi Note that if you're looking at automating the update process you should probably pay careful attention to the world/kernel update process described in the handbook; there are steps (like an initial mergemaster -p) that you will want to include. -- jan grant, ISYS, University of Bristol. http://www.bris.ac.uk/ Tel +44 (0)117 3317661 http://ioctl.org/jan/ If you have received this email in error, do whatever the hell you want with it. It's not like I can stop you anyway. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: Need /bin/sh script help
On Mon, 10 Apr 2006 22:30:32 -0700 Garrett Cooper wrote (my brief response follows all of his text): Just making a series of sh scripts to help automate updating and whatnot of my fileserver (since I am trying to avoid having mistakes occur with my system, and maybe help the community out a bit by providing some decent means of updating their own machines), and I was wondering if anyone could help me out with the following script I've developing (the grep if statements are incorrect..): #!/bin/sh # KC=""; cd /usr/src; if [ -n `grep -e s/KERNCONF=/ /etc/make.conf` ] # want to look for KERNCONF in /etc/make.conf then echo "enter in the kernel conf file full pathname:"; read KERNCONF; KC="KERNCONF=$KERNCONF"; fi if [ -n `grep -e s/NO_CLEAN=*yes*/ /etc/make.conf` ] // want to look for NO_CLEAN in /etc/make.conf -- is this really necessary? then cd sys; echo "cleaning sources" make clean; make cleandir; cd ..; fi echo "building kernel"; make buildkernel $KC; echo "installing kernel"; make installkernel $KC; echo "kernel compile complete. reboot to try new kernel"; TIA, -Garrett I see a problem in the line if [ -n `grep -e s/KERNCONF=/ /etc/make.conf` ] # want to look for you should have double-quotes around the `grep ... conf` because it is likely to produce more than one token and so the [ -n ... ] statement violates the syntax (there should be exactly 1 token between the -n and the ] , even no token there is an error, the way that is handled is to quote it. I am writing this quickly without bringing up my FreeBSD system to check it. Good luck. Another thing you can do to avoid quoting (and the long strings that may result) is use the -c option of grep and check the number resulting. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: Need /bin/sh script help
in message <[EMAIL PROTECTED]>, wrote Garrett Cooper thusly... > > I was wondering if anyone could help me out with the following > script I've developing (the grep if statements are incorrect..): > > #!/bin/sh > # > > KC=""; > > cd /usr/src; > > if [ -n `grep -e s/KERNCONF=/ /etc/make.conf` ] # want to look for Well, you did not write what exactly is the problem, as executing the above works as expected. Why do have 'KERNCONF' surrounded by 's/' & '/'? In any case, there is not need to see if the returned string length is nonzero. Just use the grep return code & send the all the output to /dev/null ... if grep -e '^KERNCONF=' /etc/make.conf >/dev/null 2>&1 then ... fi ... or you could also use '-q' and/or '-s' grep options instead of sending output to /dev/null. (I personally would do a bit more strict check to see $KERNCONF is set to value containing characters meeting some criteria, say '\<[-._[:alnum:]]+\>'.) > KERNCONF in /etc/make.conf > then >echo "enter in the kernel conf file full pathname:"; I did not know one can specify the kernel configuration path, not just the basename. >read KERNCONF; >KC="KERNCONF=$KERNCONF"; > fi > > if [ -n `grep -e s/NO_CLEAN=*yes*/ /etc/make.conf` ] // want to look for ^^ ^^ Wrong kind of comment character. See above comments about grep(1) usage. - Parv -- ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: Need /bin/sh script help
On Tuesday 11 April 2006 06:30, Garrett Cooper wrote: > cd /usr/src; > if [ -n `grep -e s/KERNCONF=/ /etc/make.conf` ] # want to look for > KERNCONF in /etc/make.conf if [ `make -V KERNCONF` ] > read KERNCONF; > KC="KERNCONF=$KERNCONF"; > fi You need to check that KC actually exists. IIRC a typo will cause GENERIC to build. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Need /bin/sh script help
Hello again all, Just making a series of sh scripts to help automate updating and whatnot of my fileserver (since I am trying to avoid having mistakes occur with my system, and maybe help the community out a bit by providing some decent means of updating their own machines), and I was wondering if anyone could help me out with the following script I've developing (the grep if statements are incorrect..): #!/bin/sh # KC=""; cd /usr/src; if [ -n `grep -e s/KERNCONF=/ /etc/make.conf` ] # want to look for KERNCONF in /etc/make.conf then echo "enter in the kernel conf file full pathname:"; read KERNCONF; KC="KERNCONF=$KERNCONF"; fi if [ -n `grep -e s/NO_CLEAN=*yes*/ /etc/make.conf` ] // want to look for NO_CLEAN in /etc/make.conf -- is this really necessary? then cd sys; echo "cleaning sources" make clean; make cleandir; cd ..; fi echo "building kernel"; make buildkernel $KC; echo "installing kernel"; make installkernel $KC; echo "kernel compile complete. reboot to try new kernel"; TIA, -Garrett ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: help with sh script
On 03 jul 2005, at 19:03, fbsd_user wrote: On Sun, 3 Jul 2005 12:14:05 -0400 "fbsd_user" <[EMAIL PROTECTED]> wrote: Thanks but I need a little more help. num_ip="(printf $raw_ip | sed 's/\.//g')" gives me a error. What would the correct syntax be? I am trying to write script to insert rules into PF firewall on 5.4. using pf anchors. Hello, The problem here is that num_ip="(printf $raw_ip | sed 's/\.//g')" makes num_ip equal to (printf $raw_ip | sed 's/\.//g') instead of its output. To assign the output of a command use "`": num_ip=`(printf $raw_ip | sed 's/\.//g')` Also the subshell (the "()") is not needed: num_ip=`printf $raw_ip | sed 's/\.//g'` Hope that helps. Best Regards, Ale Thanks that was just what I needed. Now building on that I tried this std_text='No ALTQ support in kernel ALTQ related functions disabled' ret_ob=`(echo $outrule) | pfctl -a doorman_ob:$session_name_ob -f - 2>&1` ret_ob=`printf $ret_ob | sed 's/\$std_text//g'` The goal here is to remove the std_text from the output of the pftctl command. I get this error "printf missing format character" Does sed need different syntax or have I got it all wrong? printf needs " surrounding the arguments: printf "$ret_ob" is the right syntax... Arno ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: help with sh script
On Sun, Jul 03, 2005 at 01:03:40PM -0400, fbsd_user wrote: > > I get this error "printf missing format character" > > Does sed need different syntax or have I got it all wrong? Issue the following command and be enlightend: man 1 printf Or just use 'echo' instead. Roland -- R.F.Smith (http://www.xs4all.nl/~rsmith/) Please send e-mail as plain text. public key: http://www.xs4all.nl/~rsmith/pubkey.txt pgp6WWWemkY7Y.pgp Description: PGP signature
Re: help with sh script
On 2005-07-03 09:39, fbsd_user <[EMAIL PROTECTED]> wrote: > What is the sh coding to strip the periods from a IP address?? > > raw_ip='10.0.10.5' this is starting > num_ip='100105'and this is what I need to convert to. There are many ways: echo "${raw_ip}" | sed -e 's/\.//g' echo "${raw_ip}" | perl -pe 's/\.//g' echo "${raw_ip}" | awk -F. '{gsub("\\.", ""); print}' ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: help with sh script
On Sun, 3 Jul 2005 14:59:32 -0400 "fbsd_user" <[EMAIL PROTECTED]> wrote: > > > std_text='No ALTQ support in kernel ALTQ related functions disabled' > ret_ob='No ALTQ support in kernel ALTQ related functions disabled > OK' > > ret_ob=`printf "$ret_ob" | sed 's/\$std_text//g'` > Does not strip off the std_text stuff. > > How would I code a statement to remove everything from $ret_ob > but the ok at the end so $ret_ob would only contain the ok?? > > Some times $ret_ob will end in some error message and that is > what I want to capture after striping off the std_text. > > > Thanks > Hello, The problem here is that single quotes ("'") avoid variable substitution. e.g. var="text" echo $var # outputs text echo '$var' # outputs $var (literally) Also the backslash avoids variable substitution when placed before a "$". e.g. echo $var # outputs text echo \$var # outputs $var (literally) The solution is this: ret_ob=`printf "$ret_ob" | sed "s/$std_text//g"` ^ ^ ^ Hope that helps. Best Regards, Ale ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: help with sh script
On 03 jul 2005, at 20:59, fbsd_user wrote: std_text='No ALTQ support in kernel ALTQ related functions disabled' ret_ob='No ALTQ support in kernel ALTQ related functions disabled OK' ret_ob=`printf "$ret_ob" | sed 's/\$std_text//g'` Does not strip off the std_text stuff. How would I code a statement to remove everything from $ret_ob but the ok at the end so $ret_ob would only contain the ok?? Some times $ret_ob will end in some error message and that is what I want to capture after striping off the std_text. Thanks hmm try this then: ret_ob=`printf "$ret_ob" | tr -d "$std_text"` Arno ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"