linux - crash shell: any way to check if a command executed Successfully

06
2014-04
  • limovala

    I am automating a test using crash package.

    is there some thing like $? in bash shell, which can be used to check if previous command is executed success fully or not.

    eg : when command

    crash> bt -a
    

    is executed, how to know if it was successfull or not.
    only solution in my mind is greping through the result
    But its not reliable when the enviornmentchanges.

    this is what i get when I try echo $?

    crash> bt asd
    bt: invalid task or pid value: asd
    crash> $?
    crash: command not found: $?
    crash> echo $?
    0
    crash>
    crash> q
    root@at0012-ubuntu:~/crashKernel# 
    
  • Answers
    Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

    Related Question

    process - Python: Check existence of shell command before execution
  • Gabriel L. Oliveira

    I'm trying to find a way to check the existence of a shell command before its execution.

    For example, I'll execute the command ack-grep. So, I'm trying to do:

    import subprocess
    from subprocess import PIPE

    cmd_grep = subprocess.Popen(["ack-grep", "--no-color", "--max-count=1", "--no-group", "def run_main", "../cgedit/"], stdout=PIPE, stderr=PIPE)

    Than, if I execute

    cmd_grep.stderr.read()

    I receive '' like the output. But I don't have the command ack-grep on my path. So, why Popen is not putting the error message on my .stderr variable?

    Also, is there a easyer way to do what I'm trying to do?


  • Related Answers
  • Studer

    You can use the subprocess module under Python 3 or the commands module for Python 2 as follow :

    status, result = subprocess.getstatusoutput("ls -al")

    status, result = commands.getstatusoutput("ls -al")

    Then test the value of status.

    Examples from the website :

    >>> import subprocess
    >>> subprocess.getstatusoutput('ls /bin/ls')
    (0, '/bin/ls')
    >>> subprocess.getstatusoutput('cat /bin/junk')
    (256, 'cat: /bin/junk: No such file or directory')
    >>> subprocess.getstatusoutput('/bin/junk')
    (256, 'sh: /bin/junk: not found')
    
  • djmc

    couldn't you use the "which" command somehow? the which command automatically performs a lookup for an application in the paths. I think you would merely need to call this command and pass the name of the command you want to look up, then parse the results.

  • Gabriel L. Oliveira

    I finally left it working this way:

    try:

        cmd_grep = ["ack-grep", "--no-color", "--max-count=1", "--no-group", function_definition, file_path]
    
        first_exec = Popen(cmd_grep,stdout=PIPE)
    
        execution = Popen(cmd_sed, shell=True, stdin=first_exec.stdout, stdout=PIPE)
    
    except:
    
        #use grep instead
    
        cmd_grep = cmd_grep = r'grep -R -n "' + function_definition + '" ' + file_path
    
        execution = Popen(cmd_grep + '|' + cmd_sed,shell=True,stdout=PIPE)
    
    output = execution.stdout.read()
    
  • Jeet

    For situations like this, I use:

    def find_program(prog_filename, error_on_missing=False):
        bdirs = ['$HOME/Environment/local/bin/',
                 '$HOME/bin/',
                 '/share/apps/bin/',
                 '/usr/local/bin/',
                 '/usr/bin/']
        paths_tried = []
        for d in bdirs:
            p = os.path.expandvars(os.path.join(d, prog_filename))
            paths_tried.append(p)
            if os.path.exists(p):
                return p
        if error_on_missing:
            raise Exception("*** ERROR: '%s' not found on:\n  %s\n" % (prog_filename, "\n  ".join(paths_tried)))
        else:
            return None
    

    Then you can do something like:

    grep_path = find_program('ack_grep', False)
    if grep_path is None:
        # default to standard system grep
        grep_path = 'grep'