bash - Validating return code 0


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash - Validating return code 0
# 1  
Old 11-16-2017
bash - Validating return code 0

Hi All, I am trying a script out that will startup on one of my servers. i wanted to check for RC 0 and if it didnt check out, exit.

Typo- This is BASH (Redhat)

This isnt working, and this is the best way to do error checking I feel. Heres my erorr

Code:
./start: line 25: syntax error near unexpected token `elif'
./start: line 25: `     elif [ $? -eq 0]'

heres that part of the script. Can this be done differently and more efficiently? Thanks in advance.

Code:
echo "$NOW Starting process"
if ps ax | grep -v grep | grep $process > /dev/null
then
    echo "$NOW service is already running"
else
    echo "$NOW  service is not running running, starting.."
    sudo /opt/path/to/code
    echo "$NOW $process service has been started"

    elif [ $? -eq 0]
    then
        echo "$NOW service started successfully!"
    else
        echo "$NOW service did not properly initialize! Exiting"
        exit 1
    fi
fi


Last edited by RudiC; 11-16-2017 at 01:37 PM.. Reason: Corrected typo (ksh - bash) by moving thread
# 2  
Old 11-16-2017
I want to say there should be a space before the ]

Code:
elif [ $? -eq 0]

Should be:
Code:
elif [ $? -eq 0 ]

# 3  
Old 11-16-2017
Also, you cannot use elif without a preceding if statement. Presumably you meant if, rather than elif ?

Plus:
Code:
echo "$NOW $process service has been started"

will always give exit status 0 so you cannot test the preceding statement there. You either need to put the exit status in a variable, or put the test right behind the command..

Last edited by Scrutinizer; 11-16-2017 at 01:04 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[bash] wanted: function with a clean way for multiple return values

Hi, I have a small part of a project which is done as a bash script. bash was selected as an portability issue that works out of the box. In this script I have an exec shell-function, a wrapper around arbitrary commands. I want to have STDOUT, as an addon STDERR and the EXIT-CODE of a specified... (5 Replies)
Discussion started by: stomp
5 Replies

2. Shell Programming and Scripting

Expect in bash to get the return value

cat test.sh #!/bin/sh expect <<- EOF set timeout 5 spawn ssh -o StrictHostKeyChecking=no lyang0@128.224.178.245 -C mkdir -p /tmp expect { "Password:" {send "root\r"} } spawn scp -o StrictHostKeyChecking=no /tmp/1 lyang0@128.224.178.245:/tmp/ ... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

3. Shell Programming and Scripting

Avoid carriage return until ^M is found (CentOS 6, bash 4.1)

Hi everyone, I have the following contents in a text file (as seen when viewed using vim): one two three ^M four five six ^M seven eight nine ^M ten eleven twelve ^M (That is just a small portion of the file) How can I obtain the following result? one two three ^M four five six ^M seven... (2 Replies)
Discussion started by: gacanepa
2 Replies

4. Shell Programming and Scripting

Suppressing carriage return in bash alias

I'd like to create an alias that displays my string but leaves my cursor at the end. Not seeing any examples of this. One indirect way might be to preload or stuff the history buffer, so I just hit up arrow. (2 Replies)
Discussion started by: tns1
2 Replies

5. Shell Programming and Scripting

Bash - multiple line carriage return

Hello! I have one strange question - let's say I have a long, multiple-line string displayed on the terminal using echo, and I would like to make a carriage return to the beginning of this string, no to the beginning of the last line - is something like that possible? I would like to be able to... (1 Reply)
Discussion started by: xqwzts
1 Replies

6. Shell Programming and Scripting

Search an array and return index (bash)

Hi all, In bash, is there any way of searching an array and returning the index? For example, how could I write a script that would do the following: >> search note_array=(C D E F G A B) for F return the value 3 (or 4) Thanks, R (5 Replies)
Discussion started by: RMontenegro
5 Replies

7. Shell Programming and Scripting

return part of a string in bash

Hi, I would like to return the last part of a string in an array of strings in bash. The array contains in each position the content below: and I would like to return only the last part of each string. The correct result would be: How can I do that in bash using AWK?... (7 Replies)
Discussion started by: anishkumarv
7 Replies

8. Shell Programming and Scripting

How to enter a return key in bash script?

Hi, I'm porting an install script from AIX to Red Hat (2.6.18-164.el5 #1 SMP) I have this script working in both AIX and HP-UX. The script is a wrapper for a Micro Focus Server Express install program. It responds to the install program questions with a here-now list. Responses includes... (14 Replies)
Discussion started by: duker61
14 Replies

9. Shell Programming and Scripting

Return name of running WM in bash?

While I am by habit comfortable with bash as a shell, from time to time, I find tiny reasons to believe it's a little dense. One of the things that proves it is: to date, I have not been able to get it to identify what WM it's running inside of or alongside of in an X11 kind of environment... (1 Reply)
Discussion started by: SilversleevesX
1 Replies

10. 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
Login or Register to Ask a Question