Error to Read Input from command line

Team ,
I am trying to write a case condition for database backups.But I am unable to make the script to read input from command line .

 
 
while true ;do
 read -p "Do You Wish To Take Database Backup ?? " yn
  case $yn in
 [Yy]*) echo " YES take backup ";;
        [Nn]*) echo " NO BACKUP " ;;
         *) echo "Please Answer Yes OR NO " ;;
esac
done

I am getting below error after executing .

 
read: 0403-039 No query process for pipe.

Why this error pop up ?

You are probably executing it with the wrong shell. Are you using ksh93 ? In ksh93 -p is used to read output from a coprocess, whereas bash uses the -p option for a prompt.

echo $SHELL

should give you then name of your shell.

What shell are you in? For me, I'm usually Korn shell, ksh so I would craft is like this:-

while true ;do
 read yn?"Do You Wish To Take Database Backup ?? "
 case $yn in
   [Yy]*) echo " YES take backup "        ;;
   [Nn]*) echo " NO BACKUP "              ;;
       *) echo "Please Answer Yes OR NO " ;;
 esac
done

I hope that this helps,

Robin

Thanks for your replies this is ksh .Batte I used your sytax .

 
while true ;do
        
read  yn? "Do You Wish To Take Database Backup for $DB?? " 
 case $yn in
 [Yy]*) echo " YES take backup ";;
        [Nn]*) echo " NO BACKUP " ;;
         *) echo "Please Answer Yes OR NO " ;;
esac
done

I don't see its running fine .I can see more errors though its taking inputs from command line .May be i should add break in each choice to come out of the loop ?

 
./Config.sh[56]: Do You Wish To Take Database Backup for ?? : This is not an identifier.
Please Answer Yes OR NO
y
./Config.sh[56]: Do You Wish To Take Database Backup ?? : This is not an identifier.
 YES take backup
./Config.sh[56]: Do You Wish To Take Database Backup ??  : This is not an identifier.
Please Answer Yes OR NO
n
./Config.sh[56]: Do You Wish To Take Database Backup ??   : This is not an identifier.
 NO BACKUP
./Config.sh[56]: Do You Wish To Take Database Backup ??  : This is not an identifier.
Please Answer Yes OR NO
cancel
./Config.sh[56]: Do You Wish To Take Database Backup for ?? : This is not an identifier.
Please Answer Yes OR NO

You have a space in your read command between the yn? and the quoted prompt. Take this out. I get the following output to the code as posted:-

Do You Wish To Take Database Backup ?? y
 YES take backup 
Do You Wish To Take Database Backup ?? n
 NO BACKUP 
Do You Wish To Take Database Backup ?? rubbish 
Please Answer Yes OR NO 
Do You Wish To Take Database Backup ?? 

... and then I cancelled with Cntl-C

Perhaps also use double quotes in the case statement like this to cater for someone just pressing ENTER:-

case "$yn" in

Robin

Just trying to add the case condition to a for loop where for have n number iterations.but after giving choice as N its completly coming out of for loop
not iterating for other DB values in for loop .am i missing something ? Before adding CASE conditition for loop was iterating for n number values

 
for DB in ${DATABASES}
do
 doing some thing for n number of dbs... 
while true ;do        
read yn?"Do You Wish To Take Database Backup for $DB?? " 
 case "$yn" in
 [Yy]*) echo " YES take backup ";
        break ;;
        [Nn]*) exit;;
         *)    echo "Please Answer Yes OR NO " ;;
esac
done
done

Out is below

 
Do You Wish To Take Database Backup for ? n
 

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:-

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

1 Like
 

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

 
./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 ?

Perhaps consider breaking your code up and having a function like this:-

#!/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:-

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