Need help with repeating variables in a shell script | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Need help with repeating variables in a shell script

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-24-2013
ricco19 ricco19 is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 24 January 2013, 4:05 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Need help with repeating variables in a shell script

I should preface this by saying I have never worked with shell scripts before so this is all new to me. I was able to make something that worked, but is terribly optimized, and I have no idea how to improve it. If anything it's a pretty hilarious script:


Code:
   #/bin/bash
    
    get_char() {
    	old_tty_settings=`stty -g`
    	stty -icanon min 0 time 1
    	stty cbreak
    	grabbed_char=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
    	stty -cbreak
    	stty "$old_tty_settings"
    }
    
    while true; do
    
    	unset char00
    	unset char01
    	unset char02
    	unset char03
    	unset char04
    	unset char05
    
    	echo -e "\nWaiting for keystroke..."
    	read -n 1 char00
    
    	while true; do
    
    		get_char; char01=$grabbed_char
    
    		if [ "$char01" != "" ]; then get_char; char02=$grabbed_char
    			else break
    		fi
    		if [ "$char02" != "" ]; then get_char; char03=$grabbed_char
    			else break
    		fi
    		if [ "$char03" != "" ]; then get_char; char04=$grabbed_char
    			else break
    		fi
    		if [ "$char04" != "" ]; then get_char; char05=$grabbed_char
    			else break
    		fi
    
    	done
    
    	fullstring=$char00$char01$char02$char03$char04$char05
    	echo -e "\nFULLSTRING:	!$fullstring!"
    
    done


The list is a lot longer, but I spared you.

Basically, the program needs to sit in terminal forever waiting for a keystroke. After an initial keystroke, it needs to wait a very short amount of time (1/10th of a second in this case) for another keystroke. If no keystroke is registered in that short time, it echos the output (which will be piped with sed commands), and restarts.

It is meant to be used with a barcode scanner, where you may scan 4 characters, or 100 characters. We want to process the data as quickly as possible, meaning we don't want a 1 second delay after something is scanned.

A good solution would be if "charXX" could be created as a character was received. Thanks for any help.
Sponsored Links
    #2  
Old 01-24-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 November 2014, 12:55 PM EST
Location: Saskatchewan
Posts: 19,921
Thanks: 845
Thanked 3,416 Times in 3,201 Posts
Quote:
Originally Posted by ricco19 View Post
Basically, the program needs to sit in terminal forever waiting for a keystroke. After an initial keystroke, it needs to wait a very short amount of time (1/10th of a second in this case) for another keystroke. If no keystroke is registered in that short time, it echos the output (which will be piped with sed commands), and restarts.
Hmmm.

I think a terminal device can do that all by itself without bash's help, actually! From man termios:


Code:
       In non-canonical mode input is available immediately (without the  user
       having  to  type  a line-delimiter character), and line editing is dis-
       abled.  The settings of MIN (c_cc[VMIN]) and TIME (c_cc[VTIME])  deter-
       mine  the  circumstances  in  which a read(2) completes; there are four
       distinct cases:

       * MIN == 0; TIME == 0: If data is available,  read(2)  returns  immedi-
         ately,  with the lesser of the number of bytes available, or the num-
         ber of bytes requested.  If no data is available, read(2) returns 0.

       * MIN > 0; TIME == 0: read(2) blocks until the lesser of MIN  bytes  or
         the  number  of bytes requested are available, and returns the lesser
         of these two values.

       * MIN == 0; TIME > 0: TIME specifies the limit for a timer in tenths of
         a  second.   The  timer  is  started when read(2) is called.  read(2)
         returns either when at least one byte of data is available,  or  when
         the  timer  expires.  If the timer expires without any input becoming
         available, read(2) returns 0.

       * MIN > 0; TIME > 0: TIME specifies the limit for a timer in tenths  of
         a second.  Once an initial byte of input becomes available, the timer
         is restarted after each further byte is  received.   read(2)  returns
         either  when  the lesser of the number of bytes requested or MIN byte
         have been read, or when the inter-byte timeout expires.  Because  the
         timer  is  only  started after the initial byte becomes available, at
         least one byte will be read.

I'll see if I can build something quick.
Sponsored Links
    #3  
Old 01-24-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 24 November 2014, 1:34 PM EST
Location: In the leftmost byte of /dev/kmem
Posts: 4,337
Thanks: 45
Thanked 839 Times in 662 Posts
There are a few problems with your script, conceptually and otherwise. The bottom line - to spare you the effort of IMHO optimizing a hopeless case - is that this problem is IMHO suited to a high-level language (C, whatever - pick any).

First, the shells input function is based on the terminal handling of Unix and therefore not suited to the way you want to treat the input: basically a Unix terminal is a file, where you can read from (input) and write to (output). What's more Unix was historically designed as a multi-user system. One system at the center and a number (typically a dozen or two) terminals connected to it. The Unix terminal deals with lines of data: a "read" statement will take any amount of keystrokes but only process them once the user hits <ENTER>. This is so because this way the central system will have to pay only very limited attention to the terminal until the user does formally end his input. The system is similar to how IBM mainframes deal with their terminals and the reason why they can handle vast amounts of sessions at the same time with comparatively limited resources. Would the system have to pay attention to every keystroke it would do nothing else than paying attention as the number of sessions increases.

What you now want is to indeed pay attention to every keystroke. The shell is - because of historical reasons given above - not designed to do so.

Apart from the conceptual problem there are only very minor problems which usually happen to beginners. In fact you have done astonishingly well for a beginner:


Code:
old_tty_settings=`stty -g`

You use this device (the backticks) several times. Backticks are deprecated and only support for backwards compatibility. Instead of backticks use "$(..)":


Code:
old_tty_settings="$(stty -g)"

Then you don't pay attention to quoting:


Code:
fullstring=$char00$char01$char02$char03$char04$char05

This is not a high-level-language. The shell interpretes this line by replacing the variables with their content in a first pass, then executing what results in another pass (there are actually several passes, but these two matter in this case). To protect your variables content from being interpreted by the shell you should always quote them:


Code:
fullstring="$char00$char01$char02$char03$char04$char05"

For the same reason i quoted to subshell call with which i replaced the backticks before. This way you are always on the safe side. Only omit the quotes for the expressed purpose of having the string interpreted by the shell.

Last is you don't pay attention to the variable scope. Where is "grabbed_char" belonging to? Even if the shell allows you to introduce variables on the fly you should work like it doesn't, simply because this way you get more orer in your source and have less maintenance effort in the future.

You should define a variable (use "typeset") in the main program and then pass the output via <stdin>. The following sketch shows the mechanism:


Code:
#! /bin/bash

sub_function ()
{
     typeset argument="$1"
     echo "a $argument"
     return 0
}

# main ()
typeset string1=""
typeset string2=""

string1=$(sub_function "foo")"; echo "$string1"
string2=$(sub_function "bar")"; echo "$string2"

exit 0

I hope this helps.

bakunin
    #4  
Old 01-24-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 November 2014, 12:55 PM EST
Location: Saskatchewan
Posts: 19,921
Thanks: 845
Thanked 3,416 Times in 3,201 Posts
This uses the terminal's built-in properties for read timeouts:

Code:
#!/bin/sh

old_tty_settings=$(stty -g)
# This restores the terminal settings even if you quit with ctrl-C
trap "stty $old_tty_settings" EXIT

stty -echo -icanon min 1 time 1

STRING=$(dd if=/dev/tty count=1 2>/dev/null)

echo "Read $STRING"

The terminal waits for multiple keystrokes yet times out quickly, and no loop is required. I can't type fast enough to get more than one letter in, but if I paste something to the terminal it gets the whole thing.

You still have to use dd, not the read builtin, because read messes with the terminal device too, undoing all the fancy settings you've been trying to arrange.
Sponsored Links
    #5  
Old 01-24-2013
ricco19 ricco19 is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 24 January 2013, 4:05 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
This uses the terminal's built-in properties for read timeouts:

Code:
#!/bin/sh

old_tty_settings=$(stty -g)
# This restores the terminal settings even if you quit with ctrl-C
trap "stty $old_tty_settings" EXIT

stty -echo -icanon min 1 time 1

STRING=$(dd if=/dev/tty count=1 2>/dev/null)

echo "Read $STRING"

The terminal waits for multiple keystrokes yet times out quickly, and no loop is required. I can't type fast enough to get more than one letter in, but if I paste something to the terminal it gets the whole thing.

You still have to use dd, not the read builtin, because read messes with the terminal device too, undoing all the fancy settings you've been trying to arrange.
This is a good solution and it works well. Thanks.

Yeah we knew we could make a working c program, but just using a script makes it a simpler process.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script to extract data in repeating tags from xml sailendra Shell Programming and Scripting 2 09-10-2012 08:32 AM
Accessing variables of one shell script in another shell script rsendhilmani Shell Programming and Scripting 2 03-17-2009 02:17 AM
Accessing variables of one shell script in another shell script looza Shell Programming and Scripting 2 06-30-2008 08:13 PM
Repeating variables in the code mahalakshmi Shell Programming and Scripting 1 02-08-2007 07:33 AM
Shell Script Variables ragha81 Shell Programming and Scripting 3 08-22-2006 04:43 AM



All times are GMT -4. The time now is 04:04 PM.