Executing commands through shell variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Executing commands through shell variable
# 1  
Old 07-23-2009
Executing commands through shell variable

Hey there,
I am trying to pass a command as argument to a function.
The command shows up in $1.
Now I want to execute this command, but if I do a $1

./sample
"bla/blaprintf: warning: ignoring excess arguments, starting with `bla/bla'

The code is :

#!/bin/ksh
fn()
{
$1
}

fn 'printf "bla/bla bla/bla bla bla bla bla\\n"'


Any ideas how to get it work please ?
Thanks,
Shrikant
# 2  
Old 07-23-2009
Hi.

There is nothing wrong with your function.

The error
Code:
"bla/blaprintf: warning: ignoring excess arguments, starting with `bla/bla'

shows that the printf did run.

I get a different output:
Code:
/root/tmp/test # ./sample
"bla/bla/root/tmp/test #

But there's nothing wrong with your thinking. i.e.
Code:
/root/tmp/test # cat sample
#!/bin/ksh
fn()
{
$1
}
fn ls
 
/root/tmp/test # ./sample
a  b  blah  c  file1  file2  sample  tst
/root/tmp/test #

The problem you might be seing is with your printf itself.
# 3  
Old 07-23-2009

Try:

Code:
eval "$1"

# 4  
Old 07-24-2009
That would work!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Executing multiple ssh commands inside a shell simultaneously

I would like to execute a commands in four different servers through ssh at a single instance(simultaneously). Below are the details with examples, ssh user1@server1 "grep xxxx logs" ssh user1@server2 "grep xxxx logs" ssh user1@server3 "grep xxxx logs" Each statement will take some... (4 Replies)
Discussion started by: Amutha
4 Replies

2. Shell Programming and Scripting

Trouble executing piped shell commands in perl code

I am trying to execute a piped combination of shell commands inside a perl program. However, it is not working as desired. This is my program, i am trying to print only filenames from the output of ls -l $ cat list_test #!/usr/bin/perl -w use strict; my $count=0; my @list=`ls -l|awk... (4 Replies)
Discussion started by: sam05121988
4 Replies

3. Shell Programming and Scripting

Commands not executing after FTP in shell script

Hello In the shell script we have a FTP command like below ftp -n -v -q winftp.principal.com >/infa/datafiles/GRP/Scripts/ftp_from_infa_dvcn.log<<END_SCRIPT   quote USER $FTP_USER quote PASS $FTP_PASS ascii lcd $FTP_LOCALDIR cd $FTP_FLDR put $FTP_FILE   bye exit If i... (1 Reply)
Discussion started by: Pratik4891
1 Replies

4. Homework & Coursework Questions

What are different ways of executing shell commands?

I am a student in BCIT in vancouver and taking comp2771 course.(Shell scripting). I would like to know what are different ways of executing shell commands? Thanks (1 Reply)
Discussion started by: adam25ca
1 Replies

5. Shell Programming and Scripting

Executing a shell script containing awk commands

Hi All, I am trying to execute a shell script containg awk commands. But unable to do so. Below is my script. Please help. The name of the script is scan.sh and I have tried executing it using the command sh scan.sh It is giving an error which reads like: awk:syntax error near line 7... (3 Replies)
Discussion started by: misb
3 Replies

6. UNIX for Dummies Questions & Answers

how to stay in remote shell after executing commands in ssh?

the ssh calling convention: ssh <server> If I put commands in the section, ssh will execute them immediately after logging in and return to local shell. I want to stay in the remote shell after executing these commands. How can I achieve this? Thanks for all. (1 Reply)
Discussion started by: hplonlien
1 Replies

7. Shell Programming and Scripting

Executing commands

I need to execute a command to run my script several times with varying parameters perl ex.pl -b 130198 -e 130884 -c plot plot.txt 1_plot.txt perl ex.pl -b 1345 -e 1308 -c plot plot.txt 2_plot.txt perl ex.pl -b 1345567 -e 130898 -c plot plot.txt 3_plot.txt . . . 100's of excutions ... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

8. Shell Programming and Scripting

Executing many commands at once

Hi, I want to run these two commands one after the other. awk 'BEGIN {OFS="\t"} {print $2}' sort -u rather than typing awk 'BEGIN {OFS="\t"} {print $2}' file1 > file2, then sort -u file2 > file3. Is it possible to run both commands on file1 then get output file3? Its kinda hard for... (5 Replies)
Discussion started by: kylle345
5 Replies

9. Red Hat

How commands are executing ?

Hi Folks, I have a small doubt, the binary commands under /bin and /sbin as well as other path binary files, if you peek deep into that, you can find the difference in the way of normal perl programming and some commands will be like binary files. how are the commands executing like the... (3 Replies)
Discussion started by: gsiva
3 Replies

10. UNIX for Advanced & Expert Users

executing commands in child shell

I have to execute some commands after executing one command ( cleartool setview Tagname) Problem is that I write commands in script like this. echo "test1" cleartool setview tagname echo "test2" copy file1 file2 echo "test3" but when I execute script. Output --------- test1 If I... (1 Reply)
Discussion started by: udaykishore
1 Replies
Login or Register to Ask a Question