Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am writing a script to install a program with Bash. I want to exit on error so I added set -e to the beginning of my script.

I have to check if a user exists inside of my script. To do this I am using grep ^${USER}: /etc/passwd. If the user exists, the script runs normally. However, if the user doesn't exist, this command exits. I don't want to exit should the latter case occur. Instead, I want to create the user and continue my installation.

What's the solution to make my script continue running? I tried to redirect the output of grep to a variable, but I still have the same problem.

share|improve this question

4 Answers 4

up vote 5 down vote accepted

You could always do something like below. Basically disabling the exit checking around the command you are running.

#!/bin/bash
set -e # eanble exit checking
lorem ipsum 
dolor sit amet
# temp disable exit checking
set +e
grep "^${USER}:" /etc/passwd
set -e
consectetur adipiscing elit
share|improve this answer
    
yes, it's working :) thanks so much, i didn't know that i can disable "set -e" at any place, thanks again Zoredache, problem solved :) –  Dahmad Boutfounast Apr 5 '12 at 16:46

In addition to the suggestions already given, here are two more choices. First, you can provide an alternate command that'll guarantee success:

grep "^${USER}:" /etc/passwd || true

(Basically, if the grep command fails, this'll run true and since that always succeeds it'll consider the compound command to have succeeded and won't exit the script.)

Alternately, since your interest is whether the grep succeeds (i.e. whether you need to add the user), just use it as the condition in an if statement:

if ! grep "^${USER}:" /etc/passwd; then
    # Create the user account
fi
# Continue installation...

(Note that the ! before the grep command negates the test, so it runs the body of the if statement only if grep fails. Since grep is part of a compound command, the script doesn't exit if it fails.)

share|improve this answer
    
excellent idea, thanks for this informations, it's working too :) –  Dahmad Boutfounast Apr 6 '12 at 9:53

The solution is to not use set -e which can be quite dangerous. Use if statements where appropriate instead.

grep "^${USER}:" /etc/passwd &>/dev/null
if [ $? -eq 1 ]
then
    #create user here ...
ft
share|improve this answer
    
no i can't, i am installing a program with (configure, make, ...), so i have to exit if there's an error at this level. thanks anyway, problem solved, see next answer :) –  Dahmad Boutfounast Apr 5 '12 at 16:46

one suggestion is to run that check and install in a subshell within (). would that work for you?

share|improve this answer
    
This is the best way, in my opinion –  Eran Ben-Natan Apr 8 '12 at 5:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.