LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Command Line Output (https://www.linuxquestions.org/questions/linux-newbie-8/command-line-output-4175502971/)

mp85 04-25-2014 10:23 AM

Command Line Output
 
Hi

I essentially just want to do the following with my bash script:

read.sh
Code:

#!/bin/bash

echo "Insert Number"
read num
echo $num

Now this works fine on its own when run as
Code:

read.sh
Sometimes however I want to have the option to be able to output it into a file from the command line if I would like so I tried running it as
Code:

read.sh > output.txt
This results in the echo being put into the output file instead of prompting the user. How do I go about this while maintaining the option of making an output in the command line if I would like?


Thanks

schneidz 04-25-2014 10:25 AM

you can look into tee
Code:

man tee

szboardstretcher 04-25-2014 10:38 AM

Nice CS question. This will output "insert number" to stderr, and $num to stdout. We will only save stdout to the file.

Script
Code:

#!/bin/bash
echo "Insert Number" >&2
read num
echo $num

Output
Code:

[root@dev ~]# ./run.sh 1> tmp.file
Insert Number
123

[root@dev ~]# cat tmp.file
123


mp85 04-25-2014 10:46 AM

Quote:

Originally Posted by szboardstretcher (Post 5159062)
Nice CS question. This will output "insert number" to stderr, and $num to stdout. We will only save stdout to the file.

Script
Code:

#!/bin/bash
echo "Insert Number" >&2
read num
echo $num

Output
Code:

[root@dev ~]# ./run.sh 1> tmp.file
Insert Number
123

[root@dev ~]# cat tmp.file
123


Thanks guys, this one worked perfect


All times are GMT -5. The time now is 08:02 PM.