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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
01-06-2008, 07:20 AM
|
#1
|
Member
Registered: Oct 2007
Posts: 53
Rep:
|
shell scripting/bash/variables
hi,
i ve got the following variables defined,
Y=ls
Z="$Y"
can someone please tell me how to get 'the execution of ls' out of Z , like when i write
echo $Z i get ls
but how can i change or redefine the variables to get the execution of ls ??
|
|
|
01-06-2008, 07:30 AM
|
#2
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
Do you mean like this?
Code:
y=$(ls)
z=$y
echo $z
|
|
|
01-06-2008, 07:42 AM
|
#3
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Always try commands in real-time before trying to put in a script.
try these:
echo `ls`
echo $(ls)
echo $ls
echo ls
When you understand how each of these works, then it will be easier to see what happens when they are assigned to variables.
Note the "`" (backtic)---not the same as the single quote (')
|
|
|
01-06-2008, 08:00 AM
|
#4
|
Member
Registered: Oct 2007
Posts: 53
Original Poster
Rep:
|
thanks for answering,
sadly i had tried the eval command , i typed eval ls, in my xterm and now all i get is the result of eval ls no matter what i type !!
do u know how to disable this command ?
maya
|
|
|
01-06-2008, 08:11 AM
|
#5
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
If I type "eval ls", I get the same answer as if typing "ls". After using eval, everything else works normally.
Can you give some more detail/examples of exactly what happens?
Also, why do you need to use eval?
|
|
|
01-06-2008, 08:23 AM
|
#6
|
Member
Registered: Oct 2007
Posts: 53
Original Poster
Rep:
|
I had been trying to get the answer by myself, so i tried
eval ls
to try to get the execution of ls
but then when i read the answer HOMEY posted, i tried it out, i copied it into a script called execute_ls
but everytime i type on the xterm :
chmod +x execute_ls
then
./execute_ls
all i get is
execute_ls execute_ls~
and when i typed set -x to trace whats happening
i had a line +echo <listoffilesinthedirectory>
its driving me nuts
i cant do anythg !
Last edited by mayaabboud; 01-06-2008 at 08:44 AM.
|
|
|
01-06-2008, 08:59 AM
|
#7
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
like this?
Code:
#!/bin/bash
y=$(ls)
z=$y
echo $z
|
|
|
01-06-2008, 09:06 AM
|
#8
|
Member
Registered: Oct 2007
Posts: 53
Original Poster
Rep:
|
yes just like homey posted it
|
|
|
01-06-2008, 09:09 AM
|
#9
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Can you get back to normal by simply closing the terminal?
Please post the exact content of the script file.
|
|
|
01-06-2008, 09:15 AM
|
#10
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
I tried the script and it worked fine. What happens if you simply run "ls" in that directory? The result should be the same as running the script.
|
|
|
01-06-2008, 09:27 AM
|
#11
|
Member
Registered: Oct 2007
Posts: 53
Original Poster
Rep:
|
my script execute_ls contains the following :
#!/bin/bash
Y=$(ls)
Z=$Y
echo $Z
then i go and write chmod +x execute_ls
then ./execute_ls
which gives me :
execute_ls execute_ls~
this doesnt work, i tried reopening a new terminal many times. i even restarted the computer !!
the only thg that works is when i add an echo line at the beginning like this:
echo "write>>"
read Y
echo "write>"
read Z
if [[ "Y" = "$(ls)" && "Z"="$Y"]] ; then
echo $Y
echo $Z
fi
this works, meaning that the script responds, by telling me to "write>>"
but it gives me no answer
dont blame me for being complicazed, everytime i call a script i get the result of an ls command !!
|
|
|
01-06-2008, 09:40 AM
|
#12
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
I am totally confused.
What happens if you open a new terminal and simply run "ls"?
This script:
Quote:
#!/bin/bash
y=$(ls)
z=$y
echo $z
|
should do the same thing.
|
|
|
01-06-2008, 09:43 AM
|
#13
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by mayaabboud
then i go and write chmod +x execute_ls
then ./execute_ls
which gives me :
execute_ls execute_ls~
|
Maybe I'm not following, but this looks to me the correct behavior. Let me dissect your simple script:
1. #!/bin/bash
this is the so-called sha-bang: it tells to use /bin/bash as interpreter
2. Y=$(ls)
this assigns to Y the output of the command ls. This is called "command substitution" and the syntax $(command) is equivalent to `command` (with back-ticks).
3. Z=$Y
this assigns to Z the value of Y, which is still the output of the ls command
4. echo $Z
this prints to the standard output the value of Z, that is the output of ls: execute_ls execute_ls~. It looks like in the current directory you have only the script "execute_ls" and its backup file "execute_ls~" (that one created by text editors like gedit).
What am I missing here? Can you re-formulate your original question?
|
|
|
01-06-2008, 12:16 PM
|
#14
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
Quote:
all i get is
execute_ls execute_ls~
|
That's showing the scriptname and the backup scriptname file which is what it's supposed to do.
Maybe you wanted a more verbose listing as from ( ls -al )
Code:
#!/bin/bash
y=$(ls -al)
z=$y
echo $z
Just a side note, I use script name which are less likely to get you into trouble executing something unwanted. For example: my_script
|
|
|
All times are GMT -5. The time now is 09:33 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|