Hey all, so I wrote a pretty simple script. It's viewable here:
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..
When the subshell
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.
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:
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.
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.
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:
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?
No - it will try to run the OUTPUT of date
Your example is the same as
Type that then hit return. Or if you understand eval, it implies an
either is a nonsense command. The shell will complain that it cannot run the file "Tue"
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)
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)
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)
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)
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)
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)
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)