How do I store stdout in a variable?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I store stdout in a variable?
# 1  
Old 12-11-2013
How do I store stdout in a variable?

These seems ridiculously simple but I can't get it to work. Using korn shell and I want to pass in a flag to tell my echo statements to either write to the screen for debugging or a file if not. So I have something like:

if [ $testmode = TRUE ]; then

Then later on:

echo messages>$logout

But I can't get it to write to the screen if testmode is TRUE. Tried logout=1,logout=stdout, pretty much every combination of escape/quote/backslashes around the 1 or &1 and nothing works.

What do I need to do?


# 2  
Old 12-11-2013
From the title I thought you wanted: x=`cat`
but if you are lucky, your UNIX has /dev/stdout, so you can say logout=/dev/stdout and then >$logout does nothing but waste a few cycles (open() that string as a fd, close() fd 0, dup() the new fd (defaults to 0) and close() the new fd).

Even on UNIXes where there is no /dev/fd/1 or /proc/self/fd/1 = /dev/stdout, there is almost always /dev/tty = your screen (unless the script is running from cron, at, a web server, or ssh without -t or -tt, and so has no controlling terminal). However, once you go to /dev/tty, it is hard to redirect it back into a file (think calling it from expect or ssh to get a pseudo terminal that flows to some other process's stdout).
# 3  
Old 12-11-2013
logout=/dev/stdout did the trick. That's what I was looking for.

Thanks for your help.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Store result variable

Friends have the following problem: cat $PATH_DAT/mr.txt | nawk 'BEGIN { CantPnt=0; NumReg=0; FS="|" } { NumReg++ CantPnt=CantPnt+int($2) } END{ printf... (5 Replies)
Discussion started by: tricampeon81
5 Replies

2. Shell Programming and Scripting

How to store file name in a variable?

Hi All, Daily I am generating a file dfm_daily_file_ ex: dfm_daily_file_05072015 date will be changed daily. Once the file is FTP it is deleted. I have tried the below code to get the file name with any date and store it in a variable its not working. #!/bin/ksh ... (4 Replies)
Discussion started by: ROCK_PLSQL
4 Replies

3. Shell Programming and Scripting

I can't store value in the variable

!#bin/bash clear var= grep email.txt | wc -l echo $var echo $var exit 0 OUTPUT: 1000 _ _ Where _ represent space (no value or nothing) (4 Replies)
Discussion started by: Muhammad Rehan
4 Replies

4. Programming

how to store string in variable

sorry i'm newbies c programer how to store string to variable with value flexible. example int hh=1; ---> value flexible 1,2,3,4,5; int xx=1; ---> value flexible 1,2,3,4,5; char test="value=%d and value=%d",hh,xx; --> not working char test2="value2=%d and value2=%d",hh,xx; --> not... (1 Reply)
Discussion started by: slackman
1 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. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

7. UNIX for Dummies Questions & Answers

How to Store command O/P to a variable...

Hi all.. I got a problem.. Its easy to redirect o/p to a file.. But Is it possible to redirect the O/P to a variable? For example: I've a command in my script: string1=cut -d ':' -f2 file.txt When I do: echo $string1 The value is empty... Pls suggest me how to store the value... (7 Replies)
Discussion started by: smartbuddy
7 Replies

8. Shell Programming and Scripting

To store the output in a variable

Hi, I am getting the following error while executing the script. Please can someone throw some light where is the problem. Many thanks. ./check: temp: not found The directory related to SEP instance 4 does not exist. The script is as follows. SEP_APP="/scp/sepx/app... (2 Replies)
Discussion started by: Sudhakar333
2 Replies

9. Programming

stdout to a variable?

Hi, I need to set stdout to go to a String variable, has anyone got any ideas? (6 Replies)
Discussion started by: cb.mark
6 Replies

10. Shell Programming and Scripting

ksh - get stdout name as variable

Writing a ksh script. If someone starts a process with: test.ksh > date.log How can I grab 'date.log' name as a variable in test.ksh? I need to get the 'date.log' name (not the contents) as a variable...without entering something like 'test.ksh date.log > date.log' (4 Replies)
Discussion started by: mhcueball2
4 Replies
Login or Register to Ask a Question