Unix Programming - anything similar to C string manipulation functions in shell scripting language

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > February 2004 > anything similar to C string manipulation functions in shell scripting language





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author anything similar to C string manipulation functions in shell scripting language
George Kooper

2004-02-16, 11:33 pm

anything similar to C string manipulation functions in shell
scripting language
Maurizio Loreti

2004-02-16, 11:33 pm

George Kooper <tentimes5@hotmail.com > writes:

> anything similar to C string manipulation functions in shell
> scripting language


strcpy(destination, source) <--> DESTINATION="${SOURCE}"
strcat(destination, source) <--> DESTINATION="${DESTINATION}${SOURCE}"

--
Maurizio Loreti http://www.pd.infn.it/~loreti/mlo.html
Dept. of Physics, Univ. of Padova, Italy ROT13: ybergv@cq.vasa.vg
Chris F.A. Johnson

2004-02-17, 2:34 am

On Tue, 17 Feb 2004 at 12:13 GMT, George Kooper wrote:
> anything similar to C string manipulation functions in shell
> scripting language


I just whipped these up in answer to this question. Most are so
trivial that I woudn't use them. In some, the function is more
complex than a simple in-line statement.

They more or less conform to the equivalent C functions. There's
no error checking, and they are not thoroughly tested:

strlen() { ## strlen STRING
echo ${#1}
}

strcpy() { ## strcpy VAR STRING
eval "$1=\"$2\""
}

strncpy() { ## strcpy VAR STRING N
eval "$1=\"`printf "%0.${3}s\" \"$2\"`\""
}

strcat() { ## strcat VAR STRING
eval "$1=\"\${$1}$2\""
}

strncat() { ## strncat VAR STRING N
eval "$1=\"\${$1}`printf "%${3}.${3}s" $2`\""
}

strchr() { ## STRING CHAR
echo ${1#${1%$2*}}
}

strrchr() { ## STRING CHAR
echo ${1#${1%%$2*}}
}

strcmp() { ## strcmp STRING1 STRING2
[ "$1" = "$2" ] && return
[ "$1" \< "$2" ] && return 1
return 2
}

strncmp() { ## strcmp STRING1 STRING2 N
eval strcmp `printf "\"%0.$3s\" \"%0.$3s\"" "$1" "$2"`
}

strdup() { ## strdup STRING
printf "%s\n" "$1"
}

strndup() { ## strdup STRING N
printf "%0.${2}s\n" "$1"
}

strtok() { ## strtok STRING DELIM
[ -n "$1" ] && _STRTOK=$1
_STRTOK=${_STRTOK%[$2]*}
printf "%s\n" "$_STRTOK"
}


--
Chris F.A. Johnson http://cfaj.freeshell.org
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Mark Rafn

2004-02-17, 11:34 am

George Kooper <tentimes5@hotmail.com > wrote:
>anything similar to C string manipulation functions in shell
>scripting language


Please provide more detail about what you're trying to do. Include such
arcane information as which shell (aka "shell scripting language") you're
using, what external programs you have available, and what you've tried so far
to solve what problems.

The 'expr' command is available on almost all unices, and covers at least the
basic needs.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com