Sponsored Content
Top Forums Shell Programming and Scripting Bash KeyPress (or Read Single Character) Post 302437177 by Ste_Moore01 on Wednesday 14th of July 2010 08:31:50 AM
Old 07-14-2010
Thanks Funksen, raw works for me too!

---------- Post updated at 01:31 PM ---------- Previous update was at 12:54 PM ----------

Sorry to bother you again.

When I tried this previously, I just had a standard read command.
So I would press the key and then press return and the command would be fired.

Now that have the below code instead, I can't seem to capture the keypresses correctly
Code:
	stty raw
	sel=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
	stty -raw

I used to have
Code:
read sel

case $sel in
		1)  KEY=1 ; NUM=$sel
	;;
		2)  KEY=2 ; NUM=$sel
	;;
		3)  KEY=3 ; NUM=$sel
	;;
		4)  KEY=4 ; NUM=$sel
	;;
		5)  KEY=5 ; NUM=$sel
	;;
		6)  KEY="" ; QUIT="Y" 
	;;
		["Q","q"]) KEY="" ; QUIT="Y" 
	;;
		$'\e[A' ) 
		if [ $NUM -eq 1 ]
		then
			NUM=6
		else
			NUM=$(($NUM - 1)) 
		fi
	;;
		$'\e[B' ) 
		if [ $NUM -eq 6 ]
		then
			NUM=1
		else
			NUM=$(($NUM + 1)) 
		fi
	;;
		"") KEY=$NUM 
	;;
		*) echo "ERROR"
	;;
	esac

This would determine whether the user was pressing up and down, q, a number or just pressing enter.

Now when I use the same code it isn't recognising the up, down or enter keypresses.

Have you any idea what I should change to get it to work.

Just to clarify, my code looks like the following.
Code:
	echo -n "Choose one: " 
	stty raw
	sel=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
	stty -raw
		
	case $sel in
		1)  KEY=1 ; NUM=$sel
	;;
		2)  KEY=2 ; NUM=$sel
	;;
		3)  KEY=3 ; NUM=$sel
	;;
		4)  KEY=4 ; NUM=$sel
	;;
		5)  KEY=5 ; NUM=$sel
	;;
		6)  KEY="" ; QUIT="Y" 
	;;
		["Q","q"]) KEY="" ; QUIT="Y" 
	;;
		$'\e[A' ) 
		if [ $NUM -eq 1 ]
		then
			NUM=6
		else
			NUM=$(($NUM - 1)) 
		fi
	;;
		$'\e[B' ) 
		if [ $NUM -eq 6 ]
		then
			NUM=1
		else
			NUM=$(($NUM + 1)) 
		fi
	;;
		"") KEY=$NUM 
	;;
		*) echo "ERROR"
	;;
	esac

Any suggestions would be much appreciated.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

read a variable character by character, substitute characters with something else

im having trouble doing this: i have a variable with 2 characters repeating e.g. aababbbaaaababaabbaabbba is there a way i can search the variable for a's and b's and then change a's to b's and b's to a's? im guessing its like getting the 1's compliment of the string im doing this in... (2 Replies)
Discussion started by: vipervenom25
2 Replies

2. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

3. Shell Programming and Scripting

Can I read a file character by character?

Hello all respected people, Can i read a file character by character without using sed,awk and perl commands. Thanks in advance. (4 Replies)
Discussion started by: murtaza
4 Replies

4. Shell Programming and Scripting

bash while read how to remove \n character

Hi, I've made a script to grep a file for i in `cat filename.txt` do strings ./binfile | grep "$i" 2>&1 > /dev/null done this works fine as long as in filename.txt i don't have any entries with spaces. But in my case i want to grep something with spaces like "lala tata" and... (3 Replies)
Discussion started by: papasj
3 Replies

5. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies

6. Shell Programming and Scripting

Replace multiple occurances of same character with a single character.

Hi all, Greetings, I have the following scenario, The contents of main file are like : Unix|||||forum|||||||||||||||is||||||the||best so||||||be|||||on||||||||||||||||||||||||||||||||||||||||||||it And i need the output in the following form: Unix=forum=is=the=best so=be=on=it ... (3 Replies)
Discussion started by: dipanchandra
3 Replies

7. Shell Programming and Scripting

Bash loop hording keypress input

I have a bash loop that waits for a single key press, then does $something depending on what $key is pressed before refreshing the screen with updated data. The problem I have is that the script will store additional key presses and chain them together causing the screen to redraw and the script... (1 Reply)
Discussion started by: DarkPhoenix
1 Replies

8. Shell Programming and Scripting

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 Replies

9. UNIX for Beginners Questions & Answers

Simulate keypress in bash

Hello everybody, I am using Windows 10 and cygwin/bash. I need to write a script in bash which simulates opening of a program and then press some keys such as F5, ENTER and ALT+F4. I have written a VBScript which does the job Set WshShell = WScript.CreateObject("WScript.Shell")... (9 Replies)
Discussion started by: supernono06
9 Replies

10. Shell Programming and Scripting

How to repeat a character in a field if it's a single character?

I have a csv dataset like this : C,rs18768 G,rs13785 GA,rs1065 G,rs1801279 T,rs9274407 A,rs730012 I'm thinking of use like awk, sed to covert the dataset to this format: (if it's two character, then keep the same) CC,rs18768 GG,rs13785 GA,rs1065 GG,rs1801279 TT,rs9274407... (7 Replies)
Discussion started by: nengcheng
7 Replies
nohup(1)						      General Commands Manual							  nohup(1)

NAME
nohup - Runs a utility ignoring hangups and quits SYNOPSIS
nohup utility [argument...] Note The C shell has a built-in version of the nohup command. If you are using the C shell, and want to guarantee that you are using the com- mand described here, you must specify the full path /usr/bin/nohup. See the csh(1) reference page for a description of the built-in com- mand. STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: nohup: XCU5.0 Refer to the standards(5) reference page for more information about industry standards and associated tags. OPTIONS
None OPERANDS
The name of a command to be invoked. Optional arguments required by the utility specified for invokation. DESCRIPTION
The nohup command runs utility, ignoring all hangups and QUIT signals. You can use this command to run programs in the background after logging off the system. To run a nohup command in the background, add an & (ampersand) to the end of the command. If nohup output is redirected to a terminal or is not redirected at all, the output is appended to the file nohup.out. If the file is cre- ated, the permission bits are set to Owner Read and Owner Write(600). If nohup.out is not writable in the current directory, the output is redirected to $HOME/nohup.out. If neither file can be created nor opened for appending, utility is not invoked. If standard error is a terminal, all output written to standard error is appended to the file receiving standard output. The nohup command accepts just one utility as an argument. To apply nohup to a pipeline or list of commands, enter the pipeline or list in a shell script file. Then run sh as utility using the following format: nohup sh -c file [Tru64 UNIX] You can also assign the shell file execute permission and run it as the utility in the form: nohup file NOTES
The term utility, rather than the term command, describes the argument to nohup because shell compound commands, pipelines, special built- in programs, and the like, cannot be handled directly. In addition, the term utility includes user application programs and shell scripts, not just the standard utilities. EXIT STATUS
The nohup command returns the following exit values: The specified utility was found, but could not be invoked. The error occurred in the nohup utility or the specified utility could not be found. The exit status is otherwise that of the utility. EXAMPLES
To leave a command running after logging out (sh only), enter: nohup find / -print & Shortly after you enter this, the following is displayed: 670 $ Sending output to nohup.out The process ID number changes to the background process started by the & (ampersand), in this case, 670. The $ (dollar sign) is the shell prompt. The message following informs you that the output from the find command is in the file nohup.out. Sending output... You can log out after you see these messages, even if the find command has not finished yet. If you are using csh, the built-in command nohup is executed instead of the system command. To execute the nohup system command, you must enter an absolute path. To do the same, but redirect the standard output to a different file, enter: nohup find / -print >filenames & This runs the find command and stores its output in a file named filenames. Now only the process ID and prompt are displayed. Wait for a second or two before logging out, because the nohup command takes a moment to start the command you specified. If you log out too quickly, utility may not run at all. Once utility has started, logging out does not affect it. To run more than one com- mand, use a shell script. For example, if you include the following shell script: comm -23 fi4 fi5 | comm -23 - fi6 | join -a1 - fi7 > comm.out and name it ncomm, you can run nohup for all of the commands in ncomm by entering: nohup sh ncomm If you assign ncomm Execute permission, you can obtain the same results by issuing the command: nohup ncomm To run this command in the background, enter: nohup ncomm & ENVIRONMENT VARIABLES
The following environment variables affect the execution of nohup: Provides a default value for the internationalization variables that are unset or null. If LANG is unset or null, the corresponding value from the default locale is used. If any of the internationalization vari- ables contain an invalid setting, the utility behaves as if none of the variables had been defined. If set to a non-empty string value, overrides the values of all the other internationalization variables. Determines the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multibyte characters in arguments). Determines the locale for the for- mat and contents of diagnostic messages written to standard error. Determines the location of message catalogues for the processing of LC_MESSAGES. Determines the search path that will be used to locate the command to be invoked. FILES
Standard output and standard error file for nohup. SEE ALSO
Commands: csh(1), ksh(1), nice(1), Bourne shell sh(1b), POSIX shell sh(1p) Functions: sigaction(2) Standards: standards(5) nohup(1)
All times are GMT -4. The time now is 05:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy