Redirecting terminal to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirecting terminal to variable
# 1  
Old 05-05-2018
Redirecting terminal to variable

when i do something like this:

Code:
bona=$(echo hi2 > /dev/pts/1 ; printf '%s\n' "" | sed '/^$/d')

i get:

Code:
hi2

and the $bona variable is empty, when I run:

Code:
echo ${bona}

i get the result "hi2" outside of the variable. I want it stored in the bona variable with nothing outputted to the screen until i echo out the bona variable. but the problem is that the bona variable is always empty.

how can i keep the hi2 in the bona variable so when i echo it out, it shows hi2?

i have tried the following, but to no avail:

Code:
bona=$(echo hi2 > /dev/pts/1 >&1; printf '%s\n' "" | sed '/^$/d')

fyi, /dev/pts/1 is my active terminal that im working out of. it needs to be specified.
# 2  
Old 05-05-2018
I really have no idea what you are trying to achieve.
Code:
printf '%s\n' "" | sed '/^$/d'

is of course always empty and therefore bona will always be empty.

Quote:
Originally Posted by SkySmart
[..]
how can i keep the hi2 in the bona variable so when i echo it out, it shows hi2?[..]
Try:
Code:
bona=hi2
echo "$bona"


--
Perhaps you mean something like this?
Code:
bona=$(echo hi2 | tee /dev/tty)


Last edited by Scrutinizer; 05-05-2018 at 01:19 PM..
# 3  
Old 05-05-2018
tee to terminal might not be the solution as s/he wants "it stored in the bona variable with nothing outputted to the screen".

What does "i get the result "hi2" outside of the variable" mean?
This User Gave Thanks to RudiC For This Post:
# 4  
Old 05-05-2018
I run a couple of my scripts through pipes and i need to know the terminal from which a script is running.

commands like:

Code:
who am i | sh

does not produce the expected terminal address...i.e. /dev/pts/1

So, im looking for a way to identify the most recently active terminal by sending data to it.

i feel this has to be doable.
# 5  
Old 05-05-2018
Code:
who am i | sh

Means execute the output of who am i in new shell as if it were a command, so the result is going to be an error.
# 6  
Old 05-06-2018
Would sth. like
Code:
lsof -p$$ | awk '/ 1u/ {print $9}'
/dev/pts/2

or
Code:
ps | awk -vSELF=$$ 'match ($4, SELF) {print $13}'
pts/2

help?
This User Gave Thanks to RudiC For This Post:
# 7  
Old 05-06-2018
Quote:
Originally Posted by SkySmart
fyi, /dev/pts/1 is my active terminal that im working out of. it needs to be specified.
Usually /dev/tty would be interchangeable with the device of the current terminal, have you given this a go?

or add /dev/ to the front of who am i output:
Code:
$ who am i | awk '{print "/dev/"$2}'
/dev/pts/4


Last edited by Chubler_XL; 05-06-2018 at 11:53 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expect Redirecting o/p to an Variable

I have a password reset expect script which stores all the op to an file. I need to check the whether password is successfully changed by greping out the file and storing the o/p to a variable. But we try to print the variable , its shows only the command instead of its o/p. ... (2 Replies)
Discussion started by: sudharson
2 Replies

2. Shell Programming and Scripting

Redirecting script output to terminal

When ever i started my terminal,Every time I have to change the directory like "cd user/documents/ravi/folder2/folder3" Without typing this entire command every time ,I placed "alias c='cd user/documents/ravi/folder2/folder3'" in .bash_profile file. so that i can able to execute command 'c'... (6 Replies)
Discussion started by: Raviteja saddal
6 Replies

3. Slackware

X terminal: Redirecting remote sound to my local audio device

Hello everybody, I'm testing some aspects of X Terminal implementation and it's going great. I can use remote applications on my local slow workstation at remote's processor speed by redirecting the remote DISPLAY variable to "my_local_ip:0.0"; but i'm having troubles to get remote audio and... (2 Replies)
Discussion started by: semash!
2 Replies

4. Shell Programming and Scripting

redirecting the terminal to file

Hi, I want to save the whole Output of the terminal in a file. I dont want to redirect a single command to a file (ls -l > test.txt), I want to redirect the whole last 40 lines into a file. Maybe i can read out the terminal while working with it, but i cant find a way to save the whole... (2 Replies)
Discussion started by: niratschi
2 Replies

5. Shell Programming and Scripting

Redirecting stdout to variable while printing it

Hi everybody, I am trying to do the thing you see in the title, and I can't simply do a=$(svn up) echo $a because the program (svn) gives output on lots of lines and in the variable the output is stored on only one line (resulting in a horribly formatted text). Any tips? Thanks,... (2 Replies)
Discussion started by: ocirne94
2 Replies

6. Programming

Redirecting Terminal to Local Application!

i wanted to execute some terminal commands on local linux, parse their output and display it to the user, i checked netcat source code but i couldnt understance it since im new to c (and linux at the same time). so i was wondering if there is away to run an instance of terminal hidden, read and... (15 Replies)
Discussion started by: JonhyM
15 Replies

7. Shell Programming and Scripting

redirecting values from one variable to another in a loop.

The situation is like this: I am reading records from a file, depending upon some condition extracting fields from the file into different variables in a loop one by one. I need to print all the variable in line, so I am trying to redirect hose variables one by one to a variable called final_value... (1 Reply)
Discussion started by: mady135
1 Replies

8. Shell Programming and Scripting

problem redirecting output of command to variable

Hi. I'm a newbie in scripting and i have this problem: i want to use the 'fuser' command on a file to tell if it's being accessed (for my purposes: still being written). I want to save the output of the command and later compare with the 'not being used' result. the script: #!/bin/bash... (2 Replies)
Discussion started by: nunovc
2 Replies

9. Shell Programming and Scripting

redirecting variable content to a file!

Hello! I'm having problems trying to extract the contents of a variable and placing it into a text file. Grateful for any help. Been trying something along the lines of: $variable > file.txt or `cat < $variable` > file.txt As you can see I'm a newbie to this :D (2 Replies)
Discussion started by: lloowen
2 Replies

10. UNIX for Dummies Questions & Answers

redirecting one terminal into an other??

Hi guys; I want to show what am I doing on a terminal into another. I did something close but its not working really good. Example: cat /dev/pts/12 >/dev/pts/13 where 12 is my terminal and 13 its the other terminal. This is usefull for me to share my small unix knowledge to other people... (4 Replies)
Discussion started by: piltrafa
4 Replies
Login or Register to Ask a Question