Re: Shell scripting tips and tricks (was: I need a date! )
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 At some point hitherto, Jerry Feldman hath spake thusly: > Actually, [ is a link to test. Linux uses a symlink, some Unixes use hard > links. > -rwxr-xr-x1 root root17496 Sep 20 2001 /usr/bin/test > lrwxrwxrwx1 root root4 Dec 1 13:42 /usr/bin/[ -> test > > And yes, BASH has it built in, but on some of the older Bourne shells it is > not built in. As Tom points out, modern flavors of Unix generally use a Bourne-derivative, Posix-compliant shell (often referred to as the Posix shell) which includes these as a built-in. The original Bourne shell is generally shipped on these systems too, but you often need to modify your configuration or specify the full path to it to use it. Most will not do this, as the Bourne shell lacks nice features that most other modern shells have. Few shells still rely on the existence of /usr/bin/test or its link to [, though I do often see it used by the make utility - -- Derek Martin [EMAIL PROTECTED] - - I prefer mail encrypted with PGP/GPG! GnuPG Key ID: 0x81CFE75D Retrieve my public key at http://pgp.mit.edu Learn more about it at http://www.gnupg.org -BEGIN PGP SIGNATURE- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQE8xFGXdjdlQoHP510RAtmRAJ46Yn2rVMUfs0i0wkBmTSZbK7WKegCgt5Gh pzQE9zjvzRCBGTO5cZw4oGI= =upIm -END PGP SIGNATURE- * To unsubscribe from this list, send mail to [EMAIL PROTECTED] with the text 'unsubscribe gnhlug' in the message body. *
Re: Shell scripting tips and tricks (was: I need a date! )
Yes, but several Unix systems supply an old style Bourne SH as well as a POSIX shell (essentially ksh scaled down) and ksh. KSH implemented the [[ as a way of internalizing the condition. For the most part, the syntax will work in most Bourne derived shells. if [ ] then # true stuff fi If the [ is internalized, then the script will perform better. On 22 Apr 2002 at 10:14, Steven W. Orr wrote: > Just to muddy the waters even further... > Bourne shell under Linux is actually a link to bash. Both the [ operator > and the test command are both builtins to both Bourne and bash. The [[ > operator is actually different from a builtin; it is considered a keyword. > It also has different syntax in that certain operators are not legal and > vice versa. If you ever really and truly ever want to run the binary test > (though I have no idea why you would), you have to explicity invoke it via > pathname. e.g., > > if /usr/bin/test "${x}" -eq 44 > then > echo walla > fi > > -- > -Time flies like the wind. Fruit flies like a banana. Stranger things have - > -happened but none stranger than this. Does your driver's license say Organ > -Donor?Black holes are where God divided by zero. Listen to me! We are all- > -individuals! What if this weren't a hypothetical question? [EMAIL PROTECTED] > > > > * > To unsubscribe from this list, send mail to [EMAIL PROTECTED] > with the text 'unsubscribe gnhlug' in the message body. > * -- Jerry Feldman <[EMAIL PROTECTED]> Associate Director Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9 * To unsubscribe from this list, send mail to [EMAIL PROTECTED] with the text 'unsubscribe gnhlug' in the message body. *
Re: Shell scripting tips and tricks (was: I need a date! )
That is also correct. I had forgotten. But, my original point is that it is probably better to specify the full pathname of a command. X=$(/bin/ls) will generally result in identical results as: PATH=/bin:$(PATH) X=$(ls) But, specifying the full pathname also forces the command to be executed even if some version of the shell internalizes it or if the script somehow sources ~/.bashrc directly or indirectly. On 22 Apr 2002 at 7:28, Karl J. Runge wrote: > I don't believe user's aliases are active at all in a general script. > The ~/.bashrc ~/.profile, etc are not sourced for non-interactive shells. > Only in an interactive shell will the these aliases be available. -- Jerry Feldman <[EMAIL PROTECTED]> Associate Director Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9 * To unsubscribe from this list, send mail to [EMAIL PROTECTED] with the text 'unsubscribe gnhlug' in the message body. *
Re: Shell scripting tips and tricks (was: I need a date! )
On Mon, 22 Apr 2002, "Tom Buskey" <[EMAIL PROTECTED]> wrote: > > As I said in not so many words, modern shells have it built in. I don't > consider Bourne a modern shell. I believe Jerry is saying some of the newer implementations of /bin/sh have [ as a builtin. For example, [ has been a builtin on Solaris forever. This improves performance of scripts, e.g. a big loop with lots of if tests. > > Speaking of builtin commands vs. Unix commands. > > While setting the PATH environment variable in a script is a good idea, > > specifying the full path to a standard command may be a better way. The > > user may have aliased the commands: > > alias rm='rm -i' > > So, in your script (or makefile), setting a variable for the command will > > bypass any aliases: > > RM=/bin/rm > > Or RM=\rm so that aliasing is negated. I don't alias rm and my root > accounts don't either. I don't believe user's aliases are active at all in a general script. The ~/.bashrc ~/.profile, etc are not sourced for non-interactive shells. Only in an interactive shell will the these aliases be available. Karl * To unsubscribe from this list, send mail to [EMAIL PROTECTED] with the text 'unsubscribe gnhlug' in the message body. *
Re: Shell scripting tips and tricks (was: I need a date! )
=>"Jerry Feldman" said: =>>Actually, [ is a link to test. Linux uses a symlink, some Unixes use hard =>>links. =>>-rwxr-xr-x1 root root17496 Sep 20 2001 /usr/bin/test =>>lrwxrwxrwx1 root root4 Dec 1 13:42 /usr/bin/[ -> test =>> =>>And yes, BASH has it built in, but on some of the older Bourne shells it is =>>not built in. => =>As I said in not so many words, modern shells have it built in. I don't =>consider Bourne a modern shell. If I want test, I use test, not [. =>I've also used versions of Bourne that didn't have functions for =>instance (Ultrix). Just to muddy the waters even further... Bourne shell under Linux is actually a link to bash. Both the [ operator and the test command are both builtins to both Bourne and bash. The [[ operator is actually different from a builtin; it is considered a keyword. It also has different syntax in that certain operators are not legal and vice versa. If you ever really and truly ever want to run the binary test (though I have no idea why you would), you have to explicity invoke it via pathname. e.g., if /usr/bin/test "${x}" -eq 44 then echo walla fi -- -Time flies like the wind. Fruit flies like a banana. Stranger things have - -happened but none stranger than this. Does your driver's license say Organ -Donor?Black holes are where God divided by zero. Listen to me! We are all- -individuals! What if this weren't a hypothetical question? [EMAIL PROTECTED] * To unsubscribe from this list, send mail to [EMAIL PROTECTED] with the text 'unsubscribe gnhlug' in the message body. *
Re: Shell scripting tips and tricks (was: I need a date! )
"Jerry Feldman" said: >Actually, [ is a link to test. Linux uses a symlink, some Unixes use hard >links. >-rwxr-xr-x1 root root17496 Sep 20 2001 /usr/bin/test >lrwxrwxrwx1 root root4 Dec 1 13:42 /usr/bin/[ -> test > >And yes, BASH has it built in, but on some of the older Bourne shells it is >not built in. As I said in not so many words, modern shells have it built in. I don't consider Bourne a modern shell. If I want test, I use test, not [. I've also used versions of Bourne that didn't have functions for instance (Ultrix). >Speaking of builtin commands vs. Unix commands. >While setting the PATH environment variable in a script is a good idea, >specifying the full path to a standard command may be a better way. The >user may have aliased the commands: >alias rm='rm -i' >So, in your script (or makefile), setting a variable for the command will >bypass any aliases: >RM=/bin/rm Or RM=\rm so that aliasing is negated. I don't alias rm and my root accounts don't either. > >Same with any other Unix command. > >So, for the reason of unpredictable aliases, >X=$(/usr/bin/ls) >Is the more predicatable way to proceed. Maybe. Now if I'm trying to run gnutar, on linux, it's /usr/bin/tar. On my solaris box it depends on the site. I've seen /usr/local/bin, /opt/ something, /usr/local/gnu, /local, etc. Even variations for each archetecture. I've also seen it called gtar or gnutar. It depends, of course, on your environment. Sometimes you want the full path, sometimes not. Specifying the full path for each program makes portability more difficult (think #ifdef in C). SunOS puts many programs in /usr/ccs for instance. Solaris has stuff in /usr/ucb. HP-UX and SGI use /usr/ bsd. -- --- Tom Buskey * To unsubscribe from this list, send mail to [EMAIL PROTECTED] with the text 'unsubscribe gnhlug' in the message body. *
Re: Shell scripting tips and tricks (was: I need a date! )
Actually, [ is a link to test. Linux uses a symlink, some Unixes use hard links. -rwxr-xr-x1 root root17496 Sep 20 2001 /usr/bin/test lrwxrwxrwx1 root root4 Dec 1 13:42 /usr/bin/[ -> test And yes, BASH has it built in, but on some of the older Bourne shells it is not built in. Speaking of builtin commands vs. Unix commands. While setting the PATH environment variable in a script is a good idea, specifying the full path to a standard command may be a better way. The user may have aliased the commands: alias rm='rm -i' So, in your script (or makefile), setting a variable for the command will bypass any aliases: RM=/bin/rm Same with any other Unix command. So, for the reason of unpredictable aliases, X=$(/usr/bin/ls) Is the more predicatable way to proceed. On 21 Apr 2002 at 22:31, Tom Buskey wrote: > > Benjamin Scott said: > > > If you are not interested in portability to older shells, here are some > >optimizations: > > > > > > math=$(( 1 + 1 )) # internal, easier, nestable > > > > if [[ a = b ]]; ... # internal > > I've had problems with [[ ]] on pdksh in the past. [ ] is also > internal on modern unixen. > > > > > Anyone else have some tips or tricks they would like to share? > > > > I've done lots of cross platform scripting. I find it's better to set > PATH in the top of the script rather then do things like: > > X=$(/usr/bin/ls) > > PATH=/usr/bin:$PATH > X=$(ls) > > I've also created something like an include file that I . at the top of > my scripts. It has functions I use & defines variables for thing like > mail. For instance: > date | $MAILER -s 'feedback' user@site > > On linux, MAILER=mail. Solaris, MAILER=mailx. > > Scripts attached. > -- Jerry Feldman <[EMAIL PROTECTED]> Associate Director Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9 * To unsubscribe from this list, send mail to [EMAIL PROTECTED] with the text 'unsubscribe gnhlug' in the message body. *
Re: Shell scripting tips and tricks (was: I need a date! )
On Sun, 21 Apr 2002, Derek D. Martin wrote: =>-BEGIN PGP SIGNED MESSAGE- =>Hash: SHA1 => =>At some point hitherto, Tom Buskey hath spake thusly: => =>> I've had problems with [[ ]] on pdksh in the past. [ ] is also =>> internal on modern unixen. => =>You can get the real ksh from David Korn's website. It's now freely =>available, though I don't know what the licensing terms are. If =>you're a Korn shell fan, you can have the real thing. => =>Don't know the URL... I'm sure a search will turn it up. The official site for ksh is oddly enough http://www.kornshell.com/software/ But somewhere I managed to find a src.rpm of it. This is the real deal: ksh93 If anyone wants it, I put up a copy on http://www2.syslang.net:8080/ksh93-2000.10.31.0-1.src.rpm Ksh93 is subtley different from bash and even more subtley different from ksk88. But for my money, the shells come in the following ranking: * csh (all flavors) Dangerous junk. * bash The best of the login shells * ksh (Both flavors, 88 and 93) The best choice for industrial script programming. * sh An old evil that sometimes still forces itself to be used. The differences are easily worth hours of lecture time over the right dark adult malted beverage. -- -Time flies like the wind. Fruit flies like a banana. Stranger things have - -happened but none stranger than this. Does your driver's license say Organ -Donor?Black holes are where God divided by zero. Listen to me! We are all- -individuals! What if this weren't a hypothetical question? [EMAIL PROTECTED] * To unsubscribe from this list, send mail to [EMAIL PROTECTED] with the text 'unsubscribe gnhlug' in the message body. *
Re: Shell scripting tips and tricks (was: I need a date! )
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 At some point hitherto, Tom Buskey hath spake thusly: > I've had problems with [[ ]] on pdksh in the past. [ ] is also > internal on modern unixen. You can get the real ksh from David Korn's website. It's now freely available, though I don't know what the licensing terms are. If you're a Korn shell fan, you can have the real thing. Don't know the URL... I'm sure a search will turn it up. - -- Derek Martin [EMAIL PROTECTED] - - I prefer mail encrypted with PGP/GPG! GnuPG Key ID: 0x81CFE75D Retrieve my public key at http://pgp.mit.edu Learn more about it at http://www.gnupg.org -BEGIN PGP SIGNATURE- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQE8w4n5djdlQoHP510RAvsGAKCHU47oAAfS/flow4+gv7U0Ua5WMwCfV8R0 LC7GNEQa+28afEXlxWBuFC8= =50b3 -END PGP SIGNATURE- * To unsubscribe from this list, send mail to [EMAIL PROTECTED] with the text 'unsubscribe gnhlug' in the message body. *
Re: Shell scripting tips and tricks (was: I need a date! )
Benjamin Scott said: > If you are not interested in portability to older shells, here are some >optimizations: > > > math=$(( 1 + 1 )) # internal, easier, nestable > > if [[ a = b ]]; ... # internal I've had problems with [[ ]] on pdksh in the past. [ ] is also internal on modern unixen. > > Anyone else have some tips or tricks they would like to share? > I've done lots of cross platform scripting. I find it's better to set PATH in the top of the script rather then do things like: X=$(/usr/bin/ls) PATH=/usr/bin:$PATH X=$(ls) I've also created something like an include file that I . at the top of my scripts. It has functions I use & defines variables for thing like mail. For instance: date | $MAILER -s 'feedback' user@site On linux, MAILER=mail. Solaris, MAILER=mailx. Scripts attached. #!/bin/ksh . /shared/scripts/setup.ksh echo "Variables set in setup.ksh" echo "--" echo "PATH:" echo " $PATH" echo "" echo "- Current running program & logfile " echo "PROG = $PROG" echo "LOGFILE = $LOGFILE" echo "" echo "- time ---" echo "set_HOUR = $set_HOUR" echo "set_MINUTE= $set_MINUTE" echo "set_YEAR = $set_YEAR" echo "set_MONTH = $set_MONTH" echo "set_DAY = $set_DAY" echo "" echo "- ID the system --" echo "set_HOST = $set_HOST" echo "set_SYSREV= $set_SYSREV" echo "set_SYSTEM= $set_SYSTEM" echo "set_ipaddr= $set_ipaddr" echo "set_mask = $set_mask" echo "set_ipmask= $set_ipmask" echo "set_broadaddr = $set_broadaddr" echo "" echo "- ID the user " echo "set_USER = $set_USER" echo "set_TRUEUSER = $set_TRUEUSER" echo "EDITOR= $EDITOR" echo "" echo "- commands that change ---" echo "set_pscmd = $set_pscmd" echo "set_RSH = $set_RSH" echo "set_MAILER= $set_MAILER" echo ' ex: $set_MAILER -s "subject line" dest1,dest2 < $FILE' echo "PAGER = $PAGER" echo "set_lsargs= $set_lsargs" echo 'set_HOSTS - list of hosts /etc/host w/o localhost & comments' echo $set_HOSTS echo "" echo '- useful *functions* ---' echo 'pushd, popd - push & pop directories' echo 'set_DF - df in k, not half k' echo 'set_DFL - df local disks' echo '' echo 'set_psg - ps | egrep $1 # this is a function, put arg in quotes' echo 'set_psk - ps | egrep $1 | egrep -v egrep | awk out $2 | xargs kill' echo 'set_psk9 - set_psk w/ kill -9' #! /bin/ksh # file to . to get all setups # use: # . /shared/scripts/setup.ksh # run /shared/scripts/setup to see what is set # These are the maximum PATHs PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/etc:/etc:/usr/ucb:/usr/openwin/bin:/usr/bin/X11:/shared/scripts:/usr/local/bin:/usr/local/sbin:/usr/ccs/bin # IFF you need to add to the PATH, do this: # PATH=$PATH:/usr/local/bin # etc... # because the above paths will not hang. /usr/local might IFF it's NFS mounted # and of course /shared/scripts will be available if this is being sourced PROG=$(basename $0) LOGFILE="/tmp/${PROG}log.$$" set_HOST=$(uname -n |cut -d. -f1) set_SYSREV=$(uname -r) set_SYSTEM=$(uname -s) case $set_SYSTEM in HP-UX) # hp-UX set_RSH=remsh iparg=lan0 ;; SunOS) set_RSH=rsh iparg=-a case $set_SYSREV in 5*) # Solaris set_lsargs=-l ;; 4*) # SunOS set_lsargs=-lg ;; esac esac # Whats the proper ps command? # test BSD and System V style psax=$(ps ax 2>&1 | wc -l) pse=$(ps -ef 2>&1 | wc -l) if [[ $psax -gt $pse ]] then set_pscmd=auxww # BSDish ps else set_pscmd=-ef # SYSVish ps (btw - OSF picks this one) fi unset psax unset pse # # SYS-V echo? # if test "`/bin/echo 'blah\c'`" = "blah\c" then EFLAG="-n" ENDER="" else EFLAG="" ENDER="\c" fi ECHO="/bin/echo ${EFLAG}" # example use: # ${ECHO} "some message: ${ENDER}" # which MAILER do I need to use? if [[ -f /usr/bin/mailx ]] then set_MAILER=/usr/bin/mailx elif [[ -f /usr/ucb/mail ]] then set_MAILER=/usr/ucb/mail else set_MAILER=mail fi # set the pager if [ $PAGER ] then : #preserve user's idea of a pager else # use our idea if [[ -x /usr/bin/less ]] then export PAGER=/usr/bin/less elif [[ -x /usr/local/bin/less ]] then export PAGER=/usr/local/bin/less else export PAGER=more fi fi if [ $EDITOR ] then if [[ $EDITOR != 'vi' ]] then : fi else EDITOR=vi fi set_USER=$(whoami) set_TRUEUSER=$(logname) # if $set_TRUEUSER != $set_USER, user SU'd # The time set_HOUR=$(date +%H) set_MINUTE=$(date +%M) set_YEAR=$(date | a
Shell scripting tips and tricks (was: I need a date! )
On Fri, 19 Apr 2002, at 5:25pm, Thomas M. Albright wrote: > it's the only way i know to add to variables: > today=`date +%j` > let tomorrow=$today+1 If you are not interested in portability to older shells, here are some optimizations: math=$(( 1 + 1 )) # internal, easier, nestable if [[ a = b ]]; ... # internal output=$( command ... ) # easier, nestable Anyone else have some tips or tricks they would like to share? > I know some bash. I'm actually moving along quite well on this particular > project, though it looks like it's going to be a lot longer then I > origianally thought. First rule of programming: Everything takes longer than you thought. :-) > I know just a little bit about scripting in (t)csh. Try to keep it that way. ;-) -- Ben Scott <[EMAIL PROTECTED]> | The opinions expressed in this message are those of the author and do not | | necessarily represent the views or policy of any other person, entity or | | organization. All information is provided without warranty of any kind. | * To unsubscribe from this list, send mail to [EMAIL PROTECTED] with the text 'unsubscribe gnhlug' in the message body. *