Multiple mount_smbfs commands fail in bash script
Hi all, I'm having some problems with a bash script. It's a backup script that periodically checks if a list of systems is online, and if so, uses samba to mount a specified list of shares, rsyncs them to a local directory and unmounts again. This used to run fine till a few months ago (I don't know what the trigger was that caused them to first fail). Now, when the script is run, it gives the following error when mounting the shares: mount_smbfs: can't get handle to requester (no /dev/nsmb* device) Which is strange, as there are (by last count) 1170 /dev/nsmb* devices in /dev/ (is that normal?) Searching the internet, FreeBSD and Samba mailing lists gave me no recent info, and the old info wasn't helpful. I've narrowed it down to the point where I think it's caused by one process trying to open two (or more) shares at the same time. (a simple script mounting two shares gives the same error). I can mount the shares from the command line without problems, it's only in the bash script it gives me problems. ~/.nsmbrc and /etc/nsmb.conf are correct, smbd, nmbd and winbindd are running. The system is FreeBSD 8.0 Stable. Anyone got any suggestions? Regards, Bernard ___ 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: Multiple mount_smbfs commands fail in bash script
On Thu, 02 Sep 2010 10:24:05 +0200 Bernard Scharp freebsd-questi...@itsacon.net articulated: Hi all, I'm having some problems with a bash script. It's a backup script that periodically checks if a list of systems is online, and if so, uses samba to mount a specified list of shares, rsyncs them to a local directory and unmounts again. This used to run fine till a few months ago (I don't know what the trigger was that caused them to first fail). Now, when the script is run, it gives the following error when mounting the shares: mount_smbfs: can't get handle to requester (no /dev/nsmb* device) Which is strange, as there are (by last count) 1170 /dev/nsmb* devices in /dev/ (is that normal?) Searching the internet, FreeBSD and Samba mailing lists gave me no recent info, and the old info wasn't helpful. I've narrowed it down to the point where I think it's caused by one process trying to open two (or more) shares at the same time. (a simple script mounting two shares gives the same error). I can mount the shares from the command line without problems, it's only in the bash script it gives me problems. ~/.nsmbrc and /etc/nsmb.conf are correct, smbd, nmbd and winbindd are running. The system is FreeBSD 8.0 Stable. Anyone got any suggestions? Could you post the script? Anything else would be pure guess work. You also might consider posting this on the BASH mail forum: bug-b...@gnu.org although you might have to subscribe first: http://lists.gnu.org/mailman/listinfo/bug-bash -- Jerry ✌ freebsd.u...@seibercom.net Disclaimer: off-list followups get on-list replies or get ignored. Please do not ignore the Reply-To header. __ Vital papers will demonstrate their vitality by spontaneously moving from where you left them to where you can't find them. ___ 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: Multiple mount_smbfs commands fail in bash script
Could you post the script? Anything else would be pure guess work. You Well, I can recreate it with something as simple as: #!/usr/local/bin/bash mount_smbfs //u...@remotehost/share1/ /tmp/mnt/ mount_smbfs //u...@remotehost/share2/ /tmp/mnt2/ also might consider posting this on the BASH mail forum: bug-b...@gnu.org although you might have to subscribe first: http://lists.gnu.org/mailman/listinfo/bug-bash I'l look into that, (though I doubt this is a bash issue). Thanks! Bernard ___ 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: Multiple mount_smbfs commands fail in bash script
On Thu, 02 Sep 2010 14:02:35 +0200, Bernard Scharp freebsd-questi...@itsacon.net wrote: Could you post the script? Anything else would be pure guess work. You Well, I can recreate it with something as simple as: #!/usr/local/bin/bash mount_smbfs //u...@remotehost/share1/ /tmp/mnt/ mount_smbfs //u...@remotehost/share2/ /tmp/mnt2/ Excuse me, it may just be a stupid question... but... why do you use bash for this purpose? Do you require any special bash feature that cannot be done using the standard shell, sh? I often see the urge to use bash for scripting as a typical Linuxism, which is usually non-portable (if that was your goal). FreeBSD's standard scripting shell is sh, so why not use it until you reach the ends of its functionality? Just a guess, regarding your initial question, as I don't have experience with Windows related things: Did you have the chance to monitor correct operations of your script in the past? Did the mound and umount (!) calls work properly? Have you checked your commands running them in the standard dialog shell (csh)? I assume you're running them as root (or at least with sufficient permissions), so I don't think the problem is there, as the error message mount_smbfs: can't get handle to requester (no /dev/nsmb* device) doesn't look like refering to that problem. The error message originates from /usr/src/contrib/smbfs/lib/smb/ctx.c; having a look around, and remembering that you said [...] there are (by last count) 1170 /dev/nsmb* devices in /dev/ (is that normal?) I found smb_ctx_gethandle() near line 600 (version 7 OS here): /* * well, no clone capabilities available - we have to scan * all devices in order to get free one */ for (i = 0; i 1024; i++) { snprintf(buf, sizeof(buf), /dev/%s%d, NSMB_NAME, i); fd = open(buf, O_RDWR); if (fd = 0) { ctx-ct_fd = fd; return 0; } } The limit seems to be 1024, if I read that correctly - allthough I'm considered a C hacker, I'm no OS-level C hacker. :-) Afterwards, smb_ctx_lookup() fails and gives the error message mentioned earlier. Remove the /dev/nsmb* devices and try again. Make sure no other SMB stuff is currently mounted, just to be sure, as I don't have any idea what could fail. -- 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: Multiple mount_smbfs commands fail in bash script
On 02/09/2010 15:29, Polytropon wrote: On Thu, 02 Sep 2010 14:02:35 +0200, Bernard Scharp freebsd-questi...@itsacon.net wrote: Could you post the script? Anything else would be pure guess work. You Well, I can recreate it with something as simple as: #!/usr/local/bin/bash mount_smbfs //u...@remotehost/share1/ /tmp/mnt/ mount_smbfs //u...@remotehost/share2/ /tmp/mnt2/ Excuse me, it may just be a stupid question... but... why do you use bash for this purpose? Do you require any special bash feature that cannot be done using the standard shell, sh? I often see the urge to use bash for scripting as a typical Linuxism, which is usually non-portable (if that was your goal). FreeBSD's standard scripting shell is sh, so why not use it until you reach the ends of its functionality? The script above is a (heavily) reduced version, used to isolate the problem. The real script is much longer, and uses a bunch of logic to walk through a list of different systems (each with their own lists of shares, loaded from external files), taking snapshots of the previous backup, logging which systems were backed up, rolling back operations if a backup fails, etc. Just a guess, regarding your initial question, as I don't have experience with Windows related things: Did you have the chance to monitor correct operations of your script in the past? Did the mound and umount (!) calls work properly? Have you checked your commands running them in the standard dialog shell (csh)? I assume you're running them as root (or at least with sufficient permissions), so I don't think the problem is there, as the error message mount_smbfs: can't get handle to requester (no /dev/nsmb* device) doesn't look like refering to that problem. I am running it as root, and I just tried running the (test)script (without the bash reference) under a csh shell, and got the same error, so it's not a bash problem. As for monitoring the operations of the script, it has worked fine before (for several years), so I'm pretty sure the code is correct. The error message originates from /usr/src/contrib/smbfs/lib/smb/ctx.c; having a look around, and remembering that you said [...] there are (by last count) 1170 /dev/nsmb* devices in /dev/ (is that normal?) I found smb_ctx_gethandle() near line 600 (version 7 OS here): /* * well, no clone capabilities available - we have to scan * all devices in order to get free one */ for (i = 0; i 1024; i++) { snprintf(buf, sizeof(buf), /dev/%s%d, NSMB_NAME, i); fd = open(buf, O_RDWR); if (fd = 0) { ctx-ct_fd = fd; return 0; } } The limit seems to be 1024, if I read that correctly - allthough I'm considered a C hacker, I'm no OS-level C hacker. :-) Neither am I. Hadn't even thought of grepping in /usr/src for the error message :-) Afterwards, smb_ctx_lookup() fails and gives the error message mentioned earlier. Remove the /dev/nsmb* devices and try again. Make sure no other SMB stuff is currently mounted, just to be sure, as I don't have any idea what could fail. Can I just `rm /dev/nsmbX` them? (messing in /dev/ is a level of FreeBSD I'm not familiar with) Thanks for all your help! Bernard ___ 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: Multiple mount_smbfs commands fail in bash script
On Thu, 02 Sep 2010 15:52:25 +0200, Bernard Scharp freebsd-questi...@itsacon.net wrote: Neither am I. Hadn't even thought of grepping in /usr/src for the error message :-) It's often a good starting point to see where problems might be caused from. Can I just `rm /dev/nsmbX` them? (messing in /dev/ is a level of FreeBSD I'm not familiar with) Yes, I would guess so. The content of /dev/ is dynamically generated since FreeBSD 5, if I remember correctly. As the nsmb nodes don't seem to be in use any longer, it would be no problem to remove them. The mount_smbfs program will generate them if needed. Just as an addition: After your script successfully performed the operations needing the mounted SMB shares, it could remove the corresponding device files. Still, this looks like a bug to me, a can't image anybody needs more than 1024 of them kind of bug. I would have imagined that IF a program needs files in a temporary way, it removes them after use. Just to be sure, unmount all SMB related things, as I can't predict what would happen if a nsmb device disappears when in use. -- 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
Bash Script Help - File Names With Spaces
I have a collection of yearly top 100 Billboard mp3s in this format (all one line - sorry if it wraps): /archive/Multimedia/Audio/Music/Billboard Top USA Singles/1980-028 Kenny Loggins - This Is It.mp3 I want to create symbolic links to the top 30 in 1966-1969 in another directory for easy migration to a flash card. Thus I invoked 'find' to get a list (again, all one line): find -E /archive/Multimedia/Audio/Music/Billboard Top USA Singles -regex '.*19[6-9][0-9]-0[0-2][0-9].*' (OK, I know this will only return the top 29) 'find' returns the complete filename as above: /archive/Multimedia/Audio/Music/Billboard Top USA Singles/1980-028 Kenny Loggins - This Is It.mp3 Then I attempt to use 'basename' to extract the file name to a variable which I can later pass to 'ln'. This seems to work: basename /archive/Multimedia/Audio/Music/Billboard Top USA Singles/1980-028 Kenny Loggins - This Is It.mp3 returns (all one line): 1980-028 Kenny Loggins - This Is It.mp3 which is what I would expect. However using it with 'find' give me this type of unexpected result: for i in `find -E /archive/Multimedia/Audio/Music/Billboard Top USA Singles -regex '.*19[6-9][0-9]-0[1-2][0-9].*'`; do basename ${i};done 1980-028 Kenny Loggins - This Is It.mp3 Why is this different? And more importantly, how can I capture the file name to $i? Thanks, Drew -- Like card tricks? Visit The Alchemist's Warehouse to learn card magic secrets for free! http://alchemistswarehouse.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: Bash Script Help - File Names With Spaces
Quoth Drew Tomlinson on Tuesday, 17 August 2010: I have a collection of yearly top 100 Billboard mp3s in this format (all one line - sorry if it wraps): /archive/Multimedia/Audio/Music/Billboard Top USA Singles/1980-028 Kenny Loggins - This Is It.mp3 I want to create symbolic links to the top 30 in 1966-1969 in another directory for easy migration to a flash card. Thus I invoked 'find' to get a list (again, all one line): find -E /archive/Multimedia/Audio/Music/Billboard Top USA Singles -regex '.*19[6-9][0-9]-0[0-2][0-9].*' (OK, I know this will only return the top 29) 'find' returns the complete filename as above: /archive/Multimedia/Audio/Music/Billboard Top USA Singles/1980-028 Kenny Loggins - This Is It.mp3 Then I attempt to use 'basename' to extract the file name to a variable which I can later pass to 'ln'. This seems to work: basename /archive/Multimedia/Audio/Music/Billboard Top USA Singles/1980-028 Kenny Loggins - This Is It.mp3 returns (all one line): 1980-028 Kenny Loggins - This Is It.mp3 which is what I would expect. However using it with 'find' give me this type of unexpected result: for i in `find -E /archive/Multimedia/Audio/Music/Billboard Top USA Singles -regex '.*19[6-9][0-9]-0[1-2][0-9].*'`; do basename ${i};done 1980-028 Kenny Loggins - This Is It.mp3 Why is this different? And more importantly, how can I capture the file name to $i? Try: find -E ... | while read i; do; basename $i; done When using back-ticks, all the output gets appended together, space-separated. Then 'for' can't tell the difference between a space in a filename and a delimiter. Using 'read' instead preserves line boundaries. Thanks, Drew -- Like card tricks? Visit The Alchemist's Warehouse to learn card magic secrets for free! http://alchemistswarehouse.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 -- Sterling (Chip) Camden| sterl...@camdensoftware.com | 2048D/3A978E4F http://camdensoftware.com | http://chipstips.com| http://chipsquips.com pgpCHrUZ30LlM.pgp Description: PGP signature
Re: Bash Script Help - File Names With Spaces -- SOLVED
On 8/17/2010 7:47 AM, Drew Tomlinson wrote: I have a collection of yearly top 100 Billboard mp3s in this format (all one line - sorry if it wraps): /archive/Multimedia/Audio/Music/Billboard Top USA Singles/1980-028 Kenny Loggins - This Is It.mp3 I want to create symbolic links to the top 30 in 1966-1969 in another directory for easy migration to a flash card. Thus I invoked 'find' to get a list (again, all one line): find -E /archive/Multimedia/Audio/Music/Billboard Top USA Singles -regex '.*19[6-9][0-9]-0[0-2][0-9].*' (OK, I know this will only return the top 29) 'find' returns the complete filename as above: /archive/Multimedia/Audio/Music/Billboard Top USA Singles/1980-028 Kenny Loggins - This Is It.mp3 Then I attempt to use 'basename' to extract the file name to a variable which I can later pass to 'ln'. This seems to work: basename /archive/Multimedia/Audio/Music/Billboard Top USA Singles/1980-028 Kenny Loggins - This Is It.mp3 returns (all one line): 1980-028 Kenny Loggins - This Is It.mp3 which is what I would expect. However using it with 'find' give me this type of unexpected result: for i in `find -E /archive/Multimedia/Audio/Music/Billboard Top USA Singles -regex '.*19[6-9][0-9]-0[1-2][0-9].*'`; do basename ${i};done 1980-028 Kenny Loggins - This Is It.mp3 Why is this different? And more importantly, how can I capture the file name to $i? It finally occurred to me that I needed the shell to see a new line as the delimiter and not whitespace. Then a simple search revealed my answer: O=$IFS IFS=$(echo -en \n\b) do stuff IFS=$O Sorry for the noise. Drew -- Like card tricks? Visit The Alchemist's Warehouse to learn card magic secrets for free! http://alchemistswarehouse.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: Bash Script Help - File Names With Spaces
On 8/17/2010 8:22 AM, Chip Camden wrote: Quoth Drew Tomlinson on Tuesday, 17 August 2010: I have a collection of yearly top 100 Billboard mp3s in this format (all one line - sorry if it wraps): /archive/Multimedia/Audio/Music/Billboard Top USA Singles/1980-028 Kenny Loggins - This Is It.mp3 I want to create symbolic links to the top 30 in 1966-1969 in another directory for easy migration to a flash card. Thus I invoked 'find' to get a list (again, all one line): find -E /archive/Multimedia/Audio/Music/Billboard Top USA Singles -regex '.*19[6-9][0-9]-0[0-2][0-9].*' (OK, I know this will only return the top 29) 'find' returns the complete filename as above: /archive/Multimedia/Audio/Music/Billboard Top USA Singles/1980-028 Kenny Loggins - This Is It.mp3 Then I attempt to use 'basename' to extract the file name to a variable which I can later pass to 'ln'. This seems to work: basename /archive/Multimedia/Audio/Music/Billboard Top USA Singles/1980-028 Kenny Loggins - This Is It.mp3 returns (all one line): 1980-028 Kenny Loggins - This Is It.mp3 which is what I would expect. However using it with 'find' give me this type of unexpected result: for i in `find -E /archive/Multimedia/Audio/Music/Billboard Top USA Singles -regex '.*19[6-9][0-9]-0[1-2][0-9].*'`; do basename ${i};done 1980-028 Kenny Loggins - This Is It.mp3 Why is this different? And more importantly, how can I capture the file name to $i? Try: find -E ... | while read i; do; basename $i; done When using back-ticks, all the output gets appended together, space-separated. Then 'for' can't tell the difference between a space in a filename and a delimiter. Using 'read' instead preserves line boundaries. Thanks for your reply. I like this better than manipulating $IFS because then I don't have to set it back. Cheers, Drew -- Like card tricks? Visit The Alchemist's Warehouse to learn card magic secrets for free! http://alchemistswarehouse.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: Bash Script Help - File Names With Spaces
Am Dienstag, den 17.08.2010, 08:22 -0700 schrieb Chip Camden: find -E ... | while read i; do; basename $i; done The semicolon behind do isn't necessary. -- Timm ___ 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: Bash Script Help - File Names With Spaces
On Tue, 17 Aug 2010 07:47:25 -0700, Drew Tomlinson d...@mykitchentable.net said: D Then I attempt to use 'basename' to extract the file name to a variable D which I can later pass to 'ln'. This seems to work: D basename /archive/Multimedia/Audio/Music/Billboard Top USA D Singles/1980-028 Kenny Loggins - This Is It.mp3 This is a subset of a larger problem: getting the last field from a set of delimited records which may not all have the same number of fields. I've used this when I needed basenames for ~500,000 files: find . regex-or-print-or-whatever | rev | cut -f1 -d/ | rev For dirnames: find . regex-or-print-or-whatever | rev | cut -f2- -d/ | rev | sort -u -- Karl Vogel I don't speak for the USAF or my company When I'm feeling down, I like to whistle. It makes the neighbor's dog run to the end of his chain and gag himself.--unknown ___ 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: Bash Script Help - File Names With Spaces -- SOLVED
Drew Tomlinson d...@mykitchentable.net writes: It finally occurred to me that I needed the shell to see a new line as the delimiter and not whitespace. Then a simple search revealed my answer: O=$IFS IFS=$(echo -en \n\b) do stuff IFS=$O Old IFS value can be preserved by using `local' keyword or (...) braces, too. It's a bit better than polluting global scope with temporary variable. $ echo -n $IFS | (vis -w; echo) \040\^I\^J $ for i in $(find . -type f); do echo $i; done ./My Long File Name ./Another File $ f() { local IFS=; eval $@; } $ f 'for i in $(find . -type f); do echo $i; done' ./My Long File Name ./Another File $ (IFS=; for i in $(find . -type f); do echo $i; done) ./My Long File Name ./Another File $ echo -n $IFS | (vis -w; echo) \040\^I\^J ___ 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: bash script question
I just find out: #!/usr/local/bin/bash export IFS= cuc=$* mkdir cuc Thanks anyway! László From: Dánielisz László laszlo_daniel...@yahoo.com To: freebsd-questions@freebsd.org Sent: Tue, December 1, 2009 8:37:04 PM Subject: bash script question Hello, I'd like to ask how can I read a variable in the same line when I launch a script? For example ./script.sh directory_name, and I want the script to creat the directory called directory_name or whatever I input there. Thank you! László ___ 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
bash script question
Hello, I'd like to ask how can I read a variable in the same line when I launch a script? For example ./script.sh directory_name, and I want the script to creat the directory called directory_name or whatever I input there. Thank you! László ___ 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: bash script question
On Tue, 1 Dec 2009 11:48:43 -0800 (PST), Dánielisz László laszlo_daniel...@yahoo.com wrote: I just find out: #!/usr/local/bin/bash export IFS= cuc=$* mkdir cuc The $* variable will expand to all arguments given on the command line, e. g. $ ./myscript foo bar baz will result in mkdir foo bar baz and so create a directory named foo\ bar\ baz including the spaces. If you only want to access the first parameter, use $1, and for good form, check it before further processing. Your use of quotes to include the parameter is already good form. :-) -- 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: bash script question
Dánielisz László wrote: I just find out: #!/usr/local/bin/bash export IFS= cuc=$* mkdir cuc Thanks anyway! László From: Dánielisz László laszlo_daniel...@yahoo.com To: freebsd-questions@freebsd.org Sent: Tue, December 1, 2009 8:37:04 PM Subject: bash script question Hello, I'd like to ask how can I read a variable in the same line when I launch a script? For example ./script.sh directory_name, and I want the script to creat the directory called directory_name or whatever I input there. Thank you! László ___ 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 Why are you using bash? To make a shell script as portable as possible, use /bin/sh. Bash is a third party shell, that isn't included in a base installation (you're not using bash as root's shell, are you?). By using /bin/sh, you make sure the script will run without having to install any ports. Try this instead (check the Special parameters section in the sh(1) man page to get the difference between $* and $@ and an explanation as to why I quote the $@). #!/bin/sh mkdir $@ Cheers, Rolf Nielsen ___ 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: bash script question
On Tue, 01 Dec 2009 21:06:34 +0100, Rolf G Nielsen laz...@lazlarlyricon.com wrote: Why are you using bash? To make a shell script as portable as possible, use /bin/sh. Bash is a third party shell, that isn't included in a base installation (you're not using bash as root's shell, are you?). By using /bin/sh, you make sure the script will run without having to install any ports. That's a very good advice. Using sh is strongly recommended for maximal portability. Use sh if you're not requiring features that are bash-only. -- 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: bash script question
On Tue, Dec 01, 2009 at 10:42:10PM +0100, Polytropon wrote: On Tue, 01 Dec 2009 21:06:34 +0100, Rolf G Nielsen laz...@lazlarlyricon.com wrote: Why are you using bash? To make a shell script as portable as possible, use /bin/sh. Bash is a third party shell, that isn't included in a base installation (you're not using bash as root's shell, are you?). By using /bin/sh, you make sure the script will run without having to install any ports. That's a very good advice. Using sh is strongly recommended for maximal portability. Use sh if you're not requiring features that are bash-only. Hi guys, Here's a bash-related question, kind-of. Is there any way to automagically run my .csrhc thru a script and wind up with a bash script? gary -- 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 -- Gary Kline kl...@thought.org http://www.thought.org Public Service Unix http://jottings.thought.org http://transfinite.thought.org The 7.31a release of Jottings: http://jottings.thought.org/index.php ___ 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: bash script question
Gary Kline wrote: On Tue, Dec 01, 2009 at 10:42:10PM +0100, Polytropon wrote: On Tue, 01 Dec 2009 21:06:34 +0100, Rolf G Nielsen laz...@lazlarlyricon.com wrote: Why are you using bash? To make a shell script as portable as possible, use /bin/sh. Bash is a third party shell, that isn't included in a base installation (you're not using bash as root's shell, are you?). By using /bin/sh, you make sure the script will run without having to install any ports. That's a very good advice. Using sh is strongly recommended for maximal portability. Use sh if you're not requiring features that are bash-only. Hi guys, Here's a bash-related question, kind-of. Is there any way to automagically run my .csrhc thru a script and wind up with a bash script? gary -- 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 If by Is there any way you mean is it possible, the answer would have to be yes. The next question is most likely has anyone written such a script? and to that question, someone else will have to provide the answer. Rolf Nielsen ___ 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: bash script question
On Tue, 1 Dec 2009 13:45:55 -0800, Gary Kline kl...@thought.org wrote: Hi guys, Here's a bash-related question, kind-of. Is there any way to automagically run my .csrhc thru a script and wind up with a bash script? csh and (ba)sh use dufferent syntax and variable names. But you could write an easy search and replace translator for the .cshrc settings, which are mostly alias foo = 'bar' set var = value setenv envvar = value but for some of them, there's no bash equivalent (e. g. set promptchars and set promt in cshrc, but PS1 in bash). I'm not aware of an already existing mechanism that does this. Running one shell from the other doesn't transport most of the settings. -- 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: bash script question
On Tue, Dec 01, 2009 at 11:02:07PM +0100, Rolf G Nielsen wrote: Gary Kline wrote: On Tue, Dec 01, 2009 at 10:42:10PM +0100, Polytropon wrote: On Tue, 01 Dec 2009 21:06:34 +0100, Rolf G Nielsen laz...@lazlarlyricon.com wrote: Why are you using bash? To make a shell script as portable as possible, use /bin/sh. Bash is a third party shell, that isn't included in a base installation (you're not using bash as root's shell, are you?). By using /bin/sh, you make sure the script will run without having to install any ports. That's a very good advice. Using sh is strongly recommended for maximal portability. Use sh if you're not requiring features that are bash-only. Hi guys, Here's a bash-related question, kind-of. Is there any way to automagically run my .csrhc thru a script and wind up with a bash script? gary -- 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 If by Is there any way you mean is it possible, the answer would have to be yes. The next question is most likely has anyone written such a script? and to that question, someone else will have to provide the answer. Rolf Nielsen (sheepishly, and hanging my head) yes. does anybody have a csh/cshrc-alias to a bash/bashrc-alias script? i've got hundreds of aliases to be just deleted, i suppose, by dozens more. [ ] { please, sur, might i have more to eat? i'm hungry... { beg, contrition... .} } -- Gary Kline kl...@thought.org http://www.thought.org Public Service Unix http://jottings.thought.org http://transfinite.thought.org The 7.31a release of Jottings: http://jottings.thought.org/index.php ___ 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: bash script question
On Tue, Dec 01, 2009 at 11:10:33PM +0100, Polytropon wrote: On Tue, 1 Dec 2009 13:45:55 -0800, Gary Kline kl...@thought.org wrote: Hi guys, Here's a bash-related question, kind-of. Is there any way to automagically run my .csrhc thru a script and wind up with a bash script? csh and (ba)sh use dufferent syntax and variable names. But you could write an easy search and replace translator for the .cshrc settings, which are mostly alias foo = 'bar' set var = value setenv envvar = value but for some of them, there's no bash equivalent (e. g. set promptchars and set promt in cshrc, but PS1 in bash). I'm not aware of an already existing mechanism that does this. Running one shell from the other doesn't transport most of the settings. Ah, Polyt to the rescue. I already have things like setenv aliased to ksh/zsh/borne-again/ and probably even /bin/sh || /bin/ash. I thought that especially bash was still persnikity. It used to be centuries ago, so I just stuck with zsh. Another deal was that I rarely use root, so it didn't worth it. But now, sweating the End of Days, yup. I'll see if vim can come to the resuce. thankee. gary -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ... -- Gary Kline kl...@thought.org http://www.thought.org Public Service Unix http://jottings.thought.org http://transfinite.thought.org The 7.31a release of Jottings: http://jottings.thought.org/index.php ___ 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: SUID permission on Bash script
per...@pluto.rain.com wrote: RW rwmailli...@googlemail.com wrote: On Sat, 29 Aug 2009 00:06:29 -0700 per...@pluto.rain.com wrote: Actually, absent some careful cooperation between the kernel and the interpreter to prevent a race condition ... isn't that the same issue that Matthew Seaman was saying was fixed years ago ... and is described in the follow-up: http://www.mail-archive.com/freebsd-questions@freebsd.org/msg185145.html That's entirely in the kernel, it doesn't require interpreter support. Er, I'm pretty sure it _does_ require support in the interpreter. It would do no good for the kernel to hand the interpreter an open descriptor if the interpreter did not somehow know to read the script from that open descriptor instead of opening the script file by name. This approach is exactly the careful cooperation between the kernel and the interpreter that I was referring to. Errr -- no. That's what fdescfs(5) is for. When the kernel execs the interpreter, it tells the script to open /dev/fd/5 (for example) and doing that just connects the script to the open file descriptor the kernel used previously to taste the magic number and the #! line of the script. As fdescfs(5) says: [...] the call: fd = open(/dev/fd/0, mode); and the call: fd = fcntl(0, F_DUPFD, 0); are equivalent. Cheers, Matthew -- Dr Matthew J Seaman MA, D.Phil. 7 Priory Courtyard Flat 3 PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate Kent, CT11 9PW signature.asc Description: OpenPGP digital signature
Re: SUID permission on Bash script
Matthew Seaman m.sea...@infracaninophile.co.uk wrote: It would do no good for the kernel to hand the interpreter an open descriptor if the interpreter did not somehow know to read the script from that open descriptor instead of opening the script file by name. Errr -- no. That's what fdescfs(5) is for. When the kernel execs the interpreter, it tells the script to open /dev/fd/5 (for example) and doing that just connects the script to the open file descriptor the kernel used previously to taste the magic number and the #! line of the script. which -- again absent some special arrangement in the interpreter -- would cause the script to receive $0 as /dev/fd/5 instead of the actual name of the script, no? I'd expect this to at least break any messages that the script might try to produce via constructs like echo $0: whatever ___ 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: SUID permission on Bash script
Michael David Crawford m...@prgmr.com wrote: It's not that setuid shell scripts are really more inherently insecure than programs written in C. Actually, absent some careful cooperation between the kernel and the interpreter to prevent a race condition that can cause the interpreter to run (with elevated permissions) a completely different script than the one that was marked setuid, setuid scripts _are_ insecure in a way that _cannot_ be fixed by any degree of care that might be taken in the writing of the script. Check the hackers@ archives. It was discussed a little over a month ago. ___ 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: SUID permission on Bash script
per...@pluto.rain.com wrote: Actually, absent some careful cooperation between the kernel and the interpreter to prevent a race condition that can cause the interpreter to run (with elevated permissions) a completely different script than the one that was marked setuid, setuid scripts _are_ insecure in a way that _cannot_ be fixed by any degree of care that might be taken in the writing of the script. Wow. I had no idea. A while back a coworker asked me to help figure out why he couldn't get his script to run setuid on Linux. Some investigation turned up that the Linux kernel explicitly forbids setuid programs whose first two bytes are # and !. So it disables even setuid scripts that don't use the shell, like Python or Perl scripts. I came across a page that explained all the different ways setuid scripts could screw up - one would have to be a rocket scientist to avoid all the potential pitfalls. Mike -- Michael David Crawford m...@prgmr.com prgmr.com - We Don't Assume You Are Stupid. Xen-Powered Virtual Private Servers: http://prgmr.com/xen ___ 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: SUID permission on Bash script
On Sat, 29 Aug 2009 00:17:24 -0700, Michael David Crawford m...@prgmr.com wrote: I came across a page that explained all the different ways setuid scripts could screw up - one would have to be a rocket scientist to avoid all the potential pitfalls. Hi Michael, It would be a very useful addition to the list archives if you pointed at the URI of the page. This way future readers will find it too :) ___ 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: SUID permission on Bash script
On Sat, 29 Aug 2009 00:06:29 -0700 per...@pluto.rain.com wrote: Michael David Crawford m...@prgmr.com wrote: It's not that setuid shell scripts are really more inherently insecure than programs written in C. Actually, absent some careful cooperation between the kernel and the interpreter to prevent a race condition that can cause the interpreter to run (with elevated permissions) a completely different script than the one that was marked setuid, setuid scripts _are_ insecure in a way that _cannot_ be fixed by any degree of care that might be taken in the writing of the script. Check the hackers@ archives. It was discussed a little over a month ago. But is isn't that the same issue that Matthew Seaman was saying was fixed years ago (in the link I gave before), and is described in the follow-up: http://www.mail-archive.com/freebsd-questions@freebsd.org/msg185145.html That's entirely in the kernel, it doesn't require interpreter support. ___ 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: SUID permission on Bash script
On Fri, Aug 28, 2009 at 08:10:59PM -0600, Tim Judd wrote: On 8/28/09, RW rwmailli...@googlemail.com wrote: On Fri, 28 Aug 2009 11:54:19 +0300 Giorgos Keramidas keram...@ceid.upatras.gr wrote: On Fri, 28 Aug 2009 09:24:35 +0100, Jeronimo Calvo jeronimocal...@googlemail.com wrote: As far as i know, using SUID, script must runs with root permissions... so i shoudnt get Permission denied, what im doing wrong?? No it must not. There are security reasons why shell scripts are not setuid-capable. You can find some of them in the archives of the mailing list, going back at least until 1997. I'm bit puzzled by this, previous threads have given the impression that this is a myth, for example: http://www.mail-archive.com/freebsd-questions@freebsd.org/msg185134.html So are scripts actually incapable of running setuid? Dunno, but this dawns on me.. what defines a script? I've always defined a script that starts with a #! shebang. So the script can be SUID, but the interpreter/shell isn't. Is that why it doesn't work? It doesn't work because the system does not allow it - for security reasons. You could fish around and defeat that but don't. The most common way to get around it is create a tiny binary that can run Setuid which merely invokes your script. The better way is to use Sudo as has been suggested already in this thread. jerry --Tim ___ 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: SUID permission on Bash script
RW wrote: On Sat, 29 Aug 2009 00:06:29 -0700 per...@pluto.rain.com wrote: Michael David Crawford m...@prgmr.com wrote: It's not that setuid shell scripts are really more inherently insecure than programs written in C. Actually, absent some careful cooperation between the kernel and the interpreter to prevent a race condition that can cause the interpreter to run (with elevated permissions) a completely different script than the one that was marked setuid, setuid scripts _are_ insecure in a way that _cannot_ be fixed by any degree of care that might be taken in the writing of the script. Check the hackers@ archives. It was discussed a little over a month ago. But is isn't that the same issue that Matthew Seaman was saying was fixed years ago (in the link I gave before), and is described in the follow-up: http://www.mail-archive.com/freebsd-questions@freebsd.org/msg185145.html That's entirely in the kernel, it doesn't require interpreter support. The race condition between the kernel opening the script and the interpreter doing so should certainly be fixed in any Unix or Linux distribution available today. Either, as above, by the kernel passing an open file descriptor to the invoked script, or simply by ignoring any setuid or setgid bits on interpreted scripts. There are other attacks against SUID scripts -- see for instance: http://www.tech-faq.com/suid-root-script-binary.shtml http://www.faqs.org/faqs/unix-faq/faq/part4/section-7.html most of which work by exploiting the sort of features of the scripting language that make it into a powerful and useful tool. Almost all of these sort of exploits can be avoided by careful programming -- for instance, always explicitly setting $IFS and $PATH to known good values, or using the one set of command line flags allowed on the #! line to block the '-i' trick (ie. use '#!/bin/sh --' which forces any subsequent items on the command line to be treated as files rather than command options). However, you (the programmer) would have to know all about the various tricks for exploiting suid-ness in order to counter them. The preferred way of running a script SUID is to write a very small C wrapper program that can be made SUID and that executes the script after gaining increased privileges. Done well, this is definitely the best and most secure approach. Note however that the C wrapper must be similarly as carefully written as a suid script or many of the same exploits could still be possible. So, unless you are an expert programmer and understand how to defend your code against attack, your best bet really is to just use sudo(8). Cheers, Matthew -- Dr Matthew J Seaman MA, D.Phil. 7 Priory Courtyard Flat 3 PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate Kent, CT11 9PW signature.asc Description: OpenPGP digital signature
Re: SUID permission on Bash script
Perhaps a better idea than a setuid shell script, would be to figure out just what it is about your script that really needs to be executed as root. When write a C program that can do just that one thing - and absolutely nothing else. If it takes any kind of input, or command line parameters, then it must validate them very carefully, to ensure that it's not being misused. Then your script could call that C program whenever it needs that privileged operation performed. Suppose you were to give the keys to your Lamborghini to a parking attendant. Wouldn't you want to trust that he wasn't going to sell your Lamborghini to a chop shop? Writing a setuid program is just like that: writing one poorly is like handing your race car keys to a car thief. He might not steal your car today, but if you're not careful about how you hand out your trust, he will someday. Mike -- Michael David Crawford m...@prgmr.com prgmr.com - We Don't Assume You Are Stupid. Xen-Powered Virtual Private Servers: http://prgmr.com/xen ___ 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: SUID permission on Bash script
RW rwmailli...@googlemail.com wrote: On Sat, 29 Aug 2009 00:06:29 -0700 per...@pluto.rain.com wrote: Actually, absent some careful cooperation between the kernel and the interpreter to prevent a race condition ... isn't that the same issue that Matthew Seaman was saying was fixed years ago ... and is described in the follow-up: http://www.mail-archive.com/freebsd-questions@freebsd.org/msg185145.html That's entirely in the kernel, it doesn't require interpreter support. Er, I'm pretty sure it _does_ require support in the interpreter. It would do no good for the kernel to hand the interpreter an open descriptor if the interpreter did not somehow know to read the script from that open descriptor instead of opening the script file by name. This approach is exactly the careful cooperation between the kernel and the interpreter that I was referring to. ___ 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
SUID permission on Bash script
Hi folks! Im trying to set up a reaaallly basic scrip to allow one user to shutdown my machine without root permisions, seting up SUID as follows: -rwsrwxr-- 1 root wheel 38 Aug 27 23:12 apagar.sh $ ./apagar.sh Permission denied content of script: cat apagar.sh ]#!/usr/local/bin/bash shutdown -p now As far as i know, using SUID, script must runs with root permissions... so i shoudnt get Permission denied, what im doing wrong?? ___ 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: SUID permission on Bash script
On Fri, 28 Aug 2009 09:24:35 +0100, Jeronimo Calvo jeronimocal...@googlemail.com wrote: Hi folks! Im trying to set up a reaaallly basic scrip to allow one user to shutdown my machine without root permisions, seting up SUID as follows: -rwsrwxr-- 1 root wheel 38 Aug 27 23:12 apagar.sh $ ./apagar.sh Permission denied content of script: cat apagar.sh ]#!/usr/local/bin/bash shutdown -p now As far as i know, using SUID, script must runs with root permissions... so i shoudnt get Permission denied, what im doing wrong?? No it must not. There are security reasons why shell scripts are not setuid-capable. You can find some of them in the archives of the mailing list, going back at least until 1997. The good thing is that you don't need a shell script to do that. You can install `sudo' and give permission to the specific user to run: sudo shutdown -p now ___ 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: SUID permission on Bash script
Aham! so SUID can be applied to sh but it doesn't work!, there is not anyway to apply it? apart from installing sudo?, The thing is that installing sudo and adding that user into sudoers, that user will be capable to do any other SU tasks, apart of shutting down... wich i dont like :D (I know that SUID could be even worst if they edit the .sh file... but lets believe they dont even know that XD) Cheers! 2009/8/28 Giorgos Keramidas keram...@ceid.upatras.gr On Fri, 28 Aug 2009 09:24:35 +0100, Jeronimo Calvo jeronimocal...@googlemail.com wrote: Hi folks! Im trying to set up a reaaallly basic scrip to allow one user to shutdown my machine without root permisions, seting up SUID as follows: -rwsrwxr-- 1 root wheel 38 Aug 27 23:12 apagar.sh $ ./apagar.sh Permission denied content of script: cat apagar.sh ]#!/usr/local/bin/bash shutdown -p now As far as i know, using SUID, script must runs with root permissions... so i shoudnt get Permission denied, what im doing wrong?? No it must not. There are security reasons why shell scripts are not setuid-capable. You can find some of them in the archives of the mailing list, going back at least until 1997. The good thing is that you don't need a shell script to do that. You can install `sudo' and give permission to the specific user to run: sudo shutdown -p now ___ 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: SUID permission on Bash script
On Friday 28 August 2009 10:54:19 Giorgos Keramidas wrote: On Fri, 28 Aug 2009 09:24:35 +0100, Jeronimo Calvo jeronimocal...@googlemail.com wrote: Hi folks! Im trying to set up a reaaallly basic scrip to allow one user to shutdown my machine without root permisions, seting up SUID as follows: [snip] The good thing is that you don't need a shell script to do that. You can install `sudo' and give permission to the specific user to run: sudo shutdown -p now Or (assuming it doesn't grant too many other privileges) just put the user in group operator. ___ 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: SUID permission on Bash script
On Fri, Aug 28, 2009 at 10:01:54AM +0100, Jeronimo Calvo wrote: 2009/8/28 Giorgos Keramidas keram...@ceid.upatras.gr On Fri, 28 Aug 2009 09:24:35 +0100, Jeronimo Calvo jeronimocal...@googlemail.com wrote: Im trying to set up a reaaallly basic scrip to allow one user to shutdown my machine without root permisions, seting up SUID as follows: -rwsrwxr-- 1 root wheel 38 Aug 27 23:12 apagar.sh $ ./apagar.sh Permission denied content of script: cat apagar.sh ]#!/usr/local/bin/bash shutdown -p now As far as i know, using SUID, script must runs with root permissions... so i shoudnt get Permission denied, what im doing wrong?? No it must not. There are security reasons why shell scripts are not setuid-capable. You can find some of them in the archives of the mailing list, going back at least until 1997. The good thing is that you don't need a shell script to do that. You can install `sudo' and give permission to the specific user to run: sudo shutdown -p now so SUID can be applied to sh but it doesn't work!, there is not anyway to apply it? apart from installing sudo?, The thing is that installing sudo and adding that user into sudoers, that user will be capable to do any other SU tasks, apart of shutting down... wich i dont like :D (I know that SUID could be even worst if they edit the .sh file... but lets believe they dont even know that XD) Please refrain from top-posting. It's both confusing and inconsiderate for anyone trying to read what you write or otherwise trying follow a discussion. First, as has already been pointed out, your approach is A Really Bad Idea and will lead nowhere so forget it. Second, you're misunderstanding sudo. From sudo(8): sudo allows a permitted user to execute a command as the superuser or another user, as specified in the sudoers file. Note the as specified. For example, if the sudoers file contains nothing but john ALL= NOPASSWD: /usr/sbin/shutdown then John (and only John) can use sudo to execute /usr/sbin/shutdown, but can't use sudo to execute any other commands. As an alternative to installing sudo, you can add your user to the operator group: pw groupmod operator -m john but be sure to understand the ramifications before doing so. -- 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: SUID permission on Bash script
On Fri, 28 Aug 2009 09:24:35 +0100, Jeronimo Calvo jeronimocal...@googlemail.com wrote: content of script: ]#!/usr/local/bin/bash ^ This ] doesn't belong to the script, does it? Furthermore, why do you employ bash for calling another program? It's standard to use sh (#!/bin/sh) if you don't use bash-specific commands and constructs, and I don't see them here. If you care for portablility, such a script is an absulute no-go. Furthermore, in order to perform shutdown -p now it's more convenient to use the sudo command (from ports) and add a rule (for maximum security) for the specific user who you want to be able to run this command. Finally, it's possible to place the user in question into the group operator, then he can perform the above command without needing (1st) sudo and (2nd) bash. Look at the permissions of the shutdown program: -r-sr-x--- 1 root operator /sbin/shutdown* Members of operator are +x for this binary. -- 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: SUID permission on Bash script
On Fri, 28 Aug 2009 11:54:19 +0300 Giorgos Keramidas keram...@ceid.upatras.gr wrote: On Fri, 28 Aug 2009 09:24:35 +0100, Jeronimo Calvo jeronimocal...@googlemail.com wrote: As far as i know, using SUID, script must runs with root permissions... so i shoudnt get Permission denied, what im doing wrong?? No it must not. There are security reasons why shell scripts are not setuid-capable. You can find some of them in the archives of the mailing list, going back at least until 1997. I'm bit puzzled by this, previous threads have given the impression that this is a myth, for example: http://www.mail-archive.com/freebsd-questions@freebsd.org/msg185134.html So are scripts actually incapable of running setuid? ___ 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: SUID permission on Bash script
On 8/28/09, RW rwmailli...@googlemail.com wrote: On Fri, 28 Aug 2009 11:54:19 +0300 Giorgos Keramidas keram...@ceid.upatras.gr wrote: On Fri, 28 Aug 2009 09:24:35 +0100, Jeronimo Calvo jeronimocal...@googlemail.com wrote: As far as i know, using SUID, script must runs with root permissions... so i shoudnt get Permission denied, what im doing wrong?? No it must not. There are security reasons why shell scripts are not setuid-capable. You can find some of them in the archives of the mailing list, going back at least until 1997. I'm bit puzzled by this, previous threads have given the impression that this is a myth, for example: http://www.mail-archive.com/freebsd-questions@freebsd.org/msg185134.html So are scripts actually incapable of running setuid? Dunno, but this dawns on me.. what defines a script? I've always defined a script that starts with a #! shebang. So the script can be SUID, but the interpreter/shell isn't. Is that why it doesn't work? --Tim ___ 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: SUID permission on Bash script
On Fri, 28 Aug 2009 20:10:59 -0600, Tim Judd taj...@gmail.com wrote: Dunno, but this dawns on me.. what defines a script? I've always defined a script that starts with a #! shebang. So the script can be SUID, but the interpreter/shell isn't. Is that why it doesn't work? What is the difference of the script and the interpreter? The script is read and executed by the shell, the script itself isn't runnable at all. The itnerpreter specified by #! is executed and then starts to process the script. Is the interpreter running at SUID? Are the commands (child processes) that it executes (fork) running at SUID? But let us continue this consideration: If the shell process would be replaced by the command that is called, would it make a difference? Compare #!/bin/sh - this starts /bin/sh shutdown -p now - /bin/sh starts child shutdown to #!/bin/sh - this starts /bin/sh exec shutdown -p now- /bin/sh replaced by shutdown Hmmm... do I see this correctly? -- 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: SUID permission on Bash script
On Sat, 29 Aug 2009 02:24:31 +0100, RW rwmailli...@googlemail.com wrote: On Fri, 28 Aug 2009 11:54:19 +0300 Giorgos Keramidas keram...@ceid.upatras.gr wrote: On Fri, 28 Aug 2009 09:24:35 +0100, Jeronimo Calvo jeronimocal...@googlemail.com wrote: As far as i know, using SUID, script must runs with root permissions... so i shoudnt get Permission denied, what im doing wrong?? No it must not. There are security reasons why shell scripts are not setuid-capable. You can find some of them in the archives of the mailing list, going back at least until 1997. I'm bit puzzled by this, previous threads have given the impression that this is a myth, for example: http://www.mail-archive.com/freebsd-questions@freebsd.org/msg185134.html So are scripts actually incapable of running setuid? If you hack at the kernel it may still be possible to run a script with setuid or setgid permissions. IMO there is still the possibility for many things to go wrong, especially with quick and dirty scripts. For example, what do you think will happen if a setuid script forgets to properly quote filenames in commands like: foo=$1 pidfile=/tmp/$foo.pid echo $$ $pidfile # cleanup my pidfile truncate -s $pidfile and then I run the script with: setuid.sh /etc/master.passwd /tmp/foo If you guessed that the pid value was not saved anywhere, that because of the lack of error checking nobody noticed, and that the final truncate command may have just wiped your `master.passwd' file your guess is probably 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: SUID permission on Bash script
RW wrote: So are scripts actually incapable of running setuid? They aren't on Linux. I learned about that a while back when I investigated setuid scripts for a coworker. It's not that setuid shell scripts are really more inherently insecure than programs written in C. The problem is more that those who write such scripts tend not to observe the proper precautions. For example if you don't set the PATH explicitly, and you don't give absolute pathnames to all the subprograms you run, then a trojan that has the same name as some standard program can get run as root. If a program is going to be setuid at all, you really have to know what you're doing when you write it or else you'll find yourself opening a can of worms. Mike -- Michael David Crawford m...@prgmr.com prgmr.com - We Don't Assume You Are Stupid. Xen-Powered Virtual Private Servers: http://prgmr.com/xen ___ 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
Problem with bash script
I am attempting to write a simple Bash script that will find all the '*.pem' files in a directory structure and move them to another directory. It seems to work until I get to the copy part where it fails. My scripting skills are not that good. Perhaps someone could tell me what I am doing wrong. This is the script: #! /usr/bin/env bash # Save the field separator oIFS=$IFS # Set it to line breaks IFS=$'\n' for i in $(find ./ -name *.pem -print); do # Get the basename BN=$(basename $i) # copy the file to another directory using the base name cp $i /usr/home/tmp/$BN done # Reset the IFS variable IFS=$oIFS exit -- Carmel ___ 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: Problem with bash script
Hi, Am Dienstag, 16. Jun 2009, 09:09:09 -0400 schrieb Carmel NY: [...] It seems to work until I get to the copy part where it fails. My scripting skills are not that good. Perhaps someone could tell me what I am doing wrong. # copy the file to another directory using the base name cp $i /usr/home/tmp/$BN Please provide the error message next time. I guess the filenames contain spaces. Try cp $i /usr/home/tmp/$BN Bertram -- Bertram Scharpf Stuttgart, Deutschland/Germany http://www.bertram-scharpf.de ___ 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: Problem with bash script
Hi Carmel On Wed, Jun 17, 2009 at 8:39 AM, Carmel NY carmel...@hotmail.com wrote: I am attempting to write a simple Bash script that will find all the '*.pem' files in a directory structure and move them to another directory. It seems to work until I get to the copy part where it fails. My scripting skills are not that good. Perhaps someone could tell me what I am doing wrong. This is the script: #! /usr/bin/env bash # Save the field separator oIFS=$IFS # Set it to line breaks IFS=$'\n' for i in $(find ./ -name *.pem -print); do # Get the basename BN=$(basename $i) # copy the file to another directory using the base name cp $i /usr/home/tmp/$BN done # Reset the IFS variable IFS=$oIFS exit -- Carmel ___ 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 Why you don't use exec flag of find ? You could use: $ find ./ -name *.pem -exec cp {} /usr/home/tmp/something \; Regards Moises Castellanos ___ 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: Problem with bash script
$ find ./ -name *.pem -exec cp {} /usr/home/tmp/something \; I'm a novice with shell scripting myself, but what's the difference between that code and some variant thereof using a pipe and xargs? Are they simply two different ways of achieving the same result? Or is there some more important difference I may be overlooking? Thanks, Daniel ___ 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: Problem with bash script
On Tue, 16 Jun 2009 15:56:15 +0200 Bertram Scharpf li...@bertram-scharpf.de wrote: Hi, Am Dienstag, 16. Jun 2009, 09:09:09 -0400 schrieb Carmel NY: [...] It seems to work until I get to the copy part where it fails. My scripting skills are not that good. Perhaps someone could tell me what I am doing wrong. # copy the file to another directory using the base name cp $i /usr/home/tmp/$BN Please provide the error message next time. I guess the filenames contain spaces. Try cp $i /usr/home/tmp/$BN Bertram I am an idiot. The problem was that the directory did not exist. It was suppose to be $HOME/tmp/$BN. However, I rarely use find and was not aware of the '-exec' function. I use that and it worked fine. Thanks to everyone for their assistance. -- Carmel ___ 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: Problem with bash script
--On Tuesday, June 16, 2009 08:09:09 -0500 Carmel NY carmel...@hotmail.com wrote: I am attempting to write a simple Bash script that will find all the '*.pem' files in a directory structure and move them to another directory. It seems to work until I get to the copy part where it fails. My scripting skills are not that good. Perhaps someone could tell me what I am doing wrong. This is the script: # ! /usr/bin/env bash # Save the field separator oIFS=$IFS # Set it to line breaks IFS=$'\n' for i in $(find ./ -name *.pem -print); do # Get the basename BN=$(basename $i) # copy the file to another directory using the base name cp $i /usr/home/tmp/$BN done # Reset the IFS variable IFS=$oIFS exit When I write scripts, I test them on the commandline to see if they're doing what I think they're doing. I don't get the $(find ) construction. You're invoking find from within a for loop to return a value. Use backticks. This works. for i in `find ./ -name *.pem -print` do foo bar done -- Paul Schmehl (pa...@utdallas.edu) Senior Information Security Analyst The University of Texas at Dallas http://www.utdallas.edu/ir/security/
Re: Problem with bash script
On Tue, Jun 16, 2009 at 02:33:37PM +, Paul Schmehl wrote: --On Tuesday, June 16, 2009 08:09:09 -0500 Carmel NY carmel...@hotmail.com wrote: I am attempting to write a simple Bash script that will find all the '*.pem' files in a directory structure and move them to another directory. It seems to work until I get to the copy part where it fails. My scripting skills are not that good. Perhaps someone could tell me what I am doing wrong. This is the script: # ! /usr/bin/env bash # Save the field separator oIFS=$IFS # Set it to line breaks IFS=$'\n' for i in $(find ./ -name *.pem -print); do # Get the basename BN=$(basename $i) # copy the file to another directory using the base name cp $i /usr/home/tmp/$BN done # Reset the IFS variable IFS=$oIFS exit When I write scripts, I test them on the commandline to see if they're doing what I think they're doing. I don't get the $(find ) It does exactly the same as `command -a rgs`, but all characters between the parentheses are taken literally (in the backtick form, certain chars have special meaning, but not in the $() form.) construction. You're invoking find from within a for loop to return a value. Use backticks. This works. for i in `find ./ -name *.pem -print` do foo bar done It also works with the $() form, provided the target directory exists! Dan -- Daniel Bye _ ASCII ribbon campaign ( ) - against HTML, vCards and X - proprietary attachments in e-mail / \ pgp7aBx5Ctfjr.pgp Description: PGP signature
Re: Problem with bash script
Carmel NY wrote: I am attempting to write a simple Bash script that will find all the '*.pem' files in a directory structure and move them to another directory. It seems to work until I get to the copy part where it fails. My scripting skills are not that good. Perhaps someone could tell me what I am doing wrong. This is the script: #! /usr/bin/env bash # Save the field separator oIFS=$IFS # Set it to line breaks IFS=$'\n' for i in $(find ./ -name *.pem -print); do # Get the basename BN=$(basename $i) # copy the file to another directory using the base name cp $i /usr/home/tmp/$BN done # Reset the IFS variable IFS=$oIFS exit That's a one-liner: % find . -depth -name '*.pem' -print0 | cpio -0pdmu /usr/home/tmp Actually, that just /copies/ all of the *.pem files to the other directory tree, so if you want to remove the original files, you'ld also need to do: % find . -name '*.pem' -delete once you're sure everything has copied across OK, and with the proviso that '.' is neither a parent or child of /usr/home/tmp Cheers, Matthew -- Dr Matthew J Seaman MA, D.Phil. 7 Priory Courtyard Flat 3 PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate Kent, CT11 9PW signature.asc Description: OpenPGP digital signature
Re: Problem with bash script
On Tuesday 16 June 2009 06:03:33 Daniel Underwood wrote: $ find ./ -name *.pem -exec cp {} /usr/home/tmp/something \; I'm a novice with shell scripting myself, but what's the difference between that code and some variant thereof using a pipe and xargs? Are they simply two different ways of achieving the same result? Or is there some more important difference I may be overlooking? Moises' way is less efficient as it will copy each file separately, however, if he ends with a + rather then a \;, then using exec is marginally faster as you eliminate 1 pipe from the tool chain. See find(1) for more info. -- Mel ___ 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: Problem with bash script
On Tuesday, June 16, 2009 08:09:09 -0500 Carmel NY carmel...@hotmail.com wrote: C I am attempting to write a simple Bash script that will find all the C '*.pem' files in a directory structure and move them to another C directory. Using find and pax will correctly handle filenames with spaces and other crap. If you use the -depth option in find, you'll also preserve directory modtimes: root# cd /src/directory root# mkdir -p /dest/directory root# find . -depth -print | pax -rwd -pe /dest/directory Use -rwdu if /dest/directory already exists and you don't want to overwrite newer files there with older files from /src/directory. If you want to store the output from pax as an archive file, use the cpio format for the most portability and the least number of limitations on things like pathname size. Another advantage of find vs backquotes is the ability to filter things out of the files being copied: root# find . -depth -print | fgrep -f /files/to/ignore | pax -x cpio -wd | gzip -1c /tmp/src.pax.gz [ archive the pax file, copy it to another system, etc. ] root# cd /dest/directory root# gunzip -c src.pax.gz | pax -r -pe If you have spaces in your filenames and you still want to do some filtering before running xargs, use tr to change newlines to nulls: root# find . -depth -print | /some/filter/here | tr '\012' '\000' | xargs -0 /some/command On Tue, 16 Jun 2009 16:16:26 +0100, Daniel Bye danie...@slightlystrange.org said: D It does exactly the same as `command -a rgs`, but all characters between D the parentheses are taken literally (in the backtick form, certain chars D have special meaning, but not in the $() form.) Either solution will run the command first and then rescan the line in the calling script. You can run into problems when the results of the backquoted command are too big; there's an upper limit to how many arguments you can put in a for loop or pass to a program. Also, any spaces or weird characters in the output are likely to play hell with whatever else you're doing. You're much safer using a pipe. Use backquotes or $() when you're pretty sure the output won't be too big. To be safe, check the argument count when you run something: #!/bin/bash set X `date` # should give 6 tokens plus X. case $# in 1) echo command didn't print anything ;; 7) echo success: $*; echo year should be 7th arg: $7 ;; *) echo something else is wrong ;; esac exit 0 -- Karl Vogel I don't speak for the USAF or my company There is nothing more satisfying than having someone take a shot at you and miss. --Murphy's Laws of Combat ___ 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: Problem with bash script
I botched the filter example in my previous blathering. This will ignore output from find containing patterns: root# find . -depth -print | fgrep -v -f /some/patterns Leaving off the -v will keep output containing those patterns. -- Karl Vogel I don't speak for the USAF or my company Sign in a Tacoma, Washington men's clothing store: 15 men's wool suits, $10. They won't last an hour! ___ 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: bash script on FreeBSD
On Wed, Oct 8, 2008 at 8:11 AM, Kalpin Erlangga Silaen [EMAIL PROTECTED] wrote: I am going to extract field username and UID from /etc/passwd and passed into some scripts. Let say I got line admin 100 admin2 200 admin3 300 admin4 400 and then I want to echoing into screen: admin has uid 100 admin2 has uid 200 admin3 has uid 300 admin4 has uid 400 How do I make this with bash script? # echo $line |awk '{ print $1 has uid $2 }' -- regards, Artis Caune . CCNA | BSDA | ' didii FreeBSD ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: bash script on FreeBSD
On Wed, Oct 08, 2008 at 12:11:40PM +0700, Kalpin Erlangga Silaen wrote: Dear all, I am going to extract field username and UID from /etc/passwd and passed into some scripts. Let say I got line admin 100 admin2 200 admin3 300 admin4 400 and then I want to echoing into screen: admin has uid 100 admin2 has uid 200 admin3 has uid 300 admin4 has uid 400 How do I make this with bash script? Thank you Kalpin Erlangga Silaen $ sed -e 's/\(admin[0-9]*\)\ \([0-9]*\)/\1 has uid \2/g' /etc/passwd Regards, -- Frank Contact info: http://www.shute.org.uk/misc/contact.html ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: bash script on FreeBSD
On Wed, Oct 08, 2008 at 08:11:56AM +0100, Frank Shute wrote: On Wed, Oct 08, 2008 at 12:11:40PM +0700, Kalpin Erlangga Silaen wrote: Dear all, I am going to extract field username and UID from /etc/passwd and passed into some scripts. Let say I got line admin 100 admin2 200 admin3 300 admin4 400 and then I want to echoing into screen: admin has uid 100 admin2 has uid 200 admin3 has uid 300 admin4 has uid 400 How do I make this with bash script? Thank you Kalpin Erlangga Silaen $ sed -e 's/\(admin[0-9]*\)\ \([0-9]*\)/\1 has uid \2/g' /etc/passwd Correction: You can't use that on /etc/passwd directly. But assuming you've got a file already in the format you specified, then you can use it on that. If you want to grab the data directly from /etc/passwd then you'd be better off using awk(1). Regards, -- Frank Contact info: http://www.shute.org.uk/misc/contact.html ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: bash script on FreeBSD
Dear Frank, you are correct. Finally, I write my script using awk. Thank you Kalpin Erlangga Silaen Frank Shute wrote: On Wed, Oct 08, 2008 at 08:11:56AM +0100, Frank Shute wrote: On Wed, Oct 08, 2008 at 12:11:40PM +0700, Kalpin Erlangga Silaen wrote: Dear all, I am going to extract field username and UID from /etc/passwd and passed into some scripts. Let say I got line admin 100 admin2 200 admin3 300 admin4 400 and then I want to echoing into screen: admin has uid 100 admin2 has uid 200 admin3 has uid 300 admin4 has uid 400 How do I make this with bash script? Thank you Kalpin Erlangga Silaen $ sed -e 's/\(admin[0-9]*\)\ \([0-9]*\)/\1 has uid \2/g' /etc/passwd Correction: You can't use that on /etc/passwd directly. But assuming you've got a file already in the format you specified, then you can use it on that. If you want to grab the data directly from /etc/passwd then you'd be better off using awk(1). Regards, ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: bash script on FreeBSD
On Wed, 08 Oct 2008 12:11:40 +0700, Kalpin Erlangga Silaen [EMAIL PROTECTED] wrote: Dear all, I am going to extract field username and UID from /etc/passwd and passed into some scripts. Let say I got line admin 100 admin2 200 admin3 300 admin4 400 and then I want to echoing into screen: admin has uid 100 admin2 has uid 200 admin3 has uid 300 admin4 has uid 400 How do I make this with bash script? You don't really need bash for this. Here's a sample awk script that should work: % cat -n /tmp/userlist.awk 1 #!/usr/bin/awk -f 2 3 { 4 print $1,has uid,$2; 5 } % chmod 0755 /tmp/userlist.awk % cat /tmp/user-data admin 100 admin2 200 admin3 300 admin4 400 % /tmp/userlist.awk /tmp/user-data admin has uid 100 admin2 has uid 200 admin3 has uid 300 admin4 has uid 400 % ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: bash script on FreeBSD
--On Wednesday, October 08, 2008 14:16:02 -0500 Giorgos Keramidas [EMAIL PROTECTED] wrote: On Wed, 08 Oct 2008 12:11:40 +0700, Kalpin Erlangga Silaen [EMAIL PROTECTED] wrote: Dear all, I am going to extract field username and UID from /etc/passwd and passed into some scripts. Let say I got line admin 100 admin2 200 admin3 300 admin4 400 and then I want to echoing into screen: admin has uid 100 admin2 has uid 200 admin3 has uid 300 admin4 has uid 400 How do I make this with bash script? You don't really need bash for this. Here's a sample awk script that should work: % cat -n /tmp/userlist.awk 1 #!/usr/bin/awk -f 2 3 { 4 print $1,has uid,$2; 5 } % chmod 0755 /tmp/userlist.awk % cat /tmp/user-data admin 100 admin2 200 admin3 300 admin4 400 % /tmp/userlist.awk /tmp/user-data admin has uid 100 admin2 has uid 200 admin3 has uid 300 admin4 has uid 400 % Sure, but why use an elephant to swat a flea when awk 'FS=:; {print $1 has uid $3}' /etc/passwd | sort -k 4 -n works fine and returns the accounts in numerical order? -- Paul Schmehl, Senior Infosec Analyst As if it wasn't already obvious, my opinions are my own and not those of my employer. *** Check the headers before clicking on Reply. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
bash script on FreeBSD
Dear all, I am going to extract field username and UID from /etc/passwd and passed into some scripts. Let say I got line admin 100 admin2 200 admin3 300 admin4 400 and then I want to echoing into screen: admin has uid 100 admin2 has uid 200 admin3 has uid 300 admin4 has uid 400 How do I make this with bash script? Thank you Kalpin Erlangga Silaen ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Bash script to find out the summary of user memory usage [not working]
I have correction with the script but still doesn't work: #!/usr/local/bin/bash for user in `ps -A -o user | sort | uniq | tail +2` do echo user: $user ps aux -U $user | tail +2 | while read line do mem=`echo $line | awk {'print $4'}` echo mem: $mem TMPSUMMEM=`awk -v x=$mem -v y=$TMPSUMMEM 'BEGIN{printf %.2f\n,x+y}'` echo summem: $TMPSUMMEM done echo finalsummem: $SUMMEM export SUMMEM=$TMPSUMMEM done echo finalsummem: $SUMMEM #!/usr/local/bin/bash for user in `ps -A -o user | sort | uniq | tail +2` do echo user: $user ps aux -U $user | tail +2 | while read line do mem=`echo $line | awk {'print $4'}` echo mem: $mem TMPSUMMEM=`awk -v x=$mem -v y=$TMPSUMMEM 'BEGIN{printf %.2f\n,x+y}'` echo summem: $TMPSUMMEM done echo finalsummem: $TMPSUMMEM --- Patrick Dung [EMAIL PROTECTED] wrote: Hello, any idea about why below script is not working? The final sum is empty.. #!/usr/local/bin/bash for user in `ps -A -o user | sort | uniq | tail +2` do echo user: $user ps aux -U $user | tail +2 | while read line do mem=`echo $line | awk {'print $4'}` echo mem: $mem TMPSUMMEM=`awk -v x=$mem -v y=$TMPSUMMEM 'BEGIN{printf %.2f\n,x+y}'` echo summem: $TMPSUMMEM done echo finalsummem: $SUMMEM [EMAIL PROTECTED] Looking for last minute shopping deals? Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Bash script to find out the summary of user memory usage [not working]
On 2007-12-17 06:00, Patrick Dung [EMAIL PROTECTED] wrote: I have correction with the script but still doesn't work: #!/usr/local/bin/bash for user in `ps -A -o user | sort | uniq | tail +2` do echo user: $user ps aux -U $user | tail +2 | while read line do mem=`echo $line | awk {'print $4'}` echo mem: $mem TMPSUMMEM=`awk -v x=$mem -v y=$TMPSUMMEM 'BEGIN{printf %.2f\n,x+y}'` echo summem: $TMPSUMMEM done echo finalsummem: $SUMMEM export SUMMEM=$TMPSUMMEM done echo finalsummem: $SUMMEM There are *many* race conditions in that script. For example, there's no guarantee that once you get a snapshot of the ps -A -o user output, then the same users will be listed in the loop you are running for each username. The script is also a bit 'sub-optimal' because it calls ps(1) and parses its output many times (at least as many times as there are users). A much better way to `design' something like this would be to keep a hash of the usernames, and keep incrementing the hash entry for each user as you hit ps(1) output lines. I'm not going to even bother writing a script to use a hash in bash(1), because there are much better languages to work with hashes, dictionaries or even simple arrays. Here's for example a Python script which does what I described: 1 #!/usr/bin/env python 2 3 import os 4 import re 5 import sys 6 7 try: 8 input = os.popen('ps xauwww', 'r') 9 except: 10 print Cannot open pipe for ps(1) output 11 sys.exit(1) 12 13 # Start with an empty dictionary. 14 stats = {} 15 16 # Regexp to strip the ps(1) output header. 17 header = re.compile('USER') 18 19 for line in input.readlines(): 20 if header.match(line): 21 continue 22 fields = line.split() 23 if not fields or len(fields) 4: 24 continue 25 26 (username, mem) = (fields[0], float(fields[3])) 27 value = None 28 try: 29 value = stats[username] 30 except KeyError: 31 pass 32 33 if not value: 34 stats[username] = 0.0 35 stats[username] += mem 36 37 # Print all the stats we have collected so far. 38 keys = stats.keys() 39 if len(keys) 0: 40 total = 0.0 41 print %-15s %5s % ('USERNAME', 'MEM%') 42 for k in stats.keys(): 43 print %-15s %5.2f % (k, stats[k]) 44 total += stats[k] 45 # Finally print a grand total of all users. 46 print %-15s %5.2f % ('TOTAL', total) It's not the shortest Python script one could write to do what you describe, but I've gone for readability rather than speed or conciseness. Running this script should produce: $ ./foo.py USERNAME MEM% _pflogd 0.10 daemon 0.00 bind 1.10 _dhcp0.10 keramida38.60 smmsp0.10 root10.10 build0.00 TOTAL 50.10 $ PS: Yes, you could probably do the same in bash, with sed, awk and a bit of superglue, but I prefer Perl and/or Python for anything which involves something a bit more involved than simple string substitution these days... ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Bash script to find out the summary of user memory usage [not working]
Hello, any idea about why below script is not working? The final sum is empty.. #!/usr/local/bin/bash for user in `ps -A -o user | sort | uniq | tail +2` do echo user: $user ps aux -U $user | tail +2 | while read line do mem=`echo $line | awk {'print $4'}` echo mem: $mem TMPSUMMEM=`awk -v x=$mem -v y=$TMPSUMMEM 'BEGIN{printf %.2f\n,x+y}'` echo summem: $TMPSUMMEM done echo finalsummem: $SUMMEM [EMAIL PROTECTED] Looking for last minute shopping deals? Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
need help w/ simple bash script
Hi all, I am trying to write a simple bash script that will collate pkg_version reports from all of my servers to generate centralized HTML reports. To format the output, I am trying: # REPORT=`pkg_version -v` But when I echo $REPORT, I get: Xaw3d-1.5E_1 = up-to-date with port apr-db42-1.2.7_1 = up-to-date with port autoconf-2.13.000227_5 = up-to-date with port autoconf-2.59_2 = up-to-date with port automake-1.9.6 = up-to-date with port bash-3.1.17 = up-to-date with port cvsup-without-gui-16.1h_2 = up-to-date with port db41-4.1.25_3 = up-to-date with port. When what I want is: Xaw3d-1.5E_1 = up-to-date with port apr-db42-1.2.7_1 = up-to-date with port autoconf-2.13.000227_5 = up-to-date with port autoconf-2.59_2 = up-to-date with port automake-1.9.6 = up-to-date with port bash-3.1.17 = up-to-date with port cvsup-without-gui-16.1h_2 = up-to-date with port db41-4.1.25_3 = up-to-date with port ... ... ... I've also tried: for LINE in `pkg_version -v`; do echo $LINE; done but that's even worse; then I get: Xaw3d-1.5E_1 = up-to-date with port apr-db42-1.2.7_1 = up-to-date with port autoconf-2.13.000227_5 = up-to-date with port autoconf-2.59_2 = up-to-date with port ... ... ... I know I figured out a technique once before, but I'm banging my head against a wall right now. Thanks for any help. -DW ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: need help w/ simple bash script
Hiya. I've been working on a web front-end to aggregate multiple servers' package update requirements as well. I'll probably have it ready to present in another few weeks, if ${DAYJOB} doesn't get in the way. On Tue, Jun 27, 2006 at 08:01:49AM -0400, dw wrote: # REPORT=`pkg_version -v` But when I echo $REPORT, I get: Xaw3d-1.5E_1 = up-to-date with port apr-db42-1.2.7_1 = up-to-date with ... When what I want is: Xaw3d-1.5E_1 = up-to-date with port apr-db42-1.2.7_1 = up-to-date with port ... Use more quotes. REPORT = `pkg_version -v` will protect the newlines. for LINE in `pkg_version -v`; do echo $LINE; done If you feel adventurous, you could to try something like this: tmpfile=/tmp/`basename $0`.$$ trap rm -f $tmpfile $tmpfile.? 0 1 2 3 5 15 pkg_version -v | while read package status text; do echo $package $text $tmpfile.$status done Now you have tempfiles with package lists for the various stati, which you can parse as you see fit. Note that you may get better (i.e. more useful) mileage out of something like: pkg_version -vL= which will show you only what needs to be updated. -- Paul Chvostek [EMAIL PROTECTED] it.canadahttp://www.it.ca/ ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
RE: need help w/ simple bash script
-Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of dw Sent: Tuesday, June 27, 2006 2:02 PM To: freebsd-questions@freebsd.org Subject: need help w/ simple bash script Hi all, I am trying to write a simple bash script that will collate pkg_version reports from all of my servers to generate centralized HTML reports. To format the output, I am trying: # REPORT=`pkg_version -v` But when I echo $REPORT, I get: Xaw3d-1.5E_1 = up-to-date with port apr-db42-1.2.7_1 = up-to-date with port autoconf-2.13.000227_5 = up-to-date with port autoconf-2.59_2 = up-to-date with port automake-1.9.6 = up-to-date with port bash-3.1.17 = up-to-date with port cvsup-without-gui-16.1h_2 = up-to-date with port db41-4.1.25_3 = up-to-date with port. When what I want is: Xaw3d-1.5E_1 = up-to-date with port apr-db42-1.2.7_1 = up-to-date with port autoconf-2.13.000227_5 = up-to-date with port autoconf-2.59_2 = up-to-date with port automake-1.9.6 = up-to-date with port bash-3.1.17 = up-to-date with port cvsup-without-gui-16.1h_2 = up-to-date with port db41-4.1.25_3 = up-to-date with port ... ... ... I've also tried: for LINE in `pkg_version -v`; do echo $LINE; done but that's even worse; then I get: Xaw3d-1.5E_1 = up-to-date with port apr-db42-1.2.7_1 = up-to-date with port autoconf-2.13.000227_5 = up-to-date with port autoconf-2.59_2 = up-to-date with port ... ... ... I know I figured out a technique once before, but I'm banging my head against a wall right now. Thanks for any help. -DW why not just try pkg_version -v |while read h_line do echo $h_line TO SOMEWHERE done; you'd be even able to do something like pkg_version -v |grep -v 'up-to-date with port'|while read h_line do blah done; have fun ;-) ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: bash script
[EMAIL PROTECTED] wrote: My knowledge in bash scripting is about medium not very advanced and all so I am not attempting to make a connection limiter but what I want to make is a script that checks the irc connections off a certain user and takes some actions, mostly of this I know how to do but I got stuck at this : I write in a file we will call it users.allow the following: Virtual 5 Test 7 Server 9 Power 2 This will be the file that will hold the limit of the users from where the script reads when checks. So my script will read from this file and if the user Virtual for example has more than 5 connections he will kill all his processes that are running in background. But what what I don`t know how to do is make the script read, for example if I start the script he starts reading from users.allow, I want it to read an take each line, first taking the line 1 with user Virtual and setting to a variable the number it has near it like $allow=5, next after it finishes the process for Virtual takes Test etc. So my need would be how can I get the script to read first line, do the process I will make for it, then take the next line and do the process for that user until end of file. And also after taking the first line for example reading only the first block in it, the one with user so it can set $user=Virtual and then to take the path with the number. I would greatly apreciate if you can help me with this issue, I really need this script but didn`t know what to look in the manuals for etc. Also if you know a similar script please let me know. Well, you could either use a counter: file=$0 count=`expr 1` lines=`wc -l $file` while [ $count -le $lines ]; do line=`sed -n ${count}p` $file echo $line count=`expr $count + 1` done; for a very slow version of cat ;) or you could simply grep for the line you need line=`grep Power $file` I hope this helps.. ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Help on bash script?
On Sat, 13 Aug 2005, Giorgos Keramidas wrote: On 2005-08-12 13:38, Benson Wong [EMAIL PROTECTED] wrote: I prefer: for COREFILE in `find / -type f -name core -print` do ... done Wouldn't that accomplish the same thing? More or less. Less, when the filenames are too many. See questions posted on this very same list about ``too many arguments''. True. Of course if you've got that many core files lying around, you probably have other things to worry about :) The ``while read line; do stuff with $line; done'' loop doesn't suffer from the same limitation, but is a bit more expensive in terms of the number of spawned processes and (consequently) the time it takes to run. Maybe measurable. Took me ages to appreciate the difference between say cat tempfile | while read line; do stuff with $line; done and while read line; do stuff with $line; done tempfile when 'stuff' needs to update script-scope variables, despite a slightly icky, impure feeling about using tempfiles .. cheers, Ian ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Help on bash script?
On Fri 12 Aug 2005 09:33:54 +0800 Xu Qiang [EMAIL PROTECTED] wrote: find / -type f -name core -print | while read COREFILE ; do NCOREFILES=$[ $NCOREFILES + 1 ] # a bit strange - xq echo $NCOREFILES # xq NEWNAME=${HOSTNAME}esscore${NCOREFILES}_${TIMESTAMP} # record mapping so people can go back and figure out # where they came from echo -e $NEWNAME was `ls -l $COREFILE` $SAVE_DIR/$CORELOG mv $COREFILE $SAVE_DIR/$NEWNAME echo There are $NCOREFILES core files. # xq done fi # What confused me most is the value $NCOREFILES outside # the do-while loop (but still in this function) reverted # back to its initial value, which seems contradictory to # our concept of local variables. - xq #echo $NCOREFILES It's been pointed out that the find piped to while runs in a subshell, and that changes to variables within aren't seen by its parent, but one way around this is to avoid using a pipe, thus a subshell, for instance: find / -type f -name *.core -print tempfile while read COREFILE; do [.. stuff ..] NCOREFILES=$(($NCOREFILES + 1)) done tempfile echo $NCOREFILES I use sh, not bash, but suspect that this should work in bash too. cheers, Ian ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Help on bash script?
I prefer: for COREFILE in `find / -type f -name core -print` do ... done Wouldn't that accomplish the same thing? On 8/12/05, Ian Smith [EMAIL PROTECTED] wrote: On Fri 12 Aug 2005 09:33:54 +0800 Xu Qiang [EMAIL PROTECTED] wrote: find / -type f -name core -print | while read COREFILE ; do NCOREFILES=$[ $NCOREFILES + 1 ] # a bit strange - xq echo $NCOREFILES # xq NEWNAME=${HOSTNAME}esscore${NCOREFILES}_${TIMESTAMP} # record mapping so people can go back and figure out # where they came from echo -e $NEWNAME was `ls -l $COREFILE` $SAVE_DIR/$CORELOG mv $COREFILE $SAVE_DIR/$NEWNAME echo There are $NCOREFILES core files. # xq done fi # What confused me most is the value $NCOREFILES outside # the do-while loop (but still in this function) reverted # back to its initial value, which seems contradictory to # our concept of local variables. - xq #echo $NCOREFILES It's been pointed out that the find piped to while runs in a subshell, and that changes to variables within aren't seen by its parent, but one way around this is to avoid using a pipe, thus a subshell, for instance: find / -type f -name *.core -print tempfile while read COREFILE; do [.. stuff ..] NCOREFILES=$(($NCOREFILES + 1)) done tempfile echo $NCOREFILES I use sh, not bash, but suspect that this should work in bash too. cheers, Ian ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED] -- blog: http://www.mostlygeek.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Help on bash script?
On 2005-08-12 13:38, Benson Wong [EMAIL PROTECTED] wrote: I prefer: for COREFILE in `find / -type f -name core -print` do ... done Wouldn't that accomplish the same thing? More or less. Less, when the filenames are too many. See questions posted on this very same list about ``too many arguments''. The ``while read line; do stuff with $line; done'' loop doesn't suffer from the same limitation, but is a bit more expensive in terms of the number of spawned processes and (consequently) the time it takes to run. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Help on bash script?
Hi, all: I don't know if this is the right list to ask this question. But since I didn't find a bash script mail list and you guys are always so helpful, then... Here are an excerpt of a bash script: --- #!/bin/bash # saveLogs.sh - Bourne Again Shell script ... # # FindAndSaveCoreFiles() # Search the entire filesystem for corefiles, saving each in # $SAVE_DIR. On Linux, we only need to search in ESS_LOG_DIR. # CollectCoreFiles() { local NEWNAME= local NCOREFILES=0 local OS=`uname` if [ $OS == Linux ]; then ... else # # find each corefile, record in $CORELOG, and move to a uniquely # named file in $SAVE_DIR # # Look for files named core (Lynx default) # find / -type f -name core -print | while read COREFILE ; do NCOREFILES=$[ $NCOREFILES + 1 ] # a bit strange - xq echo $NCOREFILES # xq NEWNAME=${HOSTNAME}esscore${NCOREFILES}_${TIMESTAMP} # record mapping so people can go back and figure out # where they came from echo -e $NEWNAME was `ls -l $COREFILE` $SAVE_DIR/$CORELOG mv $COREFILE $SAVE_DIR/$NEWNAME echo There are $NCOREFILES core files. # xq done fi # What confused me most is the value $NCOREFILES outside # the do-while loop (but still in this function) reverted # back to its initial value, which seems contradictory to # our concept of local variables. - xq #echo $NCOREFILES return 0 } --- The purpose of this script is to find the core files in the system and move them to elsewhere for later analysis. I am confused about the following issues: 1. The way of incrementing the variable NCOREFILES. Why does it use the formula of NCOREFILES=$[ $NCOREFILES + 1 ], and not the direct way of NCOREFILES=$NCOREFILES+1? 2. What confused me most is the value of the variable NCOREFILES ($NCOREFILES). Say there is just 1 core file, then because the initial value of NCOREFILES is 0, it will be incremented to 1. Yes, this is the value in the do-while loop. But outside the loop and if-block, the value of NCOREFILES is reverted back to 0 - its initial value. It is a local variable, so any modification to it should be valid as long as we are still in the scope of the function, right? I am really lossed at this phenomenon. Looking forward to any possible help, thanks, Xu Qiang ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Help on bash script?
On Fri, 12 Aug 2005, Xu Qiang wrote: Hi, all: I don't know if this is the right list to ask this question. But since I didn't find a bash script mail list and you guys are always so helpful, then... Here are an excerpt of a bash script: --- #!/bin/bash # saveLogs.sh - Bourne Again Shell script ... find / -type f -name core -print | while read COREFILE ; do NCOREFILES=$[ $NCOREFILES + 1 ] # a bit strange - xq echo $NCOREFILES # xq ... --- ... 1. The way of incrementing the variable NCOREFILES. Why does it use the formula of NCOREFILES=$[ $NCOREFILES + 1 ], and not the direct way of NCOREFILES=$NCOREFILES+1? If that was done, NCOREFILES would end up looking like: +1+1+1+1+1+1+1 as bash does not automatically differentiate between numeric and string variables. What the funky looking construct does is convince bash to treat it as a numeric/arithmetic expression. 2. What confused me most is the value of the variable NCOREFILES ($NCOREFILES). Say there is just 1 core file, then because the initial value of NCOREFILES is 0, it will be incremented to 1. Yes, this is the value in the do-while loop. But outside the loop and if-block, the value of NCOREFILES is reverted back to 0 - its initial value. It is a local variable, so any modification to it should be valid as long as we are still in the scope of the function, right? I am really lossed at this phenomenon. As soon as you used the pipe, to the while, you entered a sub-shell. There's no way (that I'm aware of anyways) to get the sub-shell's variables sent back up to the parent. You could do something along the lines of: for cf in `find -X -type f -name core -print` do # do stuff with $cf NCOREFILES=$[ $NCOREFILES + 1 ] done (the -X helps prevent problems when faced with spaces embedded in the path) By the way, on FreeBSD systems, the default core filename format is %N.core (see man core) where %N is the name of the program that dumped. You'd need to expand your find with -name \*.core . If you wanted to get really fancy, you could parse sysctl kern.corefile to find out the current filename format and use that in your find, but most people leave that sysctl at its default, so it probably wouldn't be necessary. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
RE: Help on bash script?
dpk wrote: On Fri, 12 Aug 2005, Xu Qiang wrote: As soon as you used the pipe, to the while, you entered a sub-shell. There's no way (that I'm aware of anyways) to get the sub-shell's variables sent back up to the parent. Thanks for your detailed analysis and a solution. Yes, I didn't notice the pipe's effect. Btw, can we export a value in the sub-shell back to the parent? thanks, Xu Qiang ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Help on bash script?
On 2005-08-12 10:16, Xu Qiang [EMAIL PROTECTED] wrote: Btw, can we export a value in the sub-shell back to the parent? Only through `backquote subtitution', as the child process cannot affect the environment of the parent process. value=`shell command` value=$(shell command) ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
RE: Help on bash script?
Giorgos Keramidas wrote: On 2005-08-12 10:16, Xu Qiang [EMAIL PROTECTED] wrote: Btw, can we export a value in the sub-shell back to the parent? Only through `backquote subtitution', as the child process cannot affect the environment of the parent process. value=`shell command` value=$(shell command) Ah, yes, I mistakened it. The export mechanism is to preserve the value from the parent to the child, not the other direction, as described in the book The Unix Programming Environment by Brian Kernighan and Rob Pike. Thanks, Xu Qiang ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
RE: Help on bash script?
dpk wrote: On Fri, 12 Aug 2005, Xu Qiang wrote: 1. The way of incrementing the variable NCOREFILES. Why does it use the formula of NCOREFILES=$[ $NCOREFILES + 1 ], and not the direct way of NCOREFILES=$NCOREFILES+1? If that was done, NCOREFILES would end up looking like: +1+1+1+1+1+1+1 as bash does not automatically differentiate between numeric and string variables. What the funky looking construct does is convince bash to treat it as a numeric/arithmetic expression. Strangely, there is no mention of this assignment usage in the book Learning the bash shell, 2nd ed by O'Reilly at my hand. :( And man bash didn't talk of it either. However, it mentioned for arithmetic evaluation, we can use id++ id-- variable post-increment and post-decrement ++id --id variable pre-increment and pre-decrement If it is correct, then I can also increment NCOREFILES by: ++NCOREFILES. But I doubt it... Regards, Xu Qiang ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
RE: Help on bash script?
This is my test script: - #!/bin/bash var=0 var=$[3] vari=0 ++vari echo $var echo $vari - The result is: ./test.sh: ++vari: command not found 3 0 So the manual of bash is incorrect? Regards, Xu Qiang ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
RE: Help on bash script?
On Fri, 12 Aug 2005, Xu Qiang wrote: This is my test script: - #!/bin/bash var=0 var=$[3] vari=0 ++vari echo $var echo $vari - The result is: ./test.sh: ++vari: command not found 3 0 So the manual of bash is incorrect? Regards, Xu Qiang It will work with either 'let' or within an 'arithmetic expansion': $[++var] let ++var By the way, there is another syntax, from the man page, that seems to operate identically: $((++var)) and $((var+1)) ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
RE: Help on bash script?
dpk wrote: It will work with either 'let' or within an 'arithmetic expansion': $[++var] let ++var By the way, there is another syntax, from the man page, that seems to operate identically: $((++var)) and $((var+1)) With let ++var, the result is still 0, it isn't incremented. With $[++var], the result is 0: command not found. $((++var)) is to the same effect as let ++var. $((var+1)) works. But the value of var can't be incremented. thanks, Xu Qiang ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
bash script
My knowledge in bash scripting is about medium not very advanced and all so I am not attempting to make a connection limiter but what I want to make is a script that checks the irc connections off a certain user and takes some actions, mostly of this I know how to do but I got stuck at this : I write in a file we will call it users.allow the following: Virtual 5 Test 7 Server 9 Power 2 This will be the file that will hold the limit of the users from where the script reads when checks. So my script will read from this file and if the user Virtual for example has more than 5 connections he will kill all his processes that are running in background. But what what I don`t know how to do is make the script read, for example if I start the script he starts reading from users.allow, I want it to read an take each line, first taking the line 1 with user Virtual and setting to a variable the number it has near it like $allow=5, next after it finishes the process for Virtual takes Test etc. So my need would be how can I get the script to read first line, do the process I will make for it, then take the next line and do the process for that user until end of file. And also after taking the first line for example reading only the first block in it, the one with user so it can set $user=Virtual and then to take the path with the number. I would greatly apreciate if you can help me with this issue, I really need this script but didn`t know what to look in the manuals for etc. Also if you know a similar script please let me know. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Simple bash script to grep files for bad keywords
Hello, I am trying to write a simple bash script that will grep all files in a directory (except ones that start with 00) for certain bad keywords. Here is what I have so far: #!/bin/bash # This is a simple script to check all sql scripts for bad keywords BAD_KEYWORDS='spool echo timing commit rollback' for i in $BAD_KEYWORDS; do echo *; echo GREPing for bad keyword '$i' echo *; grep $i ./*; done However, I'm not sure how to make it not grep the files that start with 00. Can anyone help me with this? Thanks /Brian ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Simple bash script to grep files for bad keywords
On 2005-03-23 12:29, Brian John [EMAIL PROTECTED] wrote: Hello, I am trying to write a simple bash script that will grep all files in a directory (except ones that start with 00) for certain bad keywords. Here is what I have so far: #!/bin/bash # This is a simple script to check all sql scripts for bad keywords BAD_KEYWORDS='spool echo timing commit rollback' for i in $BAD_KEYWORDS; do echo *; echo GREPing for bad keyword '$i' echo *; grep $i ./*; done However, I'm not sure how to make it not grep the files that start with 00. Can anyone help me with this? Use xargs, since it will buy you the extra feature of being able to search through arbitrarily large numbers of files: for _word in ${BAD_KEYWORDS} ;do find . | grep -v '^/00' |\ xargs grep ${_word} /dev/null done Tips: - The quotes in ${_word} are probably optional, but it's better to be safe than sorry :-) - The /dev/null is there so that grep will get at least 2 file arguments, even if there is just one file in the current directory, effectively forcing grep(1) to print the filename of this one file if it happens to match the pattern. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Simple bash script to grep files for bad keywords
On 2005-03-23 12:29, Brian John [EMAIL PROTECTED] wrote: Hello, I am trying to write a simple bash script that will grep all files in a directory (except ones that start with 00) for certain bad keywords. Here is what I have so far: #!/bin/bash # This is a simple script to check all sql scripts for bad keywords BAD_KEYWORDS='spool echo timing commit rollback' for i in $BAD_KEYWORDS; do echo *; echo GREPing for bad keyword '$i' echo *; grep $i ./*; done However, I'm not sure how to make it not grep the files that start with 00. Can anyone help me with this? Use xargs, since it will buy you the extra feature of being able to search through arbitrarily large numbers of files: for _word in ${BAD_KEYWORDS} ;do find . | grep -v '^/00' |\ xargs grep ${_word} /dev/null done Tips: - The quotes in ${_word} are probably optional, but it's better to be safe than sorry :-) - The /dev/null is there so that grep will get at least 2 file arguments, even if there is just one file in the current directory, effectively forcing grep(1) to print the filename of this one file if it happens to match the pattern. Cool, I think I get it for the most part. However, what exactly am I doing when I am piping to xargs? I can see that the filenames not starting with '00' will be piped, but what does the '\' do? Sorry, I am really new to scripting and *nix in general. But I am a programmer so I learn fast. Thanks! /Brian ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Simple bash script to grep files for bad keywords
Brian John wrote: On 2005-03-23 12:29, Brian John [EMAIL PROTECTED] wrote: Hello, I am trying to write a simple bash script that will grep all files in a directory (except ones that start with 00) for certain bad keywords. Here is what I have so far: #!/bin/bash # This is a simple script to check all sql scripts for bad keywords BAD_KEYWORDS='spool echo timing commit rollback' for i in $BAD_KEYWORDS; do echo *; echo GREPing for bad keyword '$i' echo *; grep $i ./*; done However, I'm not sure how to make it not grep the files that start with 00. Can anyone help me with this? Use xargs, since it will buy you the extra feature of being able to search through arbitrarily large numbers of files: for _word in ${BAD_KEYWORDS} ;do find . | grep -v '^/00' |\ xargs grep ${_word} /dev/null done Tips: - The quotes in ${_word} are probably optional, but it's better to be safe than sorry :-) - The /dev/null is there so that grep will get at least 2 file arguments, even if there is just one file in the current directory, effectively forcing grep(1) to print the filename of this one file if it happens to match the pattern. Cool, I think I get it for the most part. However, what exactly am I doing when I am piping to xargs? I can see that the filenames not starting with '00' will be piped, but what does the '\' do? Sorry, I am really new to scripting and *nix in general. But I am a programmer so I learn fast. Thanks! /Brian ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED] I can see that the filenames not starting with '00' will be piped, but what does the '\' do? The '\' means pretend I typed the next line on this line he used it because most mailers wrap the e-mail lines at 80 characters, but it needs to be on one line to work. If you wrote it on one line in the script file, you can omit that '\' Luck -- END - Philip M. Gollucci Senior Developer - Liquidity Services Inc. Phone: 202.568.6268 (Direct) E-Mail: [EMAIL PROTECTED] Web:http://www.liquidation.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Bash script programming. Sending commands to a 'screen' session
I just accidentily wiped one of my script folders. Really no big deal, I'm not doing multiK scripts. But I had one nice script laying around. It created an additional window in a named screen session and started a bittorrentheadless session within. I do remember that it took me some days to solve this last time, and I have only been sitting three hours so far. (No, I don't recall the exact syntax). I'm struggeling with this script now: #!/bin/bash echo Starting download: $1 screen -r p2pA -X screen nice -n 20 /usr/local/BitTorrent-3.4.2/btdownloadheadless.py --max_uploads 4 \$1\ But it just don't do anything useful. If I run that line from prompt, replacing $1 with a filename, then it works. It's surely something to do with citationmarks around arguments etc. but I just don't see it. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
A little Bash script to help you.
On Mon, 2003-08-11 at 17:49, Constantine wrote: Hello! I am writing a script, which involves unzipping some files. I would have to unzip 4 different zip-files from some directory, and I would need to unzip them to the directory, which would have the same name in it as the original zip-file, i.e. I would like to run something like ls *.zip, have each file name recorded in some variable, and do a loop like unzip $filename[$i] -d $filename[$i].unzipped/. Can someone help me with the code? How can I put the results of a command to a variable? The following assumes you have the bash port installed. Just change the ARCHIVE_DIR variable to the full path of the directory in which you have your zip files. Make sure to save this code to a file in ~/bin. I suggest pasting the following code into ~/bin/munzip.sh. Then type chmod +x ~/bin/munzip.sh, then rehash, and then munzip.sh. ** #!/usr/bin/env bash ARCHIVE_DIR=~/archive cd $ARCHIVE_DIR # you don't need ls *.zip for zip in * do # just for diagnostics echo $zip # here's the command you want. unzip $zip -d $zip.unzipped/ done exit 0 ** -- Matthew Graybosch http://www.starbreaker.net If you take a stand on a dead empty hand, never let 'em see your cards. --Savatage ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
RE: Using bc in bash script
On Thu, Aug 14, 2003 at 10:46:45AM -0500, Charles Howse wrote: Hello List, I've migrated from Redhat Linux 9 to FreeBSD 4.8-RELEASE, character mode - no gui. I'm trying to calculate the number of seconds between $start_time and $end_time in a bash script. Start_time=`date +%s` # Seconds past midnight at start of script [ do lots of stuff ] End_time=`date +%s` # Seconds past midnight at end of script Then I want to: et=`bc $end_time - $start_time` to get the number of seconds or fractions of seconds elapsed. How about: et=`echo $end_time - $start_time | bc` Hi Jez, thanks for the reply! As my daughter would say, Well, DUHHH! That worked fine, it reported 0 seconds. Can I refine it to give me something like: .784 seconds? ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Using bc in bash script
I'm trying to calculate the number of seconds between $start_time and $end_time in a bash script. Bash has built-in integer arithmetic: et=$[End_time - Start_time] -- Richard ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
RE: Using bc in bash script
Charles, This will set bc precision to 5 decimal places: et=`echo scale=5 ; $end_time - $start_time | bc` Ohhh, I was really hoping on that one...but no, it still reports 0 seconds. Maybe there's something in the script itself that's messing this up. Here is the entire script: #!/usr/local/bin/bash # Daily Report # Declare variables start_time=`date +%s` time1=`date +%R` month=`date +%b` day=`date +%e` # Cleanup files if [ -a /root/daily.log ] ; then rm /root/daily.log fi # Main report header echo Daily Report for Larry for `date '+%A, %B %d %Y'`. /root/daily.log echo /root/daily.log echo /root/daily.log # OS header echo Current Operating System /root/daily.log echo /root/daily.log uname -sr /root/daily.log echo /root/daily.log # Uptime Header echo Uptime /root/daily.log echo /root/daily.log uptime /root/daily.log echo /root/daily.log # Crontab Header echo Cron Jobs /root/daily.log echo /root/daily.log crontab -l /root/daily.log echo /root/daily.log # Last Header echo Logins today /root/daily.log echo /root/daily.log last | grep $month $day /root/daily.log echo /root/daily.log # Superuser Header echo Accounts with uid = 0 (Superusers) /root/daily.log echo /root/daily.log awk -F: '( $3 == 0 ) { print $1 }' /etc/passwd /root/daily.log echo /root/daily.log # /etc/passwd Header echo Accounts that have a valid shell /root/daily.log echo /root/daily.log cat /etc/passwd | egrep -v (nologin|uucico|\#) /root/daily.log echo /root/daily.log # DF Header echo Disk Free space /root/daily.log echo /root/daily.log df -h /root/daily.log echo /root/daily.log # netstat Header echo Netstat -an results /root/daily.log echo /root/daily.log netstat -an /root/daily.log echo /root/daily.log # ifconfig echo Status of network interfaces /root/daily.log echo /root/daily.log ifconfig /root/daily.log echo /root/daily.log # Compute the elapsed time echo Elapsed Time /root/daily.log echo /root/daily.log echo Report completed at: `date +%R` /root/daily.log echo Report begun at: $time1 /root/daily.log end_time=`date +%s` et=`echo scale=5 ; $end_time - $start_time | bc` echoElapsed Time: $et seconds /root/daily.log echo /root/daily.log # File Modification Date echo Last modified /root/daily.log echo /root/daily.log ls -l /root/bin/daily_report | cut -d -f9,10,11 /root/daily.log # Mail to Charles cat /root/daily.log | mail -s Daily Report from Larry charles ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Using bc in bash script
On Thu, Aug 14, 2003 at 11:08:21AM -0500, Charles Howse wrote: On Thu, Aug 14, 2003 at 10:46:45AM -0500, Charles Howse wrote: Hello List, I've migrated from Redhat Linux 9 to FreeBSD 4.8-RELEASE, character mode - no gui. I'm trying to calculate the number of seconds between $start_time and $end_time in a bash script. Start_time=`date +%s` # Seconds past midnight at start of script [ do lots of stuff ] End_time=`date +%s` # Seconds past midnight at end of script Then I want to: et=`bc $end_time - $start_time` to get the number of seconds or fractions of seconds elapsed. How about: et=`echo $end_time - $start_time | bc` Hi Jez, thanks for the reply! As my daughter would say, Well, DUHHH! :) That worked fine, it reported 0 seconds. Hah, computers are just too damn fast :) Can I refine it to give me something like: .784 seconds? You could use 'time' perhaps to time whatever it is you're timing - I'm not overly familiar with the time(1) command though, best to check the man page :) -- Jez http://www.munk.nu/ ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Using bc in bash script
At 2003-08-14T16:45:56Z, Charles Howse [EMAIL PROTECTED] writes: I think this whole thing is dependent on the fact that `date +%s` reports integers. As his daughter says, DUUUH! I only saw the $end_time and $start_time variables, and not their origins. I'll go back to lurking now. :) -- Kirk Strauser pgp0.pgp Description: PGP signature
Re: Using bc in bash script
On Thu, Aug 14, 2003 at 12:58:01PM -0500 or thereabouts, Stephen Hilton wrote: On Thu, 14 Aug 2003 18:34:25 +0100 Jez Hancock [EMAIL PROTECTED] wrote: On Thu, Aug 14, 2003 at 12:23:34PM -0500, Stephen Hilton wrote: On Thu, 14 Aug 2003 12:11:55 -0500 Charles Howse [EMAIL PROTECTED] wrote: Charles, This will set bc precision to 5 decimal places: et=`echo scale=5 ; $end_time - $start_time | bc` Ohhh, I was really hoping on that one...but no, it still reports 0 seconds. Sorry I jumped the gun there, the scale is needed for this to work but the date +%s willonly resolve into whole seconds after reading the date man page. I sure am curious as to how to solve this also, the /usr/bin/time command man page says this: -snip-- DESCRIPTION The time utility executes and times the specified utility. After the utility finishes, time writes to the standard error stream, (in seconds): the total time elapsed, the time used to execute the utility process and the time consumed by system overhead. -snip-- So that looks like seconds only also. The precision is in hundredths of a second as I understand it from playing with time(!): #!/bin/sh time_file=tmp.time time=time -a -o $time_file $time cat /var/log/messages /dev/null 21 $time cat /var/log/maillog /dev/null 21 awk '{sum+=$1}END{print sum}' $time_file rm $time_file which outputs: [18:34:03] [EMAIL PROTECTED] /home/munk# sh tmp.sh 0.01 This simple script just times each cat command and appends the output from time to the $time_file, then prints out the sum of the first columns of the time outputs found in the time file. Just an idea. -- Jez, Your shell script works fine for me, resolving to 100th's of a second. Looks like a good answer for Charles :-) I still am wondering why the date command does not have a format string for seconds (down to 100th's) like +%ss and also why the time command stops at 100th's when other programs resolve time to 5 or 6 decimal places ? All the good % things are taken :-) Here are three ways of doing it: % cat gettimeofday.c 'EOF' #include stdio.h #include sys/time.h int main() { struct timeval tv; struct timezone unused; gettimeofday (tv, unused); printf (%li.%li\n, tv.tv_sec, tv.tv_usec); return 0; } EOF % cc -o gettimeofday gettimeofday.c % ./gettimeofday; echo hello, world; ./gettimeofday 1060886109.667054 hello, world 1060886109.687446 % gettimeofday() { perl -MTime::HiRes=gettimeofday -e '($sec, $usec) = gettimeofday(); print $sec, ., $usec, \n' } % gettimeofday; echo hello, world; gettimeofday 1060886661.274900 hello, world 1060886661.313071 % gettimeofday2() { perl 'EOF' $now = pack (LL, ()); syscall (116, $now, 0) != 1 or die gettimeofday: $!; @now = unpack (LL, $now); print $now[0], ., $now[1], \n; EOF } % gettimeofday2; echo hello, world; gettimeofday2 1060887546.767676 hello, world 1060887546.938097 % rm gettimeofday gettimeofday.c % unset gettimeofday % unset gettimeofday2 The first one (the C program) works anywhere but you have to compile it. The second one (perl -MTime::HiRes...) works if you have either Time::HiRes from CPAN or perl=5.8. The third one (perl ... syscall 116 ...) is specific to FreeBSD/i386 and a bit slower, but it works. HTH -- Josh Thanks for sharing the info, Stephen Hilton [EMAIL PROTECTED] ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED] ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Using bc in bash script
On Thu, 14 Aug 2003 12:11:55 -0500 Charles Howse [EMAIL PROTECTED] wrote: Charles, This will set bc precision to 5 decimal places: et=`echo scale=5 ; $end_time - $start_time | bc` Ohhh, I was really hoping on that one...but no, it still reports 0 seconds. Sorry I jumped the gun there, the scale is needed for this to work but the date +%s willonly resolve into whole seconds after reading the date man page. I sure am curious as to how to solve this also, the /usr/bin/time command man page says this: -snip-- DESCRIPTION The time utility executes and times the specified utility. After the utility finishes, time writes to the standard error stream, (in seconds): the total time elapsed, the time used to execute the utility process and the time consumed by system overhead. -snip-- So that looks like seconds only also. A quick browse through man sh and man bash look like their builtin time commands also resolve to seconds. Curiousy, Stephen Hilton [EMAIL PROTECTED] ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Using bc in bash script
At 2003-08-14T16:08:21Z, Charles Howse [EMAIL PROTECTED] writes: Can I refine it to give me something like: .784 seconds? Use bc -l instead of bc. That should do it. -- Kirk Strauser pgp0.pgp Description: PGP signature
RE: Using bc in bash script
The precision is in hundredths of a second as I understand it from playing with time(!): #!/bin/sh time_file=tmp.time time=time -a -o $time_file $time cat /var/log/messages /dev/null 21 $time cat /var/log/maillog /dev/null 21 awk '{sum+=$1}END{print sum}' $time_file rm $time_file which outputs: [18:34:03] [EMAIL PROTECTED] /home/munk# sh tmp.sh 0.01 This simple script just times each cat command and appends the output from time to the $time_file, then prints out the sum of the first columns of the time outputs found in the time file. Just an idea. First, let me thank everyone who responded to my *first* post to this list. This seems like a really good 'community', I'm gonna hang around. ;-) Now, I've written many bash scripts in RedHat, and FreeBSD's set of commands *is* just a little bit different, but I'm gonna need a little shove in the right direction here. I'm not asking for anyone to re-write anything for me, just a friendly suggestion as to exactly to implement the 'time' command so that the file that my daily_report script generates includes the elapsed time in hundredths on the proper line. I've attached the script for reference. daily_report.sh Description: Binary data ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Using bc in bash script
On Thu, Aug 14, 2003 at 10:46:45AM -0500, Charles Howse wrote: Hello List, I've migrated from Redhat Linux 9 to FreeBSD 4.8-RELEASE, character mode - no gui. I'm trying to calculate the number of seconds between $start_time and $end_time in a bash script. Start_time=`date +%s` # Seconds past midnight at start of script [ do lots of stuff ] End_time=`date +%s` # Seconds past midnight at end of script Then I want to: et=`bc $end_time - $start_time` to get the number of seconds or fractions of seconds elapsed. How about: et=`echo $end_time - $start_time | bc` -- Jez http://www.munk.nu/ ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Using bc in bash script
Hello List, I've migrated from Redhat Linux 9 to FreeBSD 4.8-RELEASE, character mode - no gui. I'm trying to calculate the number of seconds between $start_time and $end_time in a bash script. Start_time=`date +%s` # Seconds past midnight at start of script [ do lots of stuff ] End_time=`date +%s` # Seconds past midnight at end of script Then I want to: et=`bc $end_time - $start_time` to get the number of seconds or fractions of seconds elapsed. That's not working, bc is printing: file 1085492365 not available From what I've read in man bc, it's probably assuming those variables are files. I've read man bc, but I'm afraid it's beyond me (makes gesture with hand passing over top of head). What is the proper way to get the elapsed time here? If the answer is in man bc, would someone please be kind enough to point out the section so I can re-read it. Thanks, Charles ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]