Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!ptsfa!lll-lcc!pyramid!prls!mips!dce From: dce@mips.UUCP Newsgroups: comp.unix.questions Subject: Re: Quotiing * in Bourne shell question Message-ID: <417@quacky.UUCP> Date: Fri, 22-May-87 10:37:14 EDT Article-I.D.: quacky.417 Posted: Fri May 22 10:37:14 1987 Date-Received: Sat, 23-May-87 10:47:45 EDT References: <1040@chinet.UUCP> <1028@mips.UUCP> Reply-To: dce@quacky.UUCP (David Elliott) Organization: MIPS Computer Systems, Sunnyvale, CA Lines: 96 In article <1028@mips.UUCP> hitz@mips.UUCP (David Hitz) writes: >In article <1040@chinet.UUCP> megabyte@chinet.UUCP (Dr. Megabyte) writes: >> >>I want to set a shell variable equal to an asterix (*). Then use that >>variable in seetting another shell variable and then finally use the >>second variable as a command line with the asterix NOT being expanded, >>I want it passed to the program. Here is an example: > >This is probably the best: > > area="*" > cmdline="echo foo '$area' bar" > eval $cmdline The last line should be eval "$cmdline" to prevent the shell from eating any other special characters. In this case, there are none, but we should try to handle them all. The general rule is: Always put double quotes around variables unless you know exactly what's in the variable and know it can't hurt you. If you can't know the values beforehand, you can check them. The following shell script takes two arguments: the new IFS (a null one uses the default) and the value to be checked. It prints a message saying whether or not the value contains the IFS characters: #!/bin/sh case "$1" in "") ;; *) IFS="$1" ;; esac case "$2" in *["$IFS"]*) echo "$2 has IFS($IFS) in it" ;; *) echo "$2 OK" ;; esac Best of all, the checking doesn't cost a single fork (the "echo" statements cost on most BSD systems, but that's not part of the check, per se). Anyway, to get back to the original problem: #!/bin/sh area="*" # set area to * cmdline="acego -q ovt.rpt $area" # run report $cmdline | lp -dDS180 # to lp in 1215 exit 0 # Bye-bye This should be #!/bin/sh area="*" cmdline="acego -q ovt.rpt '$area'" eval "$cmdline" | lp -dDS180 except that if $area could be set by the user, it could conceivably contain single quotes. I think that the following is the best solution (and I know that Dave Hitz will tell me if it isn't): #!/bin/sh area={some value} ... case "$area" in *"'"*) area=`echo "$area" | sed 's/'"'"'/'"'"'"'"'"'"'"'"'/g'` ;; esac cmdline="acego -q ovt.rpt '$area'" eval "$cmdline" | lp -dDS180 Again, it only costs an extra fork in the extreme case. In case you are baffled, the sed script is actually s/'/'"'"'/ which changes a value of x'y to x'"'"'y, which, when surrounded by single quotes is 'x'"'"'y', which, when processed by the shell, is passed to sub-commands as x'y. It's all clear as mud. -- David Elliott {decvax,ucbvax,ihnp4}!decwrl!mips!dce "With an a) like that, you've got a lot of nerve asking for a b)!"-P. Schaeffer