Dec 20, 2013 at 11:35:36AM -0500, john boris wrote:
> Lance,
> The perl script is called like this from another shell script that does
> some worlk
> 
> 
> perl weekdays.pl $school
> 
> Then in the perl script I am trying to print a long named based on the
> value of $school which ends up as $ARGV[0] in my perl script. What is
> giving me grief is that some of these titles contain single quotes. So I
> was trying to handle the single quote PITA in my PERL script. So in my
> shell script I find out which server I am running the script (That set of
> code works great for all of my scripts) so I set the variable to the
> Server's Long Name. When the name has a single quote I don't get the entire
> variable. The shell will break up the variable so "Cardinal  O'Hara" gets
> broke up when it gets passed to the PERL script as Cardinal. So in my shell
> script I set the variable for that server to
> 
> "\"Cardinal O'Hara\"" and the variable gets created as "Cardinal \O'Hara"
> but in the perl script that becomes "Cardinal  The O'Hara part gets split
> at the space or somewhere. I need that single quote as the people that read
> the report get testy. I figure I might just do it some other way as this is
> getting to a big time PITA for a stupid single quote.

Things to consider -

    #!/bin/bash

    #  It's a win if you can express 'magic' constants once
    #  and only once in a system.
    SCHOOLS="aa ab ac bb cc da db dc"
    # --- no changes required beyond this point ---


    #  Check your inputs, it saves hours of debugging

    usage() {
        echo "
    Usage: $0 <school id>
    where <school id> is one of $SCHOOLS
        "
        exit 1
    }

    # test for missing argument
    [ $1 ] || usage

    SCHOOL="$1"
    # check argument validity with regular expression
    if [[ ! "$SCHOOLS" =~ $SCHOOL ]] ; then
        usage
    fi
 
    #  Sometimes it's easier to turn a pesky quote into some
    #  easily handled marker, do the processing, and restore the
    #  original form later on.

    read -p "Enter a name with an apostrophe in it: " i
    echo "You typed $i"

    j=`echo "$i" | sed "s/[']/_MY_QUOTE_/g"`
    echo "the munged value is $j"

    #   (work with with $j without bothering about apostrophe's)

    k=`echo "$j" | sed "s/_MY_QUOTE_/'/g"`
    echo "The original value is $k"

Applied consistently, these idioms will save you lots
of time and frustration.

-- 
Charles Polisher

_______________________________________________
Tech mailing list
[email protected]
https://lists.lopsa.org/cgi-bin/mailman/listinfo/tech
This list provided by the League of Professional System Administrators
 http://lopsa.org/

Reply via email to