Find the answer to your Linux question:
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 | ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    You're close, but I'd change it up a little:
    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 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.

    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.

  3. #3
    Just 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!

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    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):
    Code:
     If [ "$user" ]
    you should really make that:
    Code:
    if [ -n "$user" ]
    And when using the equals sign to compare, you should use double-equals, e.g.:
    Code:
    [ "$x" == '1' ]
    Single equals should only be used for assigning values.

    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.

  5. #5
    Just 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •