Scripting issue in stty -icanon


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scripting issue in stty -icanon
# 1  
Old 09-23-2012
Scripting issue in stty -icanon

Hi to all the members of the forum.

I have to write a shell script to identify all zero bytes files in the current directory and delete them. Before proceeding with deletion shell script should get a confirmation from the user. Note that for deletion rm -i should not be used
since it needs an enter key to be hit after supplying y or n.


here is solution 1 which works fine using a while loop.


Code:
for file in *
do
   if [ -f $file -a ! -s $file ]
   then
        echo "Delete $file y/n"
        ans=""
        stty -icanon min 0 time 0

        while [ -z "$ans" ]
        do 
        read $ans
        done
 
        if [ $ans = y -o $ans = Y ]
        then
            rm -f $file
            echo "$file deleted..."
        fi
        fi
done

stty sane

================

but what i need is ....i need to execute the same without using while loop.

for that i am using the below script but its not working as expected since it needs me to press an enter key



Code:
for file in *
do
   if [ -f $file -a ! -s $file ]
   then
        echo "Delete $file y/n"
        ans=""
        stty -icanon min 1 time 0

        read $ans
 
        if [ $ans = y -o $ans = Y ]
        then
            rm -f $file
            echo "$file deleted..."
        fi
        fi
done

=============
does anyone knows how to achieve the same thing without using while loop.


Any help will be appreciated.

Thanks in advance

Moderator's Comments:
Mod Comment edit by bakunin: Please view this code tag video for how to use code tags when posting code and data.

Last edited by bakunin; 09-23-2012 at 08:00 AM..
# 2  
Old 09-23-2012
You don't specify what shell you are using, if you are using bash try read -n 1 -p "Delete $file y/n? " ans
# 3  
Old 09-25-2012
The below script demonstrates how to generate a auto record advance(i.e. carriage retrun) when user makes their choice.....hth

Code:
#!/usr/bin/ksh
# Script: auto_rec_advance.sh
# Descr:  Example of auto record advance(i.e. carriage return) with simple menu

menu ()
{
  echo "-------------------------------------"
  echo " Main Menu "
  echo "-------------------------------------"
  echo "[1] Show Todays date/time"
  echo "[2] Show files in current directory"
  echo "[3] Show calendar"
  echo "[4] Start vi"
  echo "[5] Exit/Stop"
  echo "======================="
  echo "Enter your menu choice [1-5]: \c"
}


getc ()
{
  stty raw
  tmp=`dd bs=1 count=1 2>/dev/null`
  eval $1='$tmp'
  stty cooked
}


yourc ()
{  
case $yourch in
  1) echo ; echo ; echo "Today is `date`" ; echo ; echo ;;
  2) echo ; echo ; echo "Files in `pwd`" ; ls -ltr ; echo ; echo ;;
  3) echo ; echo ; cal ; echo ; echo ;;
  4) vi ; echo ; echo ;;
  5) exit 0 ;;
  *) echo ; echo ; echo "Opps!!! Please select choice 1,2,3,4, or 5" ; echo ; echo ;;
esac
}

while :
do
  menu
  getc yourch
  yourc
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Scripting issue

Hi Experts, I'm stuck with a single liner bash script. Need your help to fix it. Let me brief you that what is my expectations from this script. I need the interfaces configured for a list of servers, their respective IP address and FQDN in a output file. The output should be in the same... (3 Replies)
Discussion started by: arun_adm
3 Replies

2. Shell Programming and Scripting

Scripting Issue

Hi All, I am facing a problem when i am trying to run shell script more than 5 times. I have shell script(.sh file) which ran perfectly fine in the early attempts(1,2,3,4 runs). But if i try to run the script more number of times, i am facing the below error message. Too many ('s I do... (3 Replies)
Discussion started by: am24
3 Replies

3. Shell Programming and Scripting

Scripting issue

I am having a problem. which I have described below>> I have to run a script with the format : <File_name><Start_date><End_date> abcd.sh 19-JAN-2015 01-May-2014 problem is I need to compare these two dates and throw an error as start date must be less than or equal to end date. But... (13 Replies)
Discussion started by: Chandan_Bose
13 Replies

4. UNIX for Dummies Questions & Answers

Scripting issue

Hi experts... I am really new to this scripting, i really need your help. I have this script to read and print the report, however the script does not pickup the correct hostname (eg. Svtns11d).. The script will start reading/pick up on the second line instead of reading the first line.... (1 Reply)
Discussion started by: talkhard154
1 Replies

5. Shell Programming and Scripting

Scripting Issue

needing this script to shut down 1 IceS and start up another, close the 2nd one after 12 seconds and then reboot. here is what i have so far #!/bin/bash ShutDown() { echo "Shutdown in progress." wall <<ENDOFWALL CI Shutdown has been intiated!!!! Shutdown will occur in 30 seconds...... (1 Reply)
Discussion started by: Zaine
1 Replies

6. Shell Programming and Scripting

Scripting Issue

I am having an issue with a script that I created today, my first attempt at this, and was wondering if anyone can give me insight to what changes need to be made. Below is a copy of the script that I have written. WEe are trying to monitor whether or not a services is running. I do have a cron... (1 Reply)
Discussion started by: lsudubee
1 Replies

7. Shell Programming and Scripting

Scripting issue

Hello, I have an ASCII file (many files of the same format, but different dates and numbers) in the format like below: 2008.01.02,08:00,1.46520,1.46520,1.46410,1.46440,70 2008.01.02,08:05,1.46450,1.46560,1.46440,1.46540,79 2008.01.02,08:10,1.46530,1.46540,1.46490,1.46500,46... (8 Replies)
Discussion started by: chief2000
8 Replies

8. Shell Programming and Scripting

scripting issue

Hello, Guys I am having a sql script file which contains some sql statements including inserting values, One column is of the data type date. Now i am having a KSH script for inserting values via this script into the database. The problem I am facing that when I am inserting value in the... (1 Reply)
Discussion started by: mraghunandanan
1 Replies

9. Shell Programming and Scripting

Scripting issue..

Hi guys... I am newbie to Shell Scripting... I am querying the Oracle database.I want a Shell script for fetching some data from datasets in Oracle database and displaying in an excel sheet.This is my requirement.. Can u plz help me guys.. Regards, Mahesh... (4 Replies)
Discussion started by: mraghunandanan
4 Replies

10. Shell Programming and Scripting

Scripting issue..

Hi guys... I am a newbie to scripting... I have a small requirement... I dont whether u r clear with my requirement... But plz try to help me... Like, tell me some kind of scripts that can help me in retreiving the data from the datasets. Regards, Mahesh... (1 Reply)
Discussion started by: mraghunandanan
1 Replies
Login or Register to Ask a Question