# How to read in a part of an echo line?

**URL:** <https://community.unix.com/t/how-to-read-in-a-part-of-an-echo-line/319122>\
**Category:** Shell Programming and Scripting\
**Created:** [October 31, 2012, 7:05am UTC](https://community.unix.com/t/how-to-read-in-a-part-of-an-echo-line/319122 "2012-10-31T07:05:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grueben](https://community.unix.com/letter_avatar/grueben/32/5_5575768a8748004e209b776fc1b2916d.png) [@Grueben](https://community.unix.com/u/Grueben)\
**Post date:** [October 31, 2012, 7:05am UTC](https://community.unix.com/t/how-to-read-in-a-part-of-an-echo-line/319122/1 "2012-10-31T07:05:21Z")

</div>

Hello,  
I have a very basic script

```nohighlight
#!/usr/bin/ksh
      while
print ' '
print ' 1. View Command History '
print ' 2. List files in current Directory '
 read opt'?Enter Option> ' ;do
if [$opt = 1] ;then
      fc -l
      fi
#     
      if [$opt = 2] ;then
      ls -la

```

I want to add some more options to do a 'cd' command but without coding all the 'if' statements.  
Is there a way to list the options (all the directory names) and then cd to that directory based on the number supplied?

Thanks

Rob

---

<div class="post-metadata">

**Author:** ![pamu](https://community.unix.com/letter_avatar/pamu/32/5_5575768a8748004e209b776fc1b2916d.png) [@pamu](https://community.unix.com/u/pamu)\
**Post date:** [October 31, 2012, 7:30am UTC](https://community.unix.com/t/how-to-read-in-a-part-of-an-echo-line/319122/2 "2012-10-31T07:30:05Z")

</div>

> [@grueben](#):
>
> Is there a way to list the options (all the directory names) and then cd to that directory based on the number supplied?

What about using case..?

You can add multiple options..

sample code..

```plaintext
case "$1" in

1) echo "Sending 1 signal"    
    ;;
2) echo "Sending 2 signal"
    ;;
3) echo "Sending 3 signal"
    ;;
*) echo "Signal number $1 is not processed"
   ;;
esac

```

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/5_5575768a8748004e209b776fc1b2916d.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [October 31, 2012, 7:36am UTC](https://community.unix.com/t/how-to-read-in-a-part-of-an-echo-line/319122/3 "2012-10-31T07:36:03Z")

</div>

You also may want to have a look at [this link](http://docstore.mik.ua/orelly/unix3/korn/ch06_01.htm#IXT-6-287763)

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/5_5575768a8748004e209b776fc1b2916d.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [October 31, 2012, 7:40am UTC](https://community.unix.com/t/how-to-read-in-a-part-of-an-echo-line/319122/4 "2012-10-31T07:40:49Z")

</div>

Not sure if this works the same in ksh, but according to man page you might want to give it a shot:

```nohighlight
allfiles=$(ls)
select selfile in $allfiles
   do echo $selfile
   done

```

This will return the selected file/directory in the variable selfile, to which you then can `cd`

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/5_5575768a8748004e209b776fc1b2916d.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [October 31, 2012, 7:46am UTC](https://community.unix.com/t/how-to-read-in-a-part-of-an-echo-line/319122/5 "2012-10-31T07:46:01Z")

</div>

You can find some ksh use of "select" [here](http://docstore.mik.ua/orelly/unix3/korn/ch05_01.htm#IXT-5-287609)

---

<div class="post-metadata">

**Author:** ![Grueben](https://community.unix.com/letter_avatar/grueben/32/5_5575768a8748004e209b776fc1b2916d.png) [@Grueben](https://community.unix.com/u/Grueben)\
**Post date:** [October 31, 2012, 7:49am UTC](https://community.unix.com/t/how-to-read-in-a-part-of-an-echo-line/319122/6 "2012-10-31T07:49:13Z")

</div>

Thanks I'll have a look but I don't really understand it !

---

<div class="post-metadata">

**Author:** ![pamu](https://community.unix.com/letter_avatar/pamu/32/5_5575768a8748004e209b776fc1b2916d.png) [@pamu](https://community.unix.com/u/pamu)\
**Post date:** [October 31, 2012, 7:52am UTC](https://community.unix.com/t/how-to-read-in-a-part-of-an-echo-line/319122/7 "2012-10-31T07:52:54Z")

</div>

> [@grueben](#):
>
> Thanks I'll have a look but I don't really understand it !

Please look..

```plaintext
$ cat test.sh
case "$1" in

1) echo "Sending 1 signal"
    ;;
2) echo "Sending 2 signal"
    ;;
3) echo "Sending 3 signal"
    ;;
*) echo "Signal number $1 is not processed"
   ;;
esac

$ sh test.sh 2
Sending 2 signal

$ sh test.sh 1
Sending 1 signal

$ sh test.sh 5
Signal number 5 is not processed

```

As per your given option you can change what you want to do.  
just replace echo with your command.🙂

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/5_5575768a8748004e209b776fc1b2916d.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [October 31, 2012, 7:59am UTC](https://community.unix.com/t/how-to-read-in-a-part-of-an-echo-line/319122/8 "2012-10-31T07:59:04Z")

</div>

I think what we were trying to say was : first, you should focus on how to use a "case" statement, which is more appropriate to what you are trying to do and easy to read and maintain.

Then, if you want to investigate further, you should also train to use "getopts" as well as "select". (see the links previously provided)

---

<div class="post-metadata">

**Author:** ![Grueben](https://community.unix.com/letter_avatar/grueben/32/5_5575768a8748004e209b776fc1b2916d.png) [@Grueben](https://community.unix.com/u/Grueben)\
**Post date:** [October 31, 2012, 9:27am UTC](https://community.unix.com/t/how-to-read-in-a-part-of-an-echo-line/319122/9 "2012-10-31T09:27:10Z")

</div>

Thanks Pamu but it doesn't work for what I want as I can't pass the $1 until I've been shown the options.

---

<div class="post-metadata">

**Author:** ![pamu](https://community.unix.com/letter_avatar/pamu/32/5_5575768a8748004e209b776fc1b2916d.png) [@pamu](https://community.unix.com/u/pamu)\
**Post date:** [October 31, 2012, 9:32am UTC](https://community.unix.com/t/how-to-read-in-a-part-of-an-echo-line/319122/10 "2012-10-31T09:32:27Z")

</div>

> [@grueben](#):
>
> Thanks Pamu but it doesn't work for what I want as I can't pass the $1 until I've been shown the options.

So who is stopping you from doing this..?

You can do something like this.

```plaintext
echo "Options are"
echo "1 for --"
echo "2 for --"
read opt
#You can check is $opt is present or not
case "$opt" in

1) echo "Sending 1 signal"
    ;;
2) echo "Sending 2 signal"
    ;;
3) echo "Sending 3 signal"
    ;;
*) echo "Signal number $opt is not processed"
   ;;
esac

```

I hope this helps:)

pamu

---

<div class="post-metadata">

**Author:** ![cero](https://community.unix.com/letter_avatar/cero/32/5_5575768a8748004e209b776fc1b2916d.png) [@cero](https://community.unix.com/u/cero)\
**Post date:** [October 31, 2012, 10:16am UTC](https://community.unix.com/t/how-to-read-in-a-part-of-an-echo-line/319122/11 "2012-10-31T10:16:52Z")

</div>

Are you struggling with the logic for the menu driven cd Grueben? Maybe this code snipplet can help (tested in bash, but it should work with ksh too):

```nohighlight
#!/bin/bash

dir=""
backmsg="Back to menu"
PS3="Select directory to cd into: "

while ["$dir" != "$backmsg"]
do
   alldirs=$(ls -ap |grep /)
   select dir in $alldirs "$backmsg"
   do
      if ["$dir" != "$backmsg"]
      then
         cd $dir
         pwd
      fi
      break
   done
done

```
