Sponsored Content
Top Forums Shell Programming and Scripting Problem reading terminal response string from Zsh Post 303010561 by rovf on Wednesday 3rd of January 2018 07:33:00 AM
Old 01-03-2018
Problem reading terminal response string from Zsh

Note: This posting is related to my posting at bash - Reading answer to control string sent to xterm - Stack Overflow , but I could get there a solution only for bash. I can use that solution, but for curiosity, I wonder, whether I could do this in Zsh as well.

The problem is to send a (Posix-) terminal query string to the terminal where the (interactive) shell is running, and to read the response from the terminal. For example, when we send Escape followed by the letter Z, the terminal is supposed to respond with the terminal ID. This response also starts with an Escape, followed by an arbitrary number of characters. Since I don't know the number of characters returned in advance, I have to accumulate them one by one.

This is the solution I achieved with bash, ask_tty.sh:

Code:
#!/bin/bash

str='' # Buffer for response
tty=$(tty)

# Send query string to terminal. Example: Esc Z queries for terminal id
echo -e '\e'${1:-Z}  >$tty

# Read response from terminal
while :
do
  read -rs -t 1 -n 1 <$tty
  if [[ -z $REPLY ]]
  then
    break
  fi
  str="${str}$REPLY"
done

# Output response without leading Esc
echo "${str#?}"

If I run in my terminal either ask_tty.sh Z or ask_tty.sh (because I made Z the default value), I get as response on stdout

[?63;1;2;4;6;9;15;22;29c

The actual reading of each character is done by read -rs -t 1 -n 1 <$tty.

I feel that for adapting this solution to Zsh, I have to change the bash read to an equivalent Zsh read, but I could not get it done: Either the command hangs, or it returns only the first character of the answer string. For instance, I tried read -rs -t -k and read -rs -t 1 -k 1.

How can I solve this in Zsh?

Update: I'm using Zsh 5.3 (given the evolution of Zsh, version might matter here), running on Cygwin.

Last edited by rovf; 01-03-2018 at 09:46 AM.. Reason: Providing additional information
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading response from server

I am trying to write a korn shell script which posts commands to a server and read the response back from the server. Any idea how I can read the servers response? I have tried doing the following: ( LOGIN:xxxxx command to server read ANSWER echo $ANSWER >file1... (4 Replies)
Discussion started by: frustrated1
4 Replies

2. UNIX for Advanced & Expert Users

problem while getting the response back..plz help

Hi ALL: I am not able to get the response back from weblogic in the shell script. The weblogic server in different account. I am able to login to that account and bring the server up but while doing a ping, the script is failing. While the same script is running fine if I run it on the account... (1 Reply)
Discussion started by: splax
1 Replies

3. Shell Programming and Scripting

Running a String as a command, zsh.

I have a shell script that is building a string that consists of the parts of a command that I want run at the end of the string. So it looks like this: $PART1=/path/to/command $PART2="-arg1" $PART3="-arg2" and so on. At the end of the command is a list of files I get from a loop and... (2 Replies)
Discussion started by: drnkhmlck
2 Replies

4. IP Networking

Apache mod_proxy +DNS slow response problem

My company has a private network, including a Apache web server (Linux) and some WinXP machines. The web server had been configured to use mod_proxy to connect to window update site via another company proxy server. It works for few years. Recently, some parties had setup a DNS server on the... (2 Replies)
Discussion started by: donaldfung
2 Replies

5. UNIX for Advanced & Expert Users

ssh error: Error reading response length from authentication socket

Hi - I am getting the error `Error reading response length from authentication socket' when I ssh from my cluster to another cluster, and then back to my cluster. It doesn't seem to affect anything, but it's just annoying that it always pops up and tends to confuse new users of the cluster. I... (1 Reply)
Discussion started by: cpp6f
1 Replies

6. Shell Programming and Scripting

Reading output from terminal back into bash script

How can I get a bash script to wait and read and count $i messages that a running program (drbl clonezilla) sends to the console (terminal) and only then move on to the next line in the script when the count is matched (the next line is the last line of the script and is a reboot)? The script... (0 Replies)
Discussion started by: dp123
0 Replies

7. UNIX for Advanced & Expert Users

Search for an exact string in a Terminal

Is there hopefully a way to search for an exact string in Man Pages? I know if I want to search for anything containing -c I can just do this. /-c How would I search for "-c"? I want only "-c" to show up. So I tried this. /"-c" It took me literally and looked for the quotes also. (13 Replies)
Discussion started by: cokedude
13 Replies

8. Solaris

slow response on solaris terminal

Solaris terminal responding very slow .. we have recently put a T3 hardware in to production , the applications running are it are perfectly and no complaints from user ..but when i ssh to the server ... the terminal response is very very slow .. it takes 3 seconds to show the character i type ..... (3 Replies)
Discussion started by: skamal4u
3 Replies

9. Shell Programming and Scripting

[Solved] Need Help in reading Response file

Hi All, I have a requirement to read response file which looks like below Ex: NAME=SAM DOB=01/01/1980 ADDRESS= 7658 James Street NewYork 0000 Now, I need to take NAME, DOB, ADDRESS into variables I am fine taking NAME and DOB I need help on how can I... (6 Replies)
Discussion started by: mallak
6 Replies

10. UNIX for Dummies Questions & Answers

sed Or Grep Problem OR Terminal Problem?

I don't know if you guys get this problem sometimes at Terminal but I had been having this problem since yesterday :( Maybe I overdid the Terminal. Even the codes that used to work doesn't work anymore. Here is what 's happening: * I wanted to remove lines containing digits so I used this... (25 Replies)
Discussion started by: Nexeu
25 Replies
LIBBASH(7)							  libbash Manual							LIBBASH(7)

NAME
libbash -- A bash shared libraries package. DESCRIPTION
libbash is a package that enables bash dynamic-like shared libraries. Actually its a tool for managing bash scripts whose functions you may want to load and use in scripts of your own. It contains a 'dynamic loader' for the shared libraries ( ldbash(1)), a configuration tool (ldbashconfig(8)), and some libraries. Using ldbash(1) you are able to load loadable bash libraries, such as getopts(1) and hashstash(1). A bash shared library that can be loaded using ldbash(1) must answer 4 requirments: 1. It must be installed in $LIBBASH_PREFIX/lib/bash (default is /usr/lib/bash). 2. It must contain a line that begins with '#EXPORT='. That line will contain (after the '=') a list of functions that the library exports. I.e. all the function that will be usable after loading that library will be listed in that line. 3. It must contain a line that begins with '#REQUIRE='. That line will contain (after the '=') a list of bash libraries that are required for our library. I.e. every bash library that is in use in our bash library must be listed there. 4. The library must be listed (For more information, see ldbashconfig(8)). Basic guidelines for writing library of your own: 1. Be aware, that your library will be actually sourced. So, basically, it should contain (i.e define) only functions. 2. Try to declare all variables intended for internal use as local. 3. Global variables and functions that are intended for internal use (i.e are not defined in '#EXPORT=') should begin with: __<library_name>_ For example, internal function myfoosort of hashstash library should be named as __hashstash_myfoosort This helps to avoid conflicts in global name space when using libraries that come from different vendors. 4. See html manual for full version of this guide. AUTHORS
Hai Zaar <haizaar@haizaar.com> Gil Ran <ril@ran4.net> SEE ALSO
ldbash(1), ldbashconfig(8), getopts(1), hashstash(1) colors(1) messages(1) urlcoding(1) locks(1) Linux Epoch Linux
All times are GMT -4. The time now is 08:36 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy