Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Need help with repeating variables in a shell script Post 302760697 by ricco19 on Thursday 24th of January 2013 12:17:11 PM
Old 01-24-2013
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.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script Variables

HI guys I need to store the output of a sql query in a variable, can you tell me how to do that eg) select count(*) from s_escl_req $count = count(*) from s_escl_req how would i store the count(*) from the sql statement in a variable called $count. thanks (3 Replies)
Discussion started by: ragha81
3 Replies

2. Shell Programming and Scripting

Repeating variables in the code

Hi all, I had written 3 KSH scripts for different functionalities. In all these 3 files there are some 30 variables in common. So I want to reduce the code by placing these variables in a common properties file named (dataload.prop/dataload.parms/dataload.txt) or txt file and access it... (1 Reply)
Discussion started by: mahalakshmi
1 Replies

3. Shell Programming and Scripting

Accessing variables of one shell script in another shell script

I have a variable $exe in a shell script file a.sh which I need to access in another shell script file b.sh. How can I do that? :rolleyes: Thanks!! (2 Replies)
Discussion started by: looza
2 Replies

4. Shell Programming and Scripting

Accessing variables of one shell script in another shell script

Hi All, I have a shell script called sample1.sh where I have 2 variables. Now I have another shell script called sample2.sh. I want the variables in sample1.sh to be available to sample2.sh. For example. In sample1.sh I am finding the sum of 2 numbers namely a and b. Now I want to access... (2 Replies)
Discussion started by: rsendhilmani
2 Replies

5. Shell Programming and Scripting

Variables in shell script

mysqldump --compact --add-drop-table -h192.168.150.80 -uroot -p somePass $combined | sed '/$combined/$table/g' | mysql $databaseThe sed part is not working from the above statement. The variables combined and table are already defined and instead of showing the actual variable, it is executing the... (4 Replies)
Discussion started by: shantanuo
4 Replies

6. Shell Programming and Scripting

Shell script to extract data in repeating tags from xml

Hi, I am new to shell scripting. I need to extract data between repeating tags from an xml file and store the data in an array to process it further. <ns1:root xmlns:ns1="http://example.com/config"> <ns1:interface>in1</ns1:interface> <ns1:operation attribute1="true" attribute2="abd"... (2 Replies)
Discussion started by: sailendra
2 Replies

7. UNIX for Dummies Questions & Answers

Adding variables to repeating strings

Hello, I want to add a letter to the end of a string if it repeats in a column. so if I have a file like this: DOG001 DOG0023 DOG004 DOG001 DOG0023 DOG001 the output should look like this: DOG001-a DOG0023-a DOG004 DOG001-b (15 Replies)
Discussion started by: verse123
15 Replies

8. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

9. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

10. Shell Programming and Scripting

Shell script to create runtime variables based on the number of parameters passed in the script

Hi All, I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them abc.sh ---------- export Numbr_Parms=$# export a=1 while do export... (3 Replies)
Discussion started by: dev.devil.1983
3 Replies
stty(2) 							System Calls Manual							   stty(2)

NAME
stty(), gtty() - control terminal device (Bell Version 6 compatibility) SYNOPSIS
Remarks These system calls are preserved for backward compatibility with Bell Version 6. They provide as close an approximation as possible to the old Version 6 functions. All new code should use the and calls described in termio(7). DESCRIPTION
For certain status settings and status inquiries about terminal devices, the functions and are equivalent to and respectively (see ioctl(2) and termio(7). RETURN VALUE
and return the following values: Successful completion. Failure. is set to indicate the error. ERRORS
If or fails, is set to one of the following values: [EBADF] is not a valid file descriptor. [EFAULT] points to an invalid address. WARNINGS
Obsolescent Interfaces and are to be obsoleted at a future date. SEE ALSO
stty(1), exec(2), ioctl(2), sttyV6(7), termio(7), tty(7). TO BE OBSOLETED stty(2)
All times are GMT -4. The time now is 05:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy