Error to Read Input from command line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error to Read Input from command line
# 8  
Old 07-01-2014
So, is ${DATABASES set up as multiple items? You have a while true loop that you are trying to exit with a break and that's where it is getting confused.

Can you try a variation to this:-
Code:
DATABASES="DB1 DB2 DB3"            # Remember to quote these to allow for the spaces 
for DB in ${DATABASES}
do
  echo "Doing for database $DB"
  flag=invalid
  while [ "$flag" = "invalid" ]
  do
     read yn?"Do You Wish To Take Database Backup for $DB?? " 
     case "$yn" in
        [Yy]*) echo " YES take backup "
               flag=valid                              ;;
        [Nn]*) flag=valid                              ;;
            *) echo "Please Answer Yes OR NO "         ;;
     esac
  done
done

Does that improve things?


Robin
This User Gave Thanks to rbatte1 For This Post:
# 9  
Old 07-01-2014
Code:
 

for DB in ${DATABASES}
do
i=0
while [ $i == 0 ]
do
 doing some thing ... 
        
read yn?"Do You Wish To Take Database Backup for $DB?? " 
 case "$yn" in
 [Yy]*) echo " YES"  
               i=1        ;;
        [Nn]*) echo "NO"
               i=1          ;;
esac
done
done


OUTPUT

Code:
 
./Config.sh
Do You Wish To Take Database Backup for DB1?? y
YES 
 ****    Connected to DB1      ***
Do You Wish To Take Database Backup for DB2 ?? n
NO
 ****    Connected to DB2    ***


Thanks for your help Robin .It worked for me .Now i am trying to write anotehr inner case.. if I give YES input to case condition the inner case should take input for OFFLINE/FULL ONLINE/DELTA ONLINE .Any suggessions on this ?
# 10  
Old 07-01-2014
Perhaps consider breaking your code up and having a function like this:-
Code:
#!/bin/ksh
# My code does backups
#

Get_backup_type ()
{
 j=0
 while [ $j == 0 ]
 do
    read type?"What level of backup? (Offline/Full/Delta): "
    j=1
    case "$type" in   # Only first 3 characters are significant
       [Oo][Ff][Ff]*) echo "Performing Offline backup"      ;;
       [Oo][Nn][Ll]*) echo "Performing Full online backup"  ;;
       [Dd][Ee][Ll]*) echo "Performing Delta backup."       ;;
       *)             echo "Invalid backup type." ; j=0     ;;
    esac
 done
}

#########################
# Main code starts here #
#########################
export type                      # Make this a global variable

for DB in A B C
do
   i=0
   while [ $i == 0 ]
   do
      read yn?"Do You Wish To Take Database Backup for $DB? "
      i=1
      case "$yn" in
         [Yy]*) echo " YES"
         Get_backup_type
         echo "Running $type backup for $DB"   ;;
         [Nn]*) echo "NO"                      ;;
         *)     echo "Invalid choice" ; i=0    ;;
      esac
   done
done

Of course, you could go further and write functions for all the various parts and choices.

I got the following output for my simple testing:-
Code:
Do You Wish To Take Database Backup for A? n
NO
Do You Wish To Take Database Backup for B? y
 YES
What level of backup? (Offline/Full/Delta): off
Performing Offline backup
Running off backup for B
Do You Wish To Take Database Backup for C? y
 YES
What level of backup? (Offline/Full/Delta): del
Performing Delta backup.
Running del backup for C


Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Input for read command

Hello, I want to use the read command in order to input a keyword from the keyboard. I then want to use this keyword in a grep command. How to I input a phrase as a keyword? For example if I use read keyword "today is" and then use grep, I get "No such file or directory" error. (6 Replies)
Discussion started by: FelipeAd
6 Replies

2. Shell Programming and Scripting

Script to read command line input and change it to some form

Hi, I want to write a small code in which script changes command line input to some form. Example script.sh a1 a2 a3 a4 ..... output should be "a1|a2|a3|....." Number of inputs in command line can be any variable (2 Replies)
Discussion started by: Raza Ali
2 Replies

3. Shell Programming and Scripting

Read input file with in awk script not through command line

Hi All, Do we know how to read input file within awk script and send output toanother log file. All this needs to be in awk script, not in command line. I am running this awk through crontab. Cat my.awk #!/bin/awk -f function test(var){ some code} { } END { print"test code" } (5 Replies)
Discussion started by: random_thoughts
5 Replies

4. Shell Programming and Scripting

Print output and read input on same line

How do I print output and read input on the same line in ksh? echo Hello, what is your name? read name (1 Reply)
Discussion started by: robin_simple
1 Replies

5. Shell Programming and Scripting

Read input while executing the command

Hi everyone, i have made a very simple script where it reads the user input and converts the number from celcius to faranheit but instead of running the command and prompting the user for input I want to be able to simply enter a number at the end of the command to run the script. ex. instead of... (1 Reply)
Discussion started by: subway69
1 Replies

6. Shell Programming and Scripting

filtering input from read command

I need help understanding a script I'm modifying which someone else has written. Basically I’m looping through a buffer that holds records fetched from a database query. I need a way to separate the primary key values from other attributes in the result. Heres the code: BUFF=buffer_file >... (5 Replies)
Discussion started by: d3mon_spawn
5 Replies

7. Shell Programming and Scripting

sed to read line by line and input into another file

I have two files. Fileone contains text string one text string two text string three Filetwo contains Name: Address: Summary: Name: Address: Summary: Name: Address: Summary: I would like to use sed to read each line of file one and put it at the end of the summary line of file... (3 Replies)
Discussion started by: dolacap
3 Replies

8. UNIX for Dummies Questions & Answers

How to read a line of text from user input?

Hiii I wanna a read a line of text from standard input. The user enter data like this way name phone_no month1_salary month2_salary that is user enter the name ,phone no and salary of 2 months in a single line by giving spaces. I wanna add the 3rd and 4th fields ...ie add both... (4 Replies)
Discussion started by: krishnampkkm
4 Replies

9. Shell Programming and Scripting

How can I send the input of a read line command through a shell script

Hi All, I wish to automate the unix command 'su' through a shell script. I would like to pass the content of a file as password to 'su' command. My script is as below, #! /bin/sh su userA while read line do rpm -ivh $line done < pwd.txt where pwd.txt contains the password of... (6 Replies)
Discussion started by: little_wonder
6 Replies

10. Shell Programming and Scripting

read a file as input and pass each line to another script

Hi, I am trying to write a ftp script which will read a file for filenames and ftp those files to another server. Here's my ftp script, but it's scanning the current directory for file names. My question is how can I pass multiple files (these files will have the name of data files that need to... (0 Replies)
Discussion started by: sajjad02
0 Replies
Login or Register to Ask a Question