LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 01-06-2008, 08:20 AM   #1
mayaabboud
Member
 
Registered: Oct 2007
Posts: 53

Rep: Reputation: 15
Unhappy 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 ??
 
Old 01-06-2008, 08:30 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
Do you mean like this?
Code:
y=$(ls)
z=$y
echo $z
 
Old 01-06-2008, 08:42 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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 (')
 
Old 01-06-2008, 09:00 AM   #4
mayaabboud
Member
 
Registered: Oct 2007
Posts: 53

Original Poster
Rep: Reputation: 15
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
 
Old 01-06-2008, 09:11 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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?
 
Old 01-06-2008, 09:23 AM   #6
mayaabboud
Member
 
Registered: Oct 2007
Posts: 53

Original Poster
Rep: Reputation: 15
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 09:44 AM.
 
Old 01-06-2008, 09:59 AM   #7
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
like this?
Code:
#!/bin/bash
y=$(ls)
z=$y
echo $z
 
Old 01-06-2008, 10:06 AM   #8
mayaabboud
Member
 
Registered: Oct 2007
Posts: 53

Original Poster
Rep: Reputation: 15
yes just like homey posted it
 
Old 01-06-2008, 10:09 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Can you get back to normal by simply closing the terminal?

Please post the exact content of the script file.
 
Old 01-06-2008, 10:15 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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.
 
Old 01-06-2008, 10:27 AM   #11
mayaabboud
Member
 
Registered: Oct 2007
Posts: 53

Original Poster
Rep: Reputation: 15
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 !!
 
Old 01-06-2008, 10:40 AM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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.
 
Old 01-06-2008, 10:43 AM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by mayaabboud View Post
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?
 
Old 01-06-2008, 01:16 PM   #14
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell scripting/bash/variables mayaabboud Linux - General 4 01-06-2008 09:17 AM
BASH Shell Scripting tekmann33 Linux - General 4 02-08-2007 06:03 PM
moving files that have spaces in variables -bash scripting bhar0761 Programming 10 09-22-2005 08:30 AM
server variables in bash scripting basher400 Linux - Newbie 4 04-11-2005 07:04 AM
BASH Shell scripting help ewarmour Linux - General 8 05-25-2002 08:10 AM


All times are GMT -5. The time now is 08:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration