LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-25-2014, 11:23 AM   #1
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Rep: Reputation: Disabled
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
 
Old 04-25-2014, 11:25 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
you can look into tee
Code:
man tee
 
1 members found this post helpful.
Old 04-25-2014, 11:38 AM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
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
 
1 members found this post helpful.
Old 04-25-2014, 11:46 AM   #4
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
echo two command output in the same line chuikingman Linux - Software 8 03-31-2011 07:20 PM
trying to output only first line of command out put dave247 Debian 2 11-03-2008 05:36 PM
Command line to get output on a file satimis Programming 4 06-24-2005 07:04 AM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 05:35 PM
Command to output file content line by line aznluvsmc Programming 2 09-12-2004 08:45 PM


All times are GMT -5. The time now is 07:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration