save the return value of a unix command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting save the return value of a unix command
# 1  
Old 12-01-2011
[Solved] save the return value of a unix command

Hi all,

I am new in linux script, and I have the code below

Code:
#!/bin/bash
CMD="shuf -i 1-5 -n 1"
$CMD
echo $CMD
    exit 0

if I run the $CMD, it will return me a shuffle value. However, I need to save the return value of the shuffle, and use it later on. I tried for example print the return value using $CMD, but instead, it gives me the CMD command, and not the value itself. How can I save the return value of CMD?

Thank you

Last edited by peuceul; 12-01-2011 at 09:59 AM..
# 2  
Old 12-01-2011
`...` or $(...) will actually execute the command.
with "...", you are actually just assigning the literals.

Code:
#!/bin/bash
CMD=$(shuf -i 1-5 -n 1)

echo $CMD
    exit 0


PS : welcome to the forum Smilie
This User Gave Thanks to clx For This Post:
# 3  
Old 12-01-2011
The built-in variable $? holds the exit status of the most recently executed command.
# 4  
Old 12-01-2011
Thank you all. How do I mark this thread as solved?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to save a file directly into windows from UNIX?

Hi, How can i save a file directly into my windows file system by running a shell script from my unix file system. I need to fetch data from a database and save it in a file. Since the data downloaded from database is huge, if i save it in the unix file system, the space in my home directory... (2 Replies)
Discussion started by: Little
2 Replies

2. Shell Programming and Scripting

Save value from output of Corestat and save in a list for each core

I am trying to modify the "corestat v1.1" code which is in Perl.The typical output of this code is below: Core Utilization CoreId %Usr %Sys %Total ------ ----- ----- ------ 5 4.91 0.01 4.92 6 0.06 ... (0 Replies)
Discussion started by: Zam_1234
0 Replies

3. Shell Programming and Scripting

How to exit a shell script if a unix command does not return any value for 10 seconds?

Hi, Can anyone help me how to exit a shell script if a unix command inside does not return any value for 10 seconds? The scenarios is like this. I want to login to a application using shell script where the connection string is mentioned.but suppose this connection string is not... (10 Replies)
Discussion started by: arijitsaha
10 Replies

4. Shell Programming and Scripting

how to save command into a file in linux

Hi Can anyone suggest how i can save a command into a file. i am trying to execute couple of put commands which i want to write into a file. example below put sample.dat abcd@server.com put sample1.dat abcd@server.com . . . tried cat but no luck Please suggest how i can do... (1 Reply)
Discussion started by: ramkiran77
1 Replies

5. Shell Programming and Scripting

how to save an output of a command in a variable

Hi, in shell script, i have the command swstart -p which returns an output. i want to store the output of this command into a variable. how i can do that excerpt from the script #!/usr/bin/ksh # # # # Program: swstart -p # # Description: Starts the sentinels on Slave server ... (4 Replies)
Discussion started by: lookinginfo
4 Replies

6. UNIX for Dummies Questions & Answers

the command to save session

I remember there is a command you can type and it records everything you type on after it...anybody can help a bit? (2 Replies)
Discussion started by: fedora
2 Replies

7. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

8. Shell Programming and Scripting

return code of a unix command

How to find out whether the command I executed is successful or unsuccessful(at commandlinet) Eg: say i execute the following command at command line rm * How do i find out whether my previous command is a success or failure. Thankyou. Best Regards, Ram. (1 Reply)
Discussion started by: ramky79
1 Replies

9. UNIX for Dummies Questions & Answers

SCO Unix : how to read and save data from cd

Dear All How to read the data on my cd and save these data in a dpecial folder Regards (0 Replies)
Discussion started by: sak900354
0 Replies

10. Shell Programming and Scripting

How to save all the command typed -urgent

hi I want to know how to save all the command used by all the used under a particular root with the time stamp in a file. Eg: User Name: UX10 Time: 10:56 Command: LS User Name: UX23 Time: 10:59 Command: MORE abc.txt Please do help. thanks and regards -Anand (1 Reply)
Discussion started by: anandtharani
1 Replies
Login or Register to Ask a Question