Results 1 to 5 of 5
Scripts Purpose: Display all processes running for specific users, if NO username is entered then all processes will be displayed
So far i have
Code:
#!/bin/bash
x=1
check="cat /etc/passwd | ...
- 10-10-2011 #1Just Joined!
- Join Date
- Sep 2011
- Posts
- 3
Help a NOOB with IF/while conditioning
Scripts Purpose: Display all processes running for specific users, if NO username is entered then all processes will be displayed
So far i have
Code:#!/bin/bash x=1 check="cat /etc/passwd | cut -f1 –d “:” | grep $user” echo –n “enter a username or press enter for all processes ‘user’:” read user until [ “$x” = 1 ] do if [ “$user” = “$check” ] then ps aux | grep $user | less x=1 else if [ “$user” = “” ] then ps aux | less x=1 else echo “The username is incorrect” fi fi done
I THINK there is a problem with $check comparing it to $user?
I need it to compare the name types in to current users incase of a mismatch.Last edited by MikeTbob; 10-10-2011 at 02:23 PM. Reason: Added Code Tags
- 10-10-2011 #2Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
You're close, but I'd change it up a little:
If a username is entered that is wrong (i.e., does not appear in /etc/passwd), then the script will continue to prompt for a valid username until one is received. This can be easily changed to abort the script, if you want.Code:#!/bin/bash users=$(cat /etc/passwd|cut -f1 -d:) printf "$users\n" until [ -n "$user_ok" ]; do echo -en "\nEnter a username or press enter for all processes: " read user if [ -n "$user" ]; then for check in $users; do if [ "$check" == "$user" ]; then user_ok=1 break fi done if [ -z "$user_ok" ]; then echo "The username is incorrect" unset user fi else user_ok=1 fi done if [ -n "$user" ]; then ps -f -u $user|less else ps -ef|less fi
If no name is entered (i.e., they hit [Enter]) then that is considered a valid response, too.
The format of the ps command at the end can be changed, too. That is just one example of how to customize the ps output.
- 10-11-2011 #3Just Joined!
- Join Date
- Sep 2011
- Posts
- 3
Hey thanks for the reply.
I should of taken a break for a while but i ended up getting it to work with
Code:#!/bin/bash until [ "$x" = 1 ] do clear read -p "Enter Username: " user If [ "$user" ] then check=$(egrep -w $user /etc/passwd | cut -f1 -d":") if [ "$user" = "$check" ] then ps aux | $user | less x=1 else read -p "username does not exist press ENTER]to continue" fi else ps aux | less x=1 fi done
I was just over thinking it but you are right i should be listing the content/users 1st so they know what to choose from. If you've got any tips in writing/modifying/layout of scripts im all ears.
Thanks!
- 10-11-2011 #4Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
Ah, I misunderstood what your check command was doing before. I thought you initially wanted to display all potential users. Yeah, now I see...
Btw, the reason I did the ps -f -u $user was b/c that way only shows processes owned by said $user. Running ps auxw|grep $user shows all processes that happen to match an occurrence of $user in the output. It probably would show the exact same processes, but the difference is worth mentioning.
On this line of your code (i assume you know the If is a typo):
you should really make that:Code:If [ "$user" ]
And when using the equals sign to compare, you should use double-equals, e.g.:Code:if [ -n "$user" ]
Single equals should only be used for assigning values.Code:[ "$x" == '1' ]
As for other tips, I'd recommend checking out the scripts in /etc/init.d/. They are a good repository of layout ideas and examples of esoteric bash functions and what not in action.
- 10-13-2011 #5Just Joined!
- Join Date
- Sep 2011
- Posts
- 3
Thanks so much for the help this script does exactly what i need it to do now.
Thanks for the tips to iii keep them in mind


Reply With Quote