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:
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.
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:
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:
You use this device (the backticks) several times. Backticks are deprecated and only support for backwards compatibility. Instead of backticks use "$(..)":
Then you don't pay attention to quoting:
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:
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:
This uses the terminal's built-in properties for read timeouts:
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 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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)