LinuxQuestions.org
Visit Jeremy's Blog.
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, 10: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, 10:25 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

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

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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, 10: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 11 05-06-2019 10:44 AM
trying to output only first line of command out put dave247 Debian 2 11-03-2008 04:36 PM
Command line to get output on a file satimis Programming 4 06-24-2005 06:04 AM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 04:35 PM
Command to output file content line by line aznluvsmc Programming 2 09-12-2004 07:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:35 PM.

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
Open Source Consulting | Domain Registration