Get the return value and get the output to $results


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get the return value and get the output to $results
# 1  
Old 08-06-2014
Get the return value and get the output to $results

Code:
cmd()
{
        echo "                                                            "
        echo "$(whoami)@$(hostname):$(pwd)# $*"
        results=`eval $*`
        echo $results
 
}

I want to get the eval $* 's return value and pass it to a new variable $val, and get "eval $*" 's the output to variable $results, but I just don't want to run eval $* more than once in function "cmd" to realize this ,

Last edited by yanglei_fage; 08-06-2014 at 09:23 AM..
# 2  
Old 08-06-2014
Not clear. What do you want to achieve by eval $*? Where do you assign anything to "$val"? And how do you expect that snippet to run several times?
# 3  
Old 08-06-2014
Quote:
Originally Posted by RudiC
Not clear. What do you want to achieve by eval $*? Where do you assign anything to "$val"? And how do you expect that snippet to run several times?

I want to get the eval $* 's return value and pass it to a new variable $val, and get "eval $*" 's the output to variable $results, but I just don't want to run eval $* more than once in function "cmd" to realize this ,

Last edited by yanglei_fage; 08-06-2014 at 09:25 AM..
# 4  
Old 08-06-2014
Code:
results=$("$@")
val=$?

# 5  
Old 08-06-2014
Quote:
Originally Posted by yanglei_fage
I want to get the eval $* 's return value
Your explanation is circular.

"What do you hope to achieve with eval $*?" "I hope to execute eval $*"


WHY do you want to execute eval $*? What chain of logic led you here? What problem led you here?

This is extremely ill-advised in nearly all circumstances. If you ever find yourself doing things like eval $* you can be fairly sure your programming took a wrong turn somewhere.
# 6  
Old 08-07-2014
Quote:
Originally Posted by neutronscott
Code:
results=$("$@")
val=$?

thanks, but it's output looks mess, how can I handle it ?
Code:
lyang001@lyang001-OptiPlex-9010:/tmp$ cat yy.sh 

results=$("$@")
val=$?
echo $results
echo $val
lyang001@lyang001-OptiPlex-9010:/tmp$ ./yy.sh ifconfig eth5
eth5: error fetching interface information: Device not found

1
lyang001@lyang001-OptiPlex-9010:/tmp$ ./yy.sh ifconfig eth0
eth0 Link encap:Ethernet HWaddr 90:b1:1c:8c:10:b7 inet addr:128.224.162.171 Bcast:128.224.163.255 Mask:255.255.254.0 inet6 addr: fe80::92b1:1cff:fe8c:10b7/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:14425536 errors:0 dropped:0 overruns:0 frame:0 TX packets:6596884 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:3647161381 (3.6 GB) TX bytes:2084953656 (2.0 GB) Interrupt:20 Memory:f7f00000-f7f20000
0

---------- Post updated at 08:18 PM ---------- Previous update was at 08:16 PM ----------

Quote:
Originally Posted by Corona688
Your explanation is circular.

"What do you hope to achieve with eval $*?" "I hope to execute eval $*"


WHY do you want to execute eval $*? What chain of logic led you here? What problem led you here?

This is extremely ill-advised in nearly all circumstances. If you ever find yourself doing things like eval $* you can be fairly sure your programming took a wrong turn somewhere.
Below it's logic

val ---> is the return value of $@
results ----> is the output of $@

but the results here is mess, how can I handle it, can we use "file" to store the result or better way ?

Code:
lyang001@lyang001-OptiPlex-9010:/tmp$ cat yy.sh 

results=$("$@")
val=$?
echo $results
echo $val
lyang001@lyang001-OptiPlex-9010:/tmp$ ./yy.sh ifconfig eth5
eth5: error fetching interface information: Device not found

1
lyang001@lyang001-OptiPlex-9010:/tmp$ ./yy.sh ifconfig eth0
eth0 Link encap:Ethernet HWaddr 90:b1:1c:8c:10:b7 inet addr:128.224.162.171 Bcast:128.224.163.255 Mask:255.255.254.0 inet6 addr: fe80::92b1:1cff:fe8c:10b7/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:14425536 errors:0 dropped:0 overruns:0 frame:0 TX packets:6596884 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:3647161381 (3.6 GB) TX bytes:2084953656 (2.0 GB) Interrupt:20 Memory:f7f00000-f7f20000
0

# 7  
Old 08-07-2014
I don't think the output is a mess it is just that you are passing it unquoted to echo, try:

Code:
results=$("$@")
val=$?
echo "$results"
echo $val


Last edited by Chubler_XL; 08-07-2014 at 12:54 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirecting the results to different output files

Hi All, I am trying a shell script and need your help on taking the results to different output files. I have tried the below code: nawk ' {CNF = (length()-10)/7 printf "%9s", substr ($0, 1, 9) for (i=0; i<=CNF; i++) T = substr ($0, 10+i*7, 7) TMP = 100 - (T + T + T + T + T + T + T + T... (24 Replies)
Discussion started by: am24
24 Replies

2. Shell Programming and Scripting

Always output 6 columns regardless of search results

If I am searching for AA then then BB in a loop, how do I make the output always contain 6 columns of comma separated data even when there may only be 4 search matches? AA11 AA12 AA13 AA14 BB11 BB12 BB13 BB14 BB15 BB16 Final output: AA11,AA12,AA13,AA14,,,... (14 Replies)
Discussion started by: jojojmac5
14 Replies

3. Shell Programming and Scripting

Output of AWK Results

In the following line The AWK statement parses through a listing for files and outputs the results using the {print} command to the screen. Is there a way to (a) send the output to a file and (b) actually perform a cp cmd to copy the listed files to another directory? ls | awk -va=$a -vb=$b... (1 Reply)
Discussion started by: rdburg
1 Replies

4. UNIX for Dummies Questions & Answers

Why do these 2 find commands return different results?

Hi, I am using the korn shell on Solaris box. Why does the following 2 commands return different results? This command returns no results (I already used this command to create a list of files which I moved to an archive directory) find ????10??_*.dat -type f -mtime +91 However this... (15 Replies)
Discussion started by: stumpy1
15 Replies

5. UNIX for Advanced & Expert Users

Insert Data into db and return php results

This is a three step process: a) Upload date ->scrub\prep data, b) insert into db, c) return php results page. I have a question about the best practices for unix to process this. I have data from a flat file that I've scrubbed and cleaned with sed and awk. When it is complete I have an... (0 Replies)
Discussion started by: dba_frog
0 Replies

6. UNIX for Dummies Questions & Answers

Output results of multiple commands to a file

Hi I want to output the results of multiple commands to a single file. I use simple Ping, telnet commands to check the connectivity to many servers. Can i execute all the commands and write the output to a file instead of executing them one by one? Thanks Ashok (2 Replies)
Discussion started by: ashok.k
2 Replies

7. Shell Programming and Scripting

Backing out of a script if command doesn't return any results?

Hello I have a script which emails identifies the user ID of a user and sends them an email. A user can enter part of the name of the person he/wants to send the email to. Then I use the ypcat command to identify the UID of that person. The problem I'm having, is building in an error trap... (1 Reply)
Discussion started by: Glyn_Mo
1 Replies

8. UNIX for Dummies Questions & Answers

cp output /dev/null results in not a directory

Hello, I am working on a script to measure the read performance of a busybox environment. The logical choice is to use a command line like: (time cp * /dev/null) 2> /tmp/howlong.txt Ah, the rub is cp or /dev/null will only accept a single file at a time. The result in the txt file is and... (1 Reply)
Discussion started by: stevesmo
1 Replies

9. Shell Programming and Scripting

Calling C program from cron results in no output

I can call a C program from the shell and results are outputted as normal. The C program processes some files and spits out a .csv file. If I scheduled it in cron, there is no output. If their a special way to schedule C programs in cron? thanks & regards (1 Reply)
Discussion started by: hazno
1 Replies

10. UNIX for Dummies Questions & Answers

How to output the results of the AT command - properly!

Hi, I am new to UNIX and I am more used to simple commands like those in VMS. One of them is the ability to get the output from a job using the /out=<file> command in VMS. I want to submit a job (a set of unix commands) using the AT command but to get the output in a file like that used in... (4 Replies)
Discussion started by: SpanishPassion
4 Replies
Login or Register to Ask a Question