# String extraction from user input - sh

**URL:** <https://community.unix.com/t/string-extraction-from-user-input-sh/151663>\
**Category:** Shell Programming and Scripting\
**Created:** [February 19, 2005, 7:19am UTC](https://community.unix.com/t/string-extraction-from-user-input-sh/151663 "2005-02-19T07:19:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vino](https://community.unix.com/user_avatar/community.unix.com/vino/32/156_2.png) [@vino](https://community.unix.com/u/vino)\
**Post date:** [February 19, 2005, 7:19am UTC](https://community.unix.com/t/string-extraction-from-user-input-sh/151663/1 "2005-02-19T07:19:07Z")

</div>

Hi,

I have a shell script to build components of a product. The follow snippet will explain what I am doing.

# !/bin/sh  
for choice in "$@" ;  
do  
case $choice in  
"o") echo "Calling $choice" ; o ;;  
"i") echo "Calling $choice" ; i ;;  
"w") echo "Calling $choice" ; w ;;  
"m") echo "Calling $choice" ; m ;;  
"all") o ; i ; w ; m ; ftp ;;  
\*) usage ;;  
esac  
done

Now when I call the script I call it in the following manner: sh build.sh i o w m

What I plan to do is take the input as a single argument containing the required components; sh build.sh iowm

Is there anyway to extract the individual charaters from the input string. And then check for i/w/o/m and then carry on with the build.

Also is there a way to check for 0 arguments using $@ . Currently I use  
if test $# = 0

Thanks in advance,  
Vino

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [February 19, 2005, 7:59am UTC](https://community.unix.com/t/string-extraction-from-user-input-sh/151663/2 "2005-02-19T07:59:05Z")

</div>

What system are you using? Post the output of "uname -a".

---

<div class="post-metadata">

**Author:** ![vino](https://community.unix.com/user_avatar/community.unix.com/vino/32/156_2.png) [@vino](https://community.unix.com/u/vino)\
**Post date:** [February 19, 2005, 8:25am UTC](https://community.unix.com/t/string-extraction-from-user-input-sh/151663/3 "2005-02-19T08:25:42Z")

</div>

Linux staci21 2.4.21-27.ELsmp #1 SMP Wed Dec 1 21:59:02 EST 2004 i686 i686 i386 GNU/Linux

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [February 19, 2005, 9:05am UTC](https://community.unix.com/t/string-extraction-from-user-input-sh/151663/4 "2005-02-19T09:05:28Z")

</div>

That makes a big difference. Your /bin/sh is actually bash rather than the old bourne shell. Try something like:

```nohighlight
#! /bin/sh

parm=$1

while ((${#parm})) ; do
        rest=${parm#?}
        char1=${parm%$rest}
        parm=$rest
        echo $char1 $parm
done
exit 0

```

---

<div class="post-metadata">

**Author:** ![vino](https://community.unix.com/user_avatar/community.unix.com/vino/32/156_2.png) [@vino](https://community.unix.com/u/vino)\
**Post date:** [February 20, 2005, 3:06am UTC](https://community.unix.com/t/string-extraction-from-user-input-sh/151663/5 "2005-02-20T03:06:42Z")

</div>

But I have set the sh to /bin/sh. Does it still make a difference ???

---

<div class="post-metadata">

**Author:** ![zazzybob](https://community.unix.com/user_avatar/community.unix.com/zazzybob/32/112_2.png) [@zazzybob](https://community.unix.com/u/zazzybob)\
**Post date:** [February 20, 2005, 3:27am UTC](https://community.unix.com/t/string-extraction-from-user-input-sh/151663/6 "2005-02-20T03:27:22Z")

</div>

> [@vino](#):
>
> But I have set the sh to /bin/sh. Does it still make a difference ???

No, it shouldn't matter - most Linux systems have /bin/sh linked to /bin/bash - to check, do

ls -li /bin/sh /bin/bash

see if a) the inode numbers are the same or b) there's a soft link.

Cheers  
ZB

---

<div class="post-metadata">

**Author:** ![vino](https://community.unix.com/user_avatar/community.unix.com/vino/32/156_2.png) [@vino](https://community.unix.com/u/vino)\
**Post date:** [February 20, 2005, 3:30am UTC](https://community.unix.com/t/string-extraction-from-user-input-sh/151663/7 "2005-02-20T03:30:32Z")

</div>

Perderabo,

Thanks for the solution. That extracts the first character. Is there anyway I can search for a character in the input string. For eg. if the input string is iowm, I need to search for the character o in the string. Something like grep on a charatcer string rather than a file.

Vino

---

<div class="post-metadata">

**Author:** ![vino](https://community.unix.com/user_avatar/community.unix.com/vino/32/156_2.png) [@vino](https://community.unix.com/u/vino)\
**Post date:** [February 20, 2005, 4:34am UTC](https://community.unix.com/t/string-extraction-from-user-input-sh/151663/8 "2005-02-20T04:34:39Z")

</div>

Got it.

Solution is  
if echo "$word" | grep q "$letter\_sequence"

It was there in the Advanced Bash scripting guide. It can be found at [http://www.tldp.org/LDP/abs/abs-guide.pdf](http://www.tldp.org/LDP/abs/abs-guide.pdf)

Thanks Perderabo  
Thanks zazzybob.

Vino

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [February 20, 2005, 9:54am UTC](https://community.unix.com/t/string-extraction-from-user-input-sh/151663/9 "2005-02-20T09:54:18Z")

</div>

> [@vino](#):
>
> Got it.
> 
> Solution is  
> if echo "$word" | grep q "$letter\_sequence"
> 
> It was there in the Advanced Bash scripting guide. It can be found at [http://www.tldp.org/LDP/abs/abs-guide.pdf](http://www.tldp.org/LDP/abs/abs-guide.pdf)

That is not a great solution. It doesn't make sense to invoke an external program to do what bash can do internally...

if [[$var = \*k\*]] ; then echo yes ; fi

will test var for the presence of a "k".
