Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

substr Rampage

by RobinVossen (Initiate)
on Sep 17, 2007 at 09:24 UTC ( #639367=perlquestion: print w/ replies, xml ) Need Help??
RobinVossen has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am new here and I am also new to Perl. Perl is a cool language I have to say. BUT.. its doing something crazy.. With my script.. When I want to use substr($record, $FifthCollumEnds, $SixthCollumEnds-1); it freaks out.. I need to read the Home dir from the file but he reads homedir + the Bash.. :/ Anywho.. here is my script.. I know its still hidious and has lots of comments.. But well:
#!/usr/local/bin/perl #Show all users in table in the Following format: #Username | EnabledIcons | <Enabled//Disabled Status> | <edit button> #On the Button put Button Change Global Settings print "content-type: text/html <head><title>iDesk Webmin Module</title></head> <body> <TABLE border=3> <TR> <TD><strong>Username</strong></TD><TD><strong> +Enabled Icons</strong></TD><TD><strong>Status</strong></TD><TD><stron +g>Running</strong></TD><TD><strong>Edit</strong></TD></TR>"; #Read File /etc/passwd open (PASSWDFILE, "//etc//passwd"); while ($record = <PASSWDFILE>) { #Filter out everyone from UID 1000 of Bigger #Thats the 3th Collum and set %HOMEDIR% to the 6th Collum $FirstCollumEnds = index($record,":",0) + 1; $SecondCollumEnds = index($record,":",$FirstCollumEnds) +1; $ThirthCollumEnds = index($record,":",$SecondCollumEnds)+1; $FourthCollumEnds = index($record,":",$ThirthCollumEnds)+1; $FifthCollumEnds = index($record,":",$FourthCollumEnds)+1; $SixthCollumEnds = index($record,":",$FifthCollumEnds) +1; #THIRT COLLUM IS 1000 OR HIGHER #ATTENTION NEEDED: #Changed to 0 for Debug Reasons. #Change it back to 1000 when working.. #if(substr($record, $SecondCollumEnds, ($ThirthCollumEnds-$SecondCo +llumEnds))ge 0) #ge is: >= #{ #Set %Username% to First Collum Value $Username = substr($record, 0, $SecondCollumEnds-3); #Dont ask the -3. + Dont know why, but it seems to work.. ^^ print "<TD> $Username </TD>"; #Read "ls %HOMEDIR%/.idesktop" $homedir = substr($record, $FifthCollumEnds, $SixthCollumEnds-1); #$output = ls("$homedir//.idesktop"); #print "<TD> $output </TD>"; print"<TD>$homedir</TD>"; #DEBUG ONLY print "<TD>DB:::</TD>"; print "<TD>R: $record</TD>"; print "<TD>A: ($SixthCollemEnd+1)-$FifthCollumEnds</TD>"; print "<TD>F: $FirstCollumEnds </TD>"; print "<TD>S: $SecondCollumEnds </TD>"; print "<TD>T: $ThirthCollumEnds </TD>"; print "<TD>F: $FourthCollumEnds </TD>"; print "<TD>5: $FifthCollumEnds </TD>"; print "<TD>S: $SixthCollumEnds </TD>"; #DEBUG ONLY END #Read if iDesk files and directorys exist: #File %HOMEDIR%/.ideskrc and dir %HOMEDIR%/.idesktop ##ATTENTION NEEDED: #if(($homedir/.idesktop/)&& ($homedir/.ideskrc)) #{print "<TD> FOUND </TD>";} else {print "<TD> + Not Found </TD><TD> </TD>";} #Check if iDesk is running by Person. ##ATTENTION NEEDED: #And put the edit button down. ##ATTENTION NEEDED: #print "<TD> <form ONCLICK="window.location.href = ".//ed +ituser.cgi $Username $homedir""><button>Edit</button></form> </TD> print"</TR>"; #}else{print"<TD>No Users found.</TD><TD> - </TD><TD> FAILED </TD> +<TD> FAILED </TD><TD> - </TD></TR>";} }#Check for another. close(PASSWDFILE); #When everything is found do: ##ATTENTION NEEDED: #print "<form ONCLICK="window.location.href = ".//changeglobal.cgi"" +><button>Change Global Settings</button></form> # <form ONCLICK="window.location.href = ".//reapply.cgi""><but +ton>Rebuild All Files</button></form> print"</body>";
And well the part:
$homedir = substr($record, $FifthCollumEnds, $SixthCollumEnds-1); #$output = ls("$homedir//.idesktop"); #print "<TD> $output </TD>"; print"<TD>$homedir</TD>";
Also does weird :S Well, who can help me maybe?

Comment on substr Rampage
Select or Download Code
Re: substr Rampage
by moritz (Cardinal) on Sep 17, 2007 at 09:26 UTC
Re: substr Rampage
by bruceb3 (Pilgrim) on Sep 17, 2007 at 09:35 UTC
    That is great use of the Perl built in function 'index'. Let me introduce you to another Perl built in function. This one is called 'split'.

    To read the password file try something like this code..

    open my $passwdfh, "<", "/etc/passwd" or die "couldn't open the passwo +rd file: $!\n"; while (<$passwdfh>) { next if /^\s*#/; # comments in the password file? chomp; my ($login, $passwd, $uid, $gid, $gcos, $home, $shell) = split /:/; # do stuff with the entries from the password file now ... next if $uid < 1000; # .. for example. } close $passwdfh;
Re: substr Rampage
by Sidhekin (Priest) on Sep 17, 2007 at 09:41 UTC

    substr($record, $FifthCollumEnds, $SixthCollumEnds-1);

    Without reading your code, it looks like you are misunderstanding the substr syntax. The third argument is supposed to be length, but what you are passing looks like an end offset.

    If I read that right, you might try:

    substr($record, $FifthCollumEnds, $SixthCollumEnds-$FifthCollumEnds);

    Still looks like an off-by-one error waiting to happen though.

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

Re: substr Rampage
by graff (Chancellor) on Sep 17, 2007 at 12:45 UTC
    I gather you are running this on a unix or linux box of some sort. In terms of posting code for monks to look at, you might try running this shell command, and just post the stuff that gets printed to your terminal as a result:
    perl -ne 'print unless /^\s*#/' your_script_file
    That removes all comments and lines that are commented out. (It also removes the initial "shebang" line, but we don't need to see that anyway.)

    I was going to suggest there might be a problem here:

    open (PASSWDFILE, "//etc//passwd");
    but then I remembered that the OS will automatically ignore repeated slashes in file path strings. Still, you don't need to double the slashes (it's the backslash that needs to be doubled in order to get a literal backslash: \\). And you really should check whether the open() succeeded, even for that file; it's just a good habit.

    Welcome to the Monastery!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://639367]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (13)
As of 2014-09-02 13:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (22 votes), past polls