Visit Our UNIX and Linux User Community


Assigning command to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning command to a variable
# 1  
Old 08-10-2009
Assigning command to a variable

I've been searching these forums for a while, but this is my first actual post, so please bear with me Smilie

I'm writing a short script using ksh and am trying to store a command and parameters in a variable. My intention is to issue the command by calling the variable. The command will contain double quotes. My problem is that for some reason there's no output.

Here's a simplified view of what I'm trying to accomplish (find command is used just as an example of the problem):

Code:
#!/usr/bin/ksh
cmd="find . -name \"55*\""
echo $cmd
$cmd

The only output is what I echo'ed:
Code:
# ./problem.sh
find . -name "55*"

If I issue the same command outside of the script, I get the desired result:
Code:
# find . -name "55*"
./55_messages.txt

Is is possible to store a command+parameters in a variable and run the command through the variable?

Thanks,
Hubert
# 2  
Old 08-11-2009
HTML Code:
#!/usr/bin/ksh
cmd="find . -name \"55*\""
echo $cmd
$cmd
Any command in a variable, use eval
Code:
cmd="find . -name 55\*"
eval $cmd

# 3  
Old 08-11-2009
not ""

try `` instead

Quote:
ABC=`ls -l`
$ABC
# 4  
Old 08-11-2009
Quote:
Originally Posted by sparcguy
not ""
try `` instead
Code:
ABC=`ls -l`
$ABC

Wouldn't that just put the output from the ls into the variable, then try to run it (ie execute something like "-rw-r--r--")?
# 5  
Old 08-11-2009
Quote:
Originally Posted by edidataguy
HTML Code:
#!/usr/bin/ksh
cmd="find . -name \"55*\""
echo $cmd
$cmd
Any command in a variable, use eval
Code:
cmd="find . -name 55\*"
eval $cmd

eval! Thank you edidataguy! This did the trick Smilie

---------- Post updated 08-11-09 at 12:00 AM ---------- Previous update was 08-10-09 at 11:59 PM ----------

Quote:
Originally Posted by Smiling Dragon
Wouldn't that just put the output from the ls into the variable, then try to run it (ie execute something like "-rw-r--r--")?
Yeah, that would capture the output from 'ls -l' into a variable. I found many hits for doing that, but unfortuantely not the opposite which is what I wanted to accomplish Smilie

Thanks tho guys!

Previous Thread | Next Thread
Test Your Knowledge in Computers #505
Difficulty: Medium
An executable statement may not invoke (or call or execute) another procedure (also called subroutine, function, method, etc.),
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expect - assigning UNIX command output to a variable

Hi, I'm writing a script that connects through ssh (using "expect") and then is supposed to find whether a process on that remote machine is running or not. Here's my code (user, host and password are obviously replaced with real values in actual script): #!/usr/bin/expect set timeout 1... (3 Replies)
Discussion started by: oseri
3 Replies

2. UNIX for Dummies Questions & Answers

Tcsh command for assigning output of awk to variable

Hi I have a text file with 2 values and I am trying to assign each value to a variable and then write those to text files. So if the textfile is data.txt with 2 values x and y I want to assign mean=x, and stdev=y and then write these out in text files alongwith the id ($id has already been... (6 Replies)
Discussion started by: violin
6 Replies

3. Shell Programming and Scripting

problem in assigning value to variable have value fo other variable

my script is some thing like this i11="{1,2,3,4,5,6,7,8,9,10,11,}" echo "enter value" read value ..............suppose i11 x="$value" echo "$($value)" .............the echo should be {1,2,3,4,5,6,7,8,9,10,11,} but its showing "i11" only. plz help me out to get desired... (10 Replies)
Discussion started by: sagar_1986
10 Replies

4. Shell Programming and Scripting

Removing a character from a variable and assigning it to another variable?

Hi folks. I have this variable called FirstIN that contains something like this: 001,002,003,004... I am trying to assign the content of this variable into ModifiedIN but with the following format : 001 002 003 004...(changing the commas for spaces) I thought about using sed but i am not... (17 Replies)
Discussion started by: Stephan
17 Replies

5. UNIX for Dummies Questions & Answers

Assigning the output of a command to a variable, where there may be >1 line returned?

Hello I am using unix CLI commands for the Synergy CM software. The command basically searches for a folder ID and returns the names of the projects the folder sits in. The result is assigned to a variable: FIND_USE=`ccm folder -fu -u -f "%name"-"%version" ${FOLDER_ID}` When the command... (6 Replies)
Discussion started by: Glyn_Mo
6 Replies

6. Shell Programming and Scripting

Assigning variable from command outputs to shell

First, this is bash (3.2.17), on a Mac, 10.5.7. What I'm trying to do is look at a list of users, and check to see if each exists. If they do, do some more stuff, if they don't, drop them into an error file. So, my user list is: foo - exists bar - does not exist blah - does not exist ... (2 Replies)
Discussion started by: staze
2 Replies

7. Shell Programming and Scripting

Assigning output of a command to variable

When I run time -p <command>, it outputs: real X.XX user X.XX sys X.XXwhere X.XX is seconds. How I can take just that first number output, the seconds of real time, and assign that to a variable? (9 Replies)
Discussion started by: jeriryan87
9 Replies

8. Shell Programming and Scripting

Assigning output of command to a variable in shell

hi, I want to assign find command result into some temporary variable: jarPath= find /opt/lotus/notes/ -name $jarFile cho "the jar path $jarPath" where jarPath is temporary variable. Can anybody help on this. Thanks in advance ----Sankar (6 Replies)
Discussion started by: sankar reddy
6 Replies

9. Shell Programming and Scripting

assigning command output to a shell variable

I have the sql file cde.sql with the below contents: abcdefghij abcwhendefothers sdfghj when no one else when others wwhen%others exception when others Now I want to search for the strings containing when others together and ceck whether that does not occur more than once in the... (2 Replies)
Discussion started by: kprattip
2 Replies

10. Shell Programming and Scripting

Assigning output of command to a variable

Hi, I'm trying to assign the output of a command to a variable and then concat it with another string, however, it keeps overwriting the original string instead of adding on to the end of the string. Contents of test.txt --> This is a test var1="`head -n 1 test.txt`" echo $var1 (This is a... (5 Replies)
Discussion started by: oma04
5 Replies

Featured Tech Videos