returning status of a command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting returning status of a command
# 1  
Old 07-12-2010
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,

Code:
if [[ `grep pattern file_name` -eq 0 ]]
then
fi


Last edited by pludi; 07-12-2010 at 03:34 AM.. Reason: code tags, please..
# 2  
Old 07-12-2010
This should work
Code:
if grep -q pattern file_name
then
fi

or
Code:
if grep pattern file_name >/dev/null 2>&1
then
fi

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-12-2010
yup, but is there a way to get a general commond/function execution status(returning number) withcout using $?

and btw, what if I use the dual square brackets, it only check if the returning value is null or non-null, rather than 0 or non-0.

So how should I get the $? directly in the if [[ ]] ?

Thank you all.

Last edited by biglau; 07-12-2010 at 04:40 AM..
# 4  
Old 07-12-2010
#grep pattern file_name
#echo $?
# 5  
Old 07-12-2010
Assuming we have a file called "file_which_does_exist" and we do not have a file called "file_which_does_not_exist" and we are looking for a string "string". There are three possible reply codes.

Code:
grep -qs "string" file_which_does_not_exist; REPLY="$?"
echo "${REPLY}"
2

grep -qs "rubbish" file_which_does_exist; REPLY="$?"
echo "${REPLY}"
1

grep -qs "string" file_which_does_exist; REPLY="$?"
echo "${REPLY}"
0

This User Gave Thanks to methyl For This Post:
# 6  
Old 07-19-2010
You are right, seems I have to write a customized function to get the returning status, like

Code:
function rt_status()
{
  eval "$1"
  return $?
}

then I can use the function like the following
Code:
if [[ rt_status "grep pattern file_name" -eq 0 ]]
then
    echo "pattern matched"
elif [[ rt_status "grep pattern file_name" -eq 1 ]]
then
    echo "Pattern unmatched"
elif [[ rt_status "grep pattern file_name" -eq 2 ]]
then
    echo "file_name not existed"
fi

See, just want to know if there is a system built-in function to replace my rt_status function to return the status of the execution for a command? I am not willing to fetch that status by $?

Last edited by Scott; 07-21-2010 at 03:15 AM.. Reason: Code tags...
This User Gave Thanks to biglau For This Post:
# 7  
Old 07-21-2010
I do not see any advantage in this. You have seen my example above without the use of $?. Why is that not usable for you? Also, why do you find the use of $? objectionable anyway?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

RHEL6 : Remote process exited without returning status

Hi All, I am using RHEL 6 linux in my lab server and I am new to RHEL6 features. lab1:root> uname -a Linux lab1 2.6.32-358.18.1.el6.x86_64 #1 SMP Fri Aug 2 17:04:38 EDT 2013 x86_64 x86_64 x86_64 GNU/Linux I am facing a peculiar issue in my lab machine. When I connected via Opsware Global... (0 Replies)
Discussion started by: go2suresh1979
0 Replies

2. 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

3. UNIX for Dummies Questions & Answers

Find command returning bad status--

would like to remove the post (8 Replies)
Discussion started by: vk39221
8 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. UNIX for Dummies Questions & Answers

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 . (5 Replies)
Discussion started by: Ravi Pavanv
5 Replies

7. UNIX for Advanced & Expert Users

ps | grep command not returning existing process

Hello all, I develop an ERP application that runs on a number of *nix flavors. This application had been around for quite a number of years. As part of the application launch, we do a check for a background process using the following command: ps -ef | grep -v grep | grep mi\/ba | grep... (6 Replies)
Discussion started by: jlitzie
6 Replies

8. 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

9. UNIX for Advanced & Expert Users

find command not returning any result

I am looking for all the header files (*.h).. which as per documentation of the UNIX system shouldbe there. I am using find / -name *.h -print But it does't give anything. My question is under what condition the "find" condition will fail to find the file? What is the work around. ... (4 Replies)
Discussion started by: rraajjiibb
4 Replies

10. UNIX for Dummies Questions & Answers

SED Command Returning a Blank File

I have a file called get_columns.sh that I would like to replace $2 with $3. I have the following sed command written that works sed s/'$3'/'$2'/g get_columns.sh > output.txt However, when I try the command below, the file is empty. sed s/'$3'/'$2'/g get_columns.sh > get_columns.sh ... (2 Replies)
Discussion started by: djschmitt
2 Replies
Login or Register to Ask a Question