Home > Mobile >  if with exit 0 & 1 and showing process
if with exit 0 & 1 and showing process

Time:08-22

I want to solve a certain task with a bash script which I pass to a parameter, but unfortunately I don't get anywhere.

This is what it is about.

If a user exists, the exit status 0 should be returned and displayed. If this user does not exist then 1. This works fine so far. Now it is so that if the user should exist and has running processes, only the processes should be displayed. Otherwise only the exit status 0 if the user exists and has no running processes.

#!/bin/bash                                                                                                                                                                                                                                                           
user=$1                                                                                                                     
exists=$(grep -c $user /etc/passwd)                                                                                                                                                                                                                            

if [ "$exists" -ne 0 ]; then                                                                                                               
   echo $?                                                                                                                                                                                                                                                                                                                             
else 
  echo $?                                                                                                                    
fi   

I added an additional elif, but that did not work. How can I customize the script to show me the running processes for an existing user if they should exist or if no running processes exist but the user exists only returns and displays the status 0 or 1?

Thanks a lot!

CodePudding user response:

grep returns the exit status you are looking for, so you probably just want:

#!/bin/bash                                                                                                                                                                                                                                                           
user=$1                                                                                                                     
if grep -q "$user" /etc/passwd; then
    echo 0; 
    exit 0;
else
    echo 1;
    exit 1;
fi 

But that's quite redundant and could be more easily written:

grep -q "$user" /etc/passwd
rv=$?
echo "$rv"
exit "$rv"
                                                                                                                                                                                                                     

However, the requirement to print the return status is odd, and if you simply drop that, your script can just be the grep.

Note that this completely disregards that fact that grepping /etc/password for the user name is not an adequate test to determine if the user exists, but that does not seem to be the heart of this question.

CodePudding user response:

Th id command can tell if user exists, without need to tap the passwd file, and works regardless of what resource type is providing the users database to the system.

#!/bin/sh

if id --user "$1" > /dev/null 2>&1;
then
  ps --user "$1" --format pid=,comm= || :
  echo 0
else
  echo 1
  false
fi

Or a one-liner:

id -u "$1">/dev/null 2>&1&&{ ps -u "$1" -o pid=,comm=||:;}
  • Related