Quick Question on Echo


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Quick Question on Echo
# 1  
Old 04-17-2012
Quick Question on Echo

Hey all, so I wrote a pretty simple script. It's viewable here:

Code:
#!/bin/sh
FILE=`ls $1`
for filename in $FILE
do
echo $filename
`echo $filename | tr e 5 `
done

So, as you can see it gets the list from a command line specified directory. It then loops through, echoes the filename, and then does an echo, using the tr function.

I am just not understanding my output, just hoping for some quick clarity here. Example output is

House
./shell.sh: Hous5 not found
playground
./shell.sh: playground not found

etc etc. I suppose those are the two test case. I'm curious as to why the first echo prints fine (well not really that's expected) but the second does the exchange, but then it's looking for those files? Not really sure why this is...

Any help would be great!

EDIT: Does this for both files and directories.
EDIT2: Seems like it's doing it if I'm not assigning into a variable. Just curious as to the cause of this. Looks like echo does not have to be in the external program...doesn't matter to the program in terms of error production.
EDIT3: Looks like it's when I externally call echo, and do NOT assign it into a variable. Just as a quick question, does the assignment in shell scripting act as a piping command, putting the expected stdout into the variable?

Last edited by lowExpectations; 04-17-2012 at 10:44 PM..
# 2  
Old 04-17-2012
When the subshell
Code:
`echo $filename | tr e 5 `

is evaluated, you are telling your script (shell.sh) to execute the result, i.e. Hous5, playground, etc. Since these executable files do not exist, you are getting an error.
# 3  
Old 04-17-2012
It's not the echo, but your use of back quotes. You only need the back quotes when you want to use the output of a command such as in the assignment to a variable. For instance:

Code:
d=`date`

Assigns the output from date into the variable d.

Using them without such an assignment, or some other legitimate case, as you have causes the output to be interpreted as a command which is what is causing your error. If you remove the d= from the example above, the shell will complain that the day of week (issued as the first token from the command) is not a valid command.
# 4  
Old 04-17-2012
the backticks (`) around a command is replaced but the output of that command. Thus
var=`echo stuff` or var=`ls $1` is replaced by the output of that.

Using `echo stuff` on a line without an assignment would first be evaluated to get the output, and then evaluated as a command. So you're trying to run these files after piped to tr.
# 5  
Old 04-17-2012
Okay, so what I'm getting is when you backquotes a command, it evaluates it to it's entirety.

At that point it decides "Is there an assignment?" - if there was it assigns the result to the variable.

If not it will try to run the finished evaluated contents of the backquotes as an external system command?

---------- Post updated at 09:58 PM ---------- Previous update was at 09:56 PM ----------

Quote:
Originally Posted by agama
It's not the echo, but your use of back quotes. You only need the back quotes when you want to use the output of a command such as in the assignment to a variable. For instance:

Code:
d=`date`

Assigns the output from date into the variable d.

Using them without such an assignment, or some other legitimate case, as you have causes the output to be interpreted as a command which is what is causing your error. If you remove the d= from the example above, the shell will complain that the day of week (issued as the first token from the command) is not a valid command.
But if the final evaluation of the backquotes is date (i.e. `echo dote | tr o a`) it will run date?
# 6  
Old 04-17-2012
Quote:
Originally Posted by lowExpectations
But if the final evaluation of the backquotes is date (i.e. `echo dote | tr o a`) it will run date?
Try it and see!

Code:
[mute@geek ~]$ `echo dato | tr o e`
Wed Apr 18 02:04:19 UTC 2012

# 7  
Old 04-17-2012
No - it will try to run the OUTPUT of date
Your example is the same as

Code:
Tue Apr 17 20:01:54 MDT 2012

Type that then hit return. Or if you understand eval, it implies an
Code:
 eval Tue Apr 17 20:01:54 MDT 2012

either is a nonsense command. The shell will complain that it cannot run the file "Tue"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Quick question

When I have a file like this: 0084AF aj-123-a NAME Ajay NAME Kumar Engineer 015ED6 ck-345-c 020B25 ef-456-e 027458 pq-890-p NAME Peter NAME Salob Doctor 0318F0 xy-123-x NAME Xavier Arul NAME Yesu Supervisor 0344CA de-456-d where - The first NAME is followed by... (6 Replies)
Discussion started by: ajay41aj
6 Replies

2. AIX

quick question

Hi, At best I'm a junior admin with a big problem. My developers have got my root password and mgmt insists they need it. I can't even change it when people knowing it leave. I'm certain they've hardcoded it into routines. I've searched my servers and grepped everything & can't find it. ... (5 Replies)
Discussion started by: keith.m
5 Replies

3. UNIX for Dummies Questions & Answers

Quick question

Hello all, Quick question from a fairly new to Unix developer. if then completedLogFile=$logfile.$(date +%Y%m%d-%H:%M:%S) mv $logfile $completedLogFile fi I understand that this portion of code is simply copying a tmp logfile to a completed logfile when a condition is true. The... (2 Replies)
Discussion started by: JohnnyBoy
2 Replies

4. UNIX for Dummies Questions & Answers

quick question

from command prompt I did grep two words on a same line for eg: grep abc | grep xyz and I got tht particular line, but I want to know when I vi that file how to directly search for that particular line? I appreciate if any one can provide answer, thanks in advance (2 Replies)
Discussion started by: pkolishetty
2 Replies

5. UNIX for Dummies Questions & Answers

Quick question

Hi, Is there a simple way, using ksh, to find the byte position in a file that a stated character appears? Many thanks Helen (2 Replies)
Discussion started by: Bab00shka
2 Replies

6. Shell Programming and Scripting

Ok quick question

Hi i just wanted to know is there anyway to log the keystrokes on a remote computer? For example i let my nieces play on my other computer downstairs *my computer and the one downstairs are on a LAN* and i want to see everything they type in to make sure they arent doing anything they are supposed... (1 Reply)
Discussion started by: Corrail
1 Replies

7. Shell Programming and Scripting

quick question

does anyone know what $? means? i echoed it on my box (running AIX Korn shell) and got 127 (2 Replies)
Discussion started by: penfold
2 Replies

8. UNIX for Dummies Questions & Answers

quick question

hi guys trying to understand what this line means sed is a stream editor and i understand that, i have a file already selected i want to edit so i use -e sed -e the next stesp is s/$* s is a subsititute replacement sed -e s/$*//g $ is in reference of the last line /g makes it... (2 Replies)
Discussion started by: hamoudzz
2 Replies

9. UNIX for Dummies Questions & Answers

A Quick Question

Wat is the difference between the cp mv ln etc in /usr/sbin/static and cp mv ln functions in /usr/bin (4 Replies)
Discussion started by: DPAI
4 Replies

10. Shell Programming and Scripting

A very quick question

Just a super quick question: how do you put a link in your php code. I want to make a link to something in /tmp directory. i.e. how do you put a href into php, I think it's done a bit differently. thanks john (1 Reply)
Discussion started by: jmg5
1 Replies
Login or Register to Ask a Question