Manipulating input into the shell "read" command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Manipulating input into the shell "read" command
# 1  
Old 09-14-2009
Manipulating input into the shell "read" command

Hi All

I have a migration program that creates directories based on dates, e.g 20090714 20090812 etc.. Based on their requirements, the user will select the directory they want to perform an action on.

Currently, this is a snippet of the code I use

Code:
no_of_versions=`ls | wc -l`
        if [ $no_of_versions -gt 1 ]; then
                export dirs=`ls -d [2]*`
                echo ""
                echo "Select Version:   "
                echo ""$dirs""
                read version
       fi

Currently the user has to type in the value of the date and this isn't the most elegant of ways. Consequently, I wanted to simplify this process and this is where I need help.

Is it possible to have a situation where I display the list of date directories along the lines of

(1) 20090714 (2) 20090812 (3) 20090901

They can then select either 1 or 20090714

I have no way of knowing how many dates there could be, 1 or 10 is possible.

I'm not even entirely sure this is something I can do in shell or even perl. All help as always appreciated
# 2  
Old 09-14-2009
You could use the select statement, available in ksh and bash, e.g.:
Code:
no_of_versions=$(ls -d [2]* | wc -l)
if [[ $no_of_versions -gt 1 ]]; then
  PS3="Select Version:   "
  select version in $(ls -d [2]*); do
  if [[ -f $version ]]; then
    break
  else
    echo please select one of the options above
  fi
  done
fi
echo $version

# 3  
Old 09-15-2009
Fantastic, Works a treat.. Much appreciated..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to avoid "Too many arguments" error, when passing a long String literal as input to a command?

Hi, I am using awk here. Inside an awk script, I have a variable which contains a very long XML data in string format (500kb). I want to pass this data (as argument) to curl command using system function. But getting Too many arguments error due to length of string data(payloadBlock). I... (4 Replies)
Discussion started by: cool.aquarian
4 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

4. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

5. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

6. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

7. Shell Programming and Scripting

read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell

Hallo, i need a Prompting read in my script: read -p "Enter your command: " command But i always get this Error: -p: is not an identifier When I run these in c-shell i get this error /usr/bin/read: read: bad option(s) How can I use a Prompt in the read command? (9 Replies)
Discussion started by: wiseguy
9 Replies

8. UNIX for Dummies Questions & Answers

Maximum input file size in "Diff" Command

Hello, Can anyone let me know what is the maximum file size that can be given as input for the "Diff" Command in Unix? I have a file size as large as 28MB and which can also increase. Will I face any issues with such a file size. If yes, What is the other alternative. Thanks in advance for... (1 Reply)
Discussion started by: Neeraja
1 Replies

9. Shell Programming and Scripting

Breaking input with "read" command

In this post, Perderabo's script says echo 05/06/25 14:15:56 | IFS=" /:" read Y1 M1 D1 h1 m1 s1 which, if I am not wrong, will break the input into Y1, M1 et al. I tried the following in my code #! /bin/ksh # per.sh typeset -R2 HOUR=00 typeset -R2 MIN=00 typeset -R2 SEC=00 ... (2 Replies)
Discussion started by: vino
2 Replies

10. Shell Programming and Scripting

Giving "read" from standard input a timeout.

I want to prompt a user for input but I want it to timeout after a specified time if no response is given. I tried the sleep command but this does not work. I am using ksh. Thanks. (10 Replies)
Discussion started by: rello
10 Replies
Login or Register to Ask a Question