Liniux Shell Scripting Help required!

SilverHood

FH is my second home
Joined
Dec 23, 2003
Messages
2,284
what I need is output to the screen in the following format

NAME
PID TTY TIME CMD

I'm using the ps -U command to print the above. What I'm strugling with is the syntax for nesting the ps command within my w - method.

This is what I have at the moment:

Code:
#!/bin/bash

w | cut -d ' ' -f1  | sort -u | while read userID
   do
       while IFS=: read userIDpasswd junk junk junk userName junk
           do
               if [ "${userID}" = "${userIDpasswd}" ]; then
                 echo "userID->[${userID}] userName->[${userName}]"
                 ps -U [${userName}]
               fi;
           done < /etc/passwd
    done

I can get it to work as a pure command line, but not in my script. I'm totally stumped. Any ideas?
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,804
Hi SilverHood,

you could try a set -x under your shell declaration. this will output what the script is doing al la debugging code

what you could also do, is stick the output from ps into a variable, so you can do manipulations to check what happens.

Code:
#do the command
FOO=`ps -U [${userName}]`
#then check it (assuming the returncode for ps is 0 if all goes well)
if [ $? > 0 ]; echo "an error occured in ps"
#no error occured
else echo "${FOO}"
fi
#done
 

SilverHood

FH is my second home
Joined
Dec 23, 2003
Messages
2,284
Ah, cheers for that.

Edit, its going through the entire passwd file, when it should only be going through the ones online. It never gets to the ps-command for some reason. Is there a syntax error?
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,804
this seems to work on a Solaris system


Code:
#!/bin/bash

w | cut -d ' ' -f1  | sort -u | while read userID
   do
       while IFS=: read userIDpasswd junk junk junk userName junk
           do
               if [ "${userID}" = "${userIDpasswd}" ]; then
                 echo "userID->${userID} userName->${userName}"
                 ps -U ${userID}
               fi;
           done < /etc/passwd
    done

give it a go :)
 

SilverHood

FH is my second home
Joined
Dec 23, 2003
Messages
2,284
some other useful info:

from the etc/passwd file

jasmeet.manik:x:1422:100:Jasmeet Manik:/home/jasmeet.manik:/bin/bash
pascal.morano:x:1423:100:pascal Morano:/home/pascal.morano:/bin/bash
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,804
Nope, don't get any output at all :/



yeah, not all unixen are alike :)


you could try doubling the = sign where you test if userID = userIDpasswd

for me, this already works, but assuming that a shell and all commands work exactly alike on different OE's is just too much. I get this output:

Code:
$ ./a
userID->XXXXXX userName->XXXXXXXXXXXXXXXXXXXXX
   PID TTY      TIME CMD
 17338 pts/15   0:00 bash
 22931 pts/16   0:00 bash
 22900 pts/16   0:00 bash
 22932 pts/16   0:02 ssh2
userID->XXXXXX userName->XXXXXXXXXXXXXXXXXXXXXX
   PID TTY      TIME CMD
 24440 pts/11   0:00 ksh
userID->XXXXXXXX userName->XXXXXXXXXXXXXXXXXXXXXX
   PID TTY      TIME CMD
 26699 pts/10   0:00 a
 26695 pts/10   0:00 a
 26702 pts/10   0:00 ps
 26372 pts/10   0:00 ksh
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,804
you could try a getent passwd <userid> and thus cut out looping through the passwd file directly
 

SilverHood

FH is my second home
Joined
Dec 23, 2003
Messages
2,284
how would that work? sorry, im a total noob at this shell stuff, but I've done PHP and java

Replace the if statement with a getent statement, and then run the ps command using the id collected?
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,804
no, t'other way round. you could try this one:

Code:
#!/bin/bash

w | cut -d ' ' -f1  | sort -u | grep \. | grep -v User | while read userID
   do
       while IFS=: read userIDpasswd junk junk junk userName junk
           do
               if [ "${userID}" == "${userIDpasswd}" ]; then
                 echo "userID->${userID} userName->${userName}"
                 ps -U ${userID}
               fi;
           done < /etc/passwd
    done

the grep -v User and the grep \. cleans out some junk that may have been throwing you off.
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,804
or this one:

Code:
#!/bin/bash
w | cut -d ' ' -f1  | sort -u | grep \. | grep -v User | while read userID
do
        FOO=`getent passwd ${userID}`
        userIDpasswd=`echo ${FOO} | cut -d: -f1`
        if [ "${userID}" = "${userIDpasswd}" ]; then
                userName=`echo ${FOO} | cut -d: -f5`
                echo "userID->${userID} userName->${userName}"
                ps -U ${userID}
        fi;
done


both the above examples work on Solaris
 

anattic

Fledgling Freddie
Joined
Dec 22, 2003
Messages
182
Just an observation: the 'w' command only lists the logged-in users and commands associated with their TTYs. If you're looking for detached (nohup'd) tasks or tasks from lost sessions, you might do better replacing:

Code:
w | cut -d ' ' -f1  | sort -u | grep \. | grep -v User

with

Code:
ps --no-headers -eo user --sort user |uniq
(tested on FC5 Linux)

Admittedly, this does then list all the processes including root, which may be more spammy that you want.
 

SilverHood

FH is my second home
Joined
Dec 23, 2003
Messages
2,284
Nope, still nothing. I'm thinking it may have something to do witht he usernames having "." in them? anyway, this is getting silly, I'll ask one of the guys that runs the training scheme, though unlikely they can help me. I had another friend write a script that works perfectly on ubuntu and gives me the same error as yours when I run it on the red hat server.
 

SilverHood

FH is my second home
Joined
Dec 23, 2003
Messages
2,284
No luck with any of yours TDC

using annatics command, I get the names of people logged in. It's a start. If I can parse that into a ps -U command, it should work
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,804
heh you're getting bitten by the nasty subtle differences between the differing unixen :) for example, anattic's ps command does not work on solaris as he typed it! you could possibly walk through your script line for line on the command line and verify it all works before sticking it together.

however the bad smell here is that you will end up with a script that is tuned to a specific server and not generic in any way, so testing will be required if you're planning to roll it out over all your workstations or something like that.

ofc, another plus would be to do it senior sysadmin stylee with proper sane path settings, sanity checks and command error checking with debugging in place to make sure all goes well. still, takes so much time eh ;)
 

anattic

Fledgling Freddie
Joined
Dec 22, 2003
Messages
182
I think that's just Solaris being ornery tbh ;) But you have a point. If I'm writing scripts (especially like these ones in areas of the OS that vary from one platform to another), I tend to write them in Perl. These days, it's generally already installed, and usually someone else has already fixed the incompatibilities before I get there. :)

Something similar in Perl (that works on FC5 and Solaris 10):

Code:
#!/usr/bin/perl -w

my @psnames = `ps -e -o user`;
my %actnames;

for( $i = 0; $i <= $#psnames; $i ++ )
{
  $name = $psnames[$i];
  $name =~ s/\s+//g;

  if ( $name ne "USER" and not defined $actnames{$name} )
  {
    $actnames{$name} = 1;
    $ps = `ps -u $name`;

    @ent = getpwnam $name;
    printf( "Name: %s   Fullname: %s\n%s\n", $name, $ent[6], $ps );
  }
}
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,804
haha yeah I was just doing the same in shell:


Code:
#!/bin/bash
ps -e -o user | grep -v USER | uniq -u | sort -u | while read userID
do
        FOO=`getent passwd ${userID}`
        userIDpasswd=`echo ${FOO} | cut -d: -f1`
        if [ "${userID}" = "${userIDpasswd}" ]; then
                userName=`echo ${FOO} | cut -d: -f5`
                echo "userID->${userID} userName->${userName}"
                ps -U ${userID}
        fi;
done
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,804
I think that's just Solaris being ornery tbh ;) But you have a point. If I'm writing scripts (especially like these ones in areas of the OS that vary from one platform to another), I tend to write them in Perl. These days, it's generally already installed, and usually someone else has already fixed the incompatibilities before I get there. :)


certainly, and the more platforms you support the better you have to look out hehe. you prolly have a little codeblock that you stick in all your scripts that standardizes your proggys and options with a little test to see what you're running on. me, my stuff is either Solaris 8 or Solaris 10 (with a few 9's in there somewhere to keep me on my toes I admit) and even that can drive me batshit from time to time tbh :/
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,804
woot, glad it works now matey :)
 

SilverHood

FH is my second home
Joined
Dec 23, 2003
Messages
2,284
Ok, another problem. :)
One of my scripts uses SED, which isn't installed on the target machine. So I need to write one out the long way.

It's pretty simple. I can do it in java and PHP, but shell scripting is not my forte, as you've probably worked out by now. :confused:

Code:
echo "Enter file name: "
read file
echo "          READ WRITE EXECUTE"
echo "          ----  -----  -------"
getfacl "${file}" | while IFS=':' read what junk perm
do
   case "${what}" in
  user|group|other)
     echo "${what} $(echo ${perm} | sed -e 's/[rwx]/ YES /g' -e 's/-/ NO /g')"
   esac
done

So how the hell do I do this without SED?

I assume I build an if structure around the $perm variable. Simply removing the code in the SED line brings the output to this:

Code:
          READ WRITE EXECUTE
          ---- ----- -------
user rwx
group rwx
other rwx

The rwx is stored in the $perm variable. However, this is where I run into trouble. I get binary or unary operater errors when trying to manipulate it. For example, if I try to compare $perm with the string rwx I get an error. If I try to compare it with 777 I get yet another error. Any ideas?
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,804
no sed installed? are you mad? :eek:
 

SilverHood

FH is my second home
Joined
Dec 23, 2003
Messages
2,284
It's ok, got it sorted. Had to stick the perm variable into a string variable and then compare it. Can't wait to get back to java, a real crossplatofrm language :)
 

Users who are viewing this thread

Top Bottom