if with exit 0 & 1 and showing process - linux

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!

The 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= || :
else
false
fi
Or a one-liner:
sh -c 'id -u "$1">/dev/null 2>&1&&{ ps -u "$1" -o pid=,comm=||:;}' sh username

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.

Related

For each parameters, script checks if there exists a user of such username. If exists script prints all processes run by this user

#!/bin/bash
if [ $# -ne 1 ]
then
echo "Script Should Have Atleast 1 Parameter"
exit
fi
USER=$1
echo $USER
if [ $? -eq 0 ]
then
echo " Yes the User Exists"
else
echo "No , The User Doesnt Exists"
fi
My problem here is whatever the input i give, it shows yes the user exists.
And can anyone suggest me a command to print out all the process runs by this user
Should i use awk , or grep
Well, first of all, you are not checking if user exists, but ultimately if command echo $USER didn't fail. You will need to implement proper user check.
Good way how to check if arbitrary user exists would be something like this, using command id.
if id "${1}" &> /dev/null; then
echo 'user found'
else
echo 'user not found'
fi
Next you want to list all processes belonging to this user. You will need ps. There are many ways how ps can format output. Be sure to check manual page of ps. Here is example which lists command and pid of all processes belonging to user in "$1".
ps -eo pid,comm --user $(id -u "${1}")
At last, running check using id and only then using ps for process list is inefficient at least. In your use-case, you can simply run ps and check if it was successful. If not, you can show warning that user was not found.
#!/bin/bash
if ! ps -u "$1" 2> /dev/null; then
echo 'user not exists!'
exit 1
fi

ssh to different nodes using shell scripting

I am using below code to ssh to different nodes and find if an user exists or not. If the user doesn't exist it will create it.
The script works fine if I don't do ssh but it fails if I do ssh.
How can I go through different nodes using this script?
for node in `nodes.txt`
usr=root
ssh $usr#$node
do
if [ $(id -u) -eq 0 ]; then
read -p "Enter username : " username
read -s -p "Enter password : " password
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "F
ailed to add a user!"
fi
else
echo "Only root may add a user to the system"
exit 2
fi
done
Your script has grave syntax errors. I guess the for loop at the beginning is what you attempted to add but you totally broke the script in the process.
The syntax for looping over lines in a file is
while read -r line; do
.... # loop over "$line"
done <nodes.txt
(or marginally for line in $(cat nodes.txt); do ... but this has multiple issues; see http://mywiki.wooledge.org/DontReadLinesWithFor for details).
If the intent is to actually run the remainder of the script in the ssh you need to pass it to the ssh command. Something like this:
while read -r node; do
read -p "Enter user name: " username
read -p -s "Enter password: "
ssh root#"$node" "
# Note addition of -q option and trailing :
egrep -q '^$username:' /etc/passwd ||
useradd -m -p \"\$(perl -e 'print crypt(\$ARGV[0], \"password\")' \"$password\")" '$username'" </dev/null
done <nodes.txt
Granted, the command you pass to ssh can be arbitrarily complex, but you will want to avoid doing interactive I/O inside a root-privileged remote script, and generally make sure the remote command is as quiet and robust as possible.
The anti-pattern command; if [ $? -eq 0 ]; then ... is clumsy but very common. The purpose of if is to run a command and examine its result code, so this is better and more idiomatically written if command; then ... (which can be even more succinctly written command && ... or ! command || ... if you only need the then or the else part, respectively, of the full long-hand if/then/else structure).
Maybe you should only do the remote tasks via ssh. All the rest runs local.
ssh $user#$node egrep "^$username" /etc/passwd >/dev/null
and
ssh $user#$node useradd -m -p $pass $username
It might also be better to ask for username and password outside of the loop if you want to create the same user on all nodes.

get return value of command run with script -c

Is there a way to capture the return value of a program run using script -c?
For example (in bash)
/bin/false; echo $? # outputs 1
/usr/bin/script -c "/bin/false" /dev/null; echo $?
# outputs 0 as script exited successfully.
I need to get the return value from /bin/false instead of from /usr/bin/script.. is this possible? I'm using script to trick a program into thinking it is running in a real tty even though it isn't... Thanks!
According to man script, using -e option will return the exit code of the child process.
-e, --return
Return the exit code of the child process.
Uses the same format as bash termination
on signal termination exit code is 128+n.
Here's some example.
$ /usr/bin/script -e -c "/bin/false" /dev/null; echo $?
1
$ /usr/bin/script -e -c "/bin/true" /dev/null; echo $?
0
$ /usr/bin/script -e -c "exit 123" /dev/null; echo $?
123

How do I check if a man page exists?

I'm trying to write a script to convert man pages to PDF files. The script I have right now is:
#! /usr/bin/env bash
[[ $# -ne 1 ]] && { echo "Usage: $(basename $0) [command]" ; exit 1 ; }
man -t ${1} | ps2pdf14 - > "${1}_man.pdf"
The problem is that if a man page does not exist, the script will still carry on executing, generating an empty PDF file. So I wonder if there's a way to determine if a man page exists?
Use Man's Exit Status from a Function
The man(1) manual page defines the following exit status codes:
EXIT STATUS
0 Successful program execution.
1 Usage, syntax or configuration file error.
2 Operational error.
3 A child process returned a non-zero exit status.
16 At least one of the pages/files/keywords didn't exist or wasn't
matched.
That means you can use the exit status of man itself to determine if a page is available via manpath. For example:
check_for_man_page () {
man "$1" > /dev/null 2>&1
}
With this function, you can use test conditionals on the exit status like so:
$ check_for_man_page "cat" && echo 'Found it!'
Found it!
$ check_for_man_page "quux" || echo 'Not found!'
Not found!
Using an If/Else Statement
Here's another way to do this, using an if/else statement to determine whether to run your original code:
if man "$1" > /dev/null 2>&1
then
man -t ${1} | ps2pdf14 - > "${1}_man.pdf"
else
echo "Missing man page: $1" >&2
fi
It's not neat, but this seems to do what you want:
if man -w ${1} >/dev/null 2>/dev/null ; then
man -t ${1} | ps2pdf14 - > "${1}_man.pdf"
fi
However, some man pages may exist in different sections of the manual -- for example man 1 printf and man 3 printf. You may wish to modify your script to take this into account.

bash: fail if script is not being run by root

I have a bash script that installs some software. I want to fail as soon as possible if it is not being run by root. How can I do that?
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
Source: http://www.cyberciti.biz/tips/shell-root-user-check-script.html
After digging around on this, the consensus seems to be that there is no need to use id -u in bash, as the EUID (effective user id) variable will be set. As opposed to UID, the EUID will be 0 when the user is root or using sudo. Apparently, this is around 100 times faster than running id -u:
#!/bin/bash
if (( EUID != 0 )); then
echo "You must be root to do this." 1>&2
exit 1
fi
Source: https://askubuntu.com/questions/30148/how-can-i-determine-whether-a-shellscript-runs-as-root-or-not

Resources