How to know the status of a Command?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to know the status of a Command?
# 1  
Old 01-12-2010
How to know the status of a Command?

Hi

i have done a copy operation (Storing one content of a file to another)

For example :

cp Fileone Filetwo

Now is it possible to know the status of the execution of that command ??
Without manually checking the Filetwo contents ??

Thanks in advance .
# 2  
Old 01-12-2010
Every UNIX process gives a return code. Zero means it executed successfully, nonzero means an error of some sort occurred. What nonzero codes mean can be program-specific but nonzero is always supposed to mean an error of some sort.

You can use these codes from shell easily enough:

Code:
touch a
touch b
# cause copy to fail by preventing it writing to b
chmod 000 b

if cp a b
then
        echo "Copied file"
else
        echo "File not copied!"
fi

Or the rather terse way of using boolean operators:
Code:
function die
{
        echo "$@" >&2
        exit 1
}

cp a b || die "Couldn't copy file!"

# 3  
Old 01-12-2010
thanks



Assume that , on to the terminal i have executed a copy operation , how can check the status ??
# 4  
Old 01-12-2010
$? should hold the value of whether it was successful. 0 = success, anything else = failed.

So immediately after your command, you can echo back $?.

Code:
cp file1 file2
if [ $? -eq 0 ]
then
  echo "Copy was successful"
else
  echo "Copy was unsuccessful"
fi

# 5  
Old 01-12-2010
A slight variation.

Code:
$ cp file1 file2
$ echo $?

Ref: Exit and Exit Status

Code:
Every command return exit status (sometimes referred to as a return status or exit code). A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code. Well-behaved UNIX commands

# 6  
Old 01-13-2010
the best and the simplest solution is

$ cp file1 file2
$ echo $?

where "echo $?" will print 0 if the command is successful or else it will print non 0 value.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exit Status of Command

Hi All, I am doing an export and import (datapump) of 4 schema. I know we can do an export of 4 schema in one command. But just want to know how to check the exit status if i do the export/import of 4 schema in different commands in background. Please suggest. Thanks, Mani (1 Reply)
Discussion started by: pvmanikandan
1 Replies

2. UNIX for Dummies Questions & Answers

Exit Status Of Find Command

Hello All, I am trying to capture the exit status of find command and want to delete the files only when it is successful. But it is always returning me as success even if the pattern of that file doesn't exist in the current directory. please help, checked manual page but couldn't able to figure... (6 Replies)
Discussion started by: Ariean
6 Replies

3. UNIX for Dummies Questions & Answers

Check the exist status of the cd command

Hi, As in scripting , some cd commands getting failed, so we do check the exist status as 0 or 1. But every time we call to function for it. But does any single line exist will do the job with || , && ? i.e ls -l Logs cd Logss | exit echo hias Logss is not exist , Before printing "hi" we... (5 Replies)
Discussion started by: posix
5 Replies

4. UNIX for Advanced & Expert Users

Equivalents of tee command to find exit status of command

Hi, Want to log the output of command & check the exit status to find whether it succeeded or failed. > ls abc ls: abc: No such file or directory > echo $? 1 > ls abc 2>&1 | tee log ls: abc: No such file or directory > echo $? 0 Tee commands changes my exit status to be always... (7 Replies)
Discussion started by: vibhor_agarwali
7 Replies

5. Shell Programming and Scripting

last command status problem

Hi there is below code in my code , i did not understand what does it mean RC=0 RC=$(( RC | $? )) Please help me regarding this ---------- Post updated at 02:22 AM ---------- Previous update was at 01:46 AM ---------- Below is more example of the code (2 Replies)
Discussion started by: aishsimplesweet
2 Replies

6. Shell Programming and Scripting

returning status of a command

Hello gurus, Tired of using $? to view the execution status of the previous command. how can I get the status directly? definitely this is not working, if ] then fi (6 Replies)
Discussion started by: biglau
6 Replies

7. AIX

Tape status command

We sometime run backups to tape in the background using the cron. Is there a command that will show me how fast the information is being written and or how much data has been written? I know if we enter the information via the command prompt or smit it shows the speed and amount written. (2 Replies)
Discussion started by: daveisme
2 Replies

8. Shell Programming and Scripting

Getting the exit status of a remote command

Hi to everyone. How can I get the exit status from a remote command executed with rexec? :eek: machine A has RedHat Linux 9 and the remote machine B has SCO UNIX. Code: rexec -l user -p password host sh /u/files/scripts/seq_cal.sh 2006 08 I want the exit status returned by... (1 Reply)
Discussion started by: zoonalex
1 Replies

9. Shell Programming and Scripting

Status code of last command execution

Hello people, I am using KSH and inside my script I do a cksum and check the status code to see whether the command sucessfully. Here even though the cksum fails due to file not existing the status returned is still 0 because the awk command worked fine. How do I capture just the status of the... (1 Reply)
Discussion started by: tipsy
1 Replies

10. Shell Programming and Scripting

Status of the second to last command

Below is a chuck of a shell script I have written. The problem I am having is with the test section. I want to test the status of the f60gen command but since I'm running the output of the f60gen command into tee, the test command is actually testing "tee" and not "f60gen". Is there a way to... (5 Replies)
Discussion started by: goswell
5 Replies
Login or Register to Ask a Question