Redirection of file input to command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Redirection of file input to command
# 1  
Old 09-23-2013
Redirection of file input to command

Hello,

I'm new to Unix (working with OS X 10.8.5) and therefore at the beginning of my adventure. If I ask something stupid, then this is not intentional, but simple nescience. Smilie

I have a problem with the redirection of text file content to echo. I was experimenting with redirection and stuff like that and I think I borked something.

I think this commands worked for my in the bash but I'm not sure anymore:
Code:
echo < file.txt

I expect that this will force echo to print the text file content to the command line. But this is not happen. Nothing but a newline is printed. Maybe my memories are wrong and this never worked. As I said, I'm not sure anymore.

I created this file with the following command:
Code:
ls -l > file.txt

And this is working. The file exists. But not the other direction.

What is at this moment wrong? My memories or my file descriptors?
# 2  
Old 09-23-2013
A starter for you:-

OSX 10.7.5, default bash terminal done longhand...
Code:
Last login: Mon Sep 23 09:11:17 on console
AMIGA:barrywalker~> text="This is a line."
AMIGA:barrywalker~> echo -n "$text" > /tmp/txt
AMIGA:barrywalker~> read newtext < /tmp/txt
AMIGA:barrywalker~> echo "$newtext"
This is a line.
AMIGA:barrywalker~> _

This User Gave Thanks to wisecracker For This Post:
# 3  
Old 09-23-2013
Thank you for your fast answer. Smilie

This means, as I wrote it, it never been able to work? I'm so sure that this was working before.... Smilie

I thought I changed the file descriptors to a state, that's causing this behavior.
# 4  
Old 09-23-2013
Again longhand to demonstrate...
Code:
Last login: Mon Sep 23 14:05:22 on ttys000
AMIGA:barrywalker~> text="This is a line."
AMIGA:barrywalker~> echo -n "$text" > /tmp/txt
AMIGA:barrywalker~> read newtext < /tmp/txt
AMIGA:barrywalker~> echo "$newtext"
This is a line.
AMIGA:barrywalker~> # Use echo direct.
AMIGA:barrywalker~> echo < /tmp/txt

AMIGA:barrywalker~> # Nothing!
AMIGA:barrywalker~> # Now attempt to force into a variable.
AMIGA:barrywalker~> oldtext=$(echo < /tmp/txt)
AMIGA:barrywalker~> # Check "oldtext".
AMIGA:barrywalker~> echo "$oldtext"

AMIGA:barrywalker~> # Again nothing.
AMIGA:barrywalker~> # Now check the variable length.
AMIGA:barrywalker~> echo ${#oldtext}
0
AMIGA:barrywalker~> # As you can see no characters.
AMIGA:barrywalker~> _

This User Gave Thanks to wisecracker For This Post:
# 5  
Old 09-23-2013
echo prints its arguments, not its stdin.
You can use xargs to transform stdin to arguments
Code:
xargs -i echo {} < file.txt

or simply
Code:
cat file.txt

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 09-23-2013
Thanks guys, you've helped me a lot to solve this brainteaser. I hope these are not the first signs of Alzheimer. Smilie
# 7  
Old 09-23-2013
Without trying to be picky, -i of xargs is deprecated. You should use -I instead.

xargs
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command output redirection to file issues

Hi, I have a peculiar issue w.r.t redirecting the command output to a file when using loop. I am redirecting command output to same file in a series of if condition statements, but if one block of if condition statement writes the log to the file , the subsequent block of if condition... (7 Replies)
Discussion started by: ananan
7 Replies

2. Shell Programming and Scripting

Input redirection within bash script

Hi, when I try to redirect input and the command is described as a string within an array redirection does not work. why? #!/bin/bash dir=("tail < ./hello.txt") tail < ./hello.txt #works ${dir} #does not work (2 Replies)
Discussion started by: heinzel
2 Replies

3. Shell Programming and Scripting

Input redirection script

Hi, #!/bin/bash while ; do rm -f /tmp/pipe mkfifo /tmp/pipe ./yuv4mpeg_to_v4l2 < /tmp/pipe & mplayer tom_and_jerry.mp4 -vf scale=480:360 -vo yuv4mpeg:file=/tmp/pipe sleep 65; done When I run this - after mplayer finishes playing video it says - Exiting... (End of... (2 Replies)
Discussion started by: ashokvpp
2 Replies

4. UNIX for Dummies Questions & Answers

When do I use input redirection?

Can someone please explain when input redirection is necessary? For example, "cat filename" and "cat< filename" produce the same result. I was told that if I need to bunzip a file that I should type "bunzip2<filename.bz2." However, if I omit the "<" I still get the same result. Can someone... (4 Replies)
Discussion started by: PTcharger
4 Replies

5. Shell Programming and Scripting

Input redirection and for loop

Hello, I need help with a bash script that I try to improve. I could not find answer so far, maybe because I'm not to familiar with the terminology so feel free to correct my language. I have a script that looks like: NODES="node_a node_b node_c" for NODE in $NODES do ... (4 Replies)
Discussion started by: pn8830
4 Replies

6. Shell Programming and Scripting

echo command and file I/O Redirection

I have a colon-delimited text file of names, addresses and phone numbers. I am trying to write a script that can add additional entries and then sort it alphabetical by last name and resave it to the original file. I am using C shell to script. This is the section of my script that I wish to... (8 Replies)
Discussion started by: userix
8 Replies

7. Shell Programming and Scripting

permanent redirection of standard input

while running a user inter-active program how can we get the commands from a file instead of the user? is there anyway to permanently redirect content of a file to standard input? (6 Replies)
Discussion started by: gfhgfnhhn
6 Replies

8. Shell Programming and Scripting

input redirection question

Hi, in my script I need to execute the following command: query $id 456 432 but it waits for a RETURN character from keyboard and therefore, it fails. I tried something like: query $id 456 432 << '\n' but, i'ts clear it is not correct. Is there any way to do this? Thxs. (0 Replies)
Discussion started by: luistid
0 Replies

9. UNIX for Dummies Questions & Answers

Input Redirection

Hi everybody, first of all i am a new member in UNIX.com and this is my first post. I am impressed with the amount of information a person can ever have in this forum, it is really great having something similiar; anyways let me tell you about the problem I am having, hope you will answer me.... (6 Replies)
Discussion started by: majeed73
6 Replies

10. Programming

Changing stdin from file redirection to console input

Hi I am doing file redirection at console for use by my binary. %console%> bin &lt inputfile After reading in the entire file, I want my program to continue taking input from the console. So essentially I want to redirect stdin back to console. But I cant figure out how to do it. I am... (4 Replies)
Discussion started by: nauman
4 Replies
Login or Register to Ask a Question