Unexpected Echo Behavior


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unexpected Echo Behavior
# 1  
Old 11-30-2012
Unexpected Echo Behavior

Hello all,

I have a basic issue that I can't seem to search for since I'm not sure how to describe the behavior. Could anyone kindly assist the novice?

(Sample) File Data:
Code:
bundle-ppp-1/1.78
bundle-ppp-1/2.80
bundle-ppp-1/1.79
bundle-ppp-1/2.81
bundle-ppp-1/1.80
bundle-ppp-1/2.82
bundle-ppp-1/1.81
bundle-ppp-1/2.83
bundle-ppp-1/1.82
bundle-ppp-1/2.84

Code:
Code:
# for all in `cat output`; do echo "/configure port $all shutdown"; done

Expected output:
Code:
/configure port bundle-ppp-1/1.78 shutdown

Actual output:
Code:
shutdowne port bundle-ppp-1/1.78

I see that it has taken the "shutdown" portion of the echo command and overwritten the word "configure" but I don't understand why or how to correct the behavior.

Thanks for taking the time to read my post Smilie

Last edited by Franklin52; 12-02-2012 at 08:45 AM.. Reason: Please use code tags
# 2  
Old 11-30-2012
Looks like there are carriage returns in your text file, i.e. it's a DOS/Windows text file.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 3  
Old 11-30-2012
Thank you for the reply. I feel quite silly for not realizing this. I just reformatted the carriage returns and everything works properly.
# 4  
Old 11-30-2012
After you've been bitten by this once or twice, you'll always suspect it when you see overwriting at the start of a display line.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 12-01-2012
Also: That's a useless use of cat and a useless use of backticks.

If your lines had spaces in them, this code will break down.

If the file is too large, this code will break down.

Much better done with a while-read loop.

Code:
while read LINE
do
...
done < inputfile

This User Gave Thanks to Corona688 For This Post:
# 6  
Old 12-01-2012
Thanks for the articles Cor, I'm new and looking to improve. I'd be embarrassed to post some of the spaghetti code I string together to get my tasks done. As I keep learning, I'm striving for simple, efficient code. I enjoy this world Smilie
# 7  
Old 12-03-2012
Another way using exec:
Code:
exec < infile

while read LINE
do
    ....
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Launchd-owned processes unexpected behavior

Ok, so I have been struggling with this for a few days and I think I need an explanation of a few things before I go any further. I'm not sure it's possible to do what I'm trying, so before I pull my hair out, here is what I'm doing: I have written a program in LiveCode that sits on our... (2 Replies)
Discussion started by: nextyoyoma
2 Replies

2. UNIX for Advanced & Expert Users

unexpected behavior bash, set -o vi, history -a, and HISTFILE

I am trying to get my history in sync in multiple bash sections and things aren't working the way I expect. Desired behavior, hitting esc-K in all bash sessions (same userid and machine) will use the same history. Observed behavior: Esc-k shows the history of the current session, rather than... (8 Replies)
Discussion started by: gg48gg
8 Replies

3. IP Networking

iptables DNAT of outgoing destination port, unexpected behavior

Not sure if this should be here or in the security section. I am developing software that dynamically manipulates netfilter/iptables rules (through system() calls of the command strings, I'm not trying to hack the netfilter code). Basically, UDP messages that are sent by an application on, say,... (0 Replies)
Discussion started by: cjh19460
0 Replies

4. Shell Programming and Scripting

With that logic this echoes "echo". Question about echo!

echo `echo ` doesn't echoes anything. And it's logic. But echo `echo `echo ` ` does echoes "echo". What's the logic of it? the `echo `echo ` inside of the whole (first) echo, echoes nothing, so the first echo have to echo nothing but echoes "echo" (too much echoing :P):o (2 Replies)
Discussion started by: hakermania
2 Replies

5. HP-UX

Unusual Behavior?

Our comp-operator has come across a peculiar ‘feature'. We have this directory where we save all the reports that were generated for a particular department for only one calendar year. Currently there are 45,869 files. When the operator tried to backup that drive it started to print a flie-listing... (3 Replies)
Discussion started by: vslewis
3 Replies

6. UNIX for Dummies Questions & Answers

How to correctly use an echo inside an echo?

Bit of a weird one i suppose, i want to use an echo inside an echo... For example... i have a script that i want to use to take users input and create another script. Inside this script it creates it also needs to use echos... echo "echo "hello"" >$file echo "echo "goodbye"" >$file ... (3 Replies)
Discussion started by: mokachoka
3 Replies

7. Shell Programming and Scripting

Difference between using "echo" builtin and /bin/echo

So in my shell i execute: { while true; do echo string; sleep 1; done } | read line This waits one second and returns. But { while true; do /bin/echo string; sleep 1; done } | read line continues to run, and doesn't stop until i kill it explicitly. I have tried this in bash as well as zsh,... (2 Replies)
Discussion started by: ulidtko
2 Replies

8. Solaris

Unexpected df behavior

Hi, I have Sun SPARC Enterprise T5140 with two 2,5" 15rpm 146GB SAS HDD. In friday there were a lot of errors with fs on them. After reconfiguring all seemed to be fine but today I get the following strange behavior of df -kh command and troubles with files, I written on first disk in friday.... (9 Replies)
Discussion started by: Sapfeer
9 Replies

9. Shell Programming and Scripting

Echo behavior

Echo is removing extra blank spaces. See the command. export INSTALLDIR=”First Second /Two Space” echo $INSTALLDIR out put: First Second /Two Space Here only on blnak space is present while with command Echo “$INSTALLDIR” Out put: ”First Second /Two Space” It's correct output... (2 Replies)
Discussion started by: Saurabh78
2 Replies

10. Programming

ls behavior

I put this here because it is a 'behavior' type question.. I seem to remember doing ls .* and getting all the .-files, like .profile .login etc. But ls .* doesn't do that, it lsts the contents of every .*-type subdirectory. Is it supposed to? I should think that a -R should be given to... (10 Replies)
Discussion started by: AtleRamsli
10 Replies
Login or Register to Ask a Question