Shell perl scripts one liners

An equivalent of the other find-replace, except it's a one-liner that generates no temp files, and is more flexible:

perl -pi -e 's/find/replace/g' *.txt

Or, to change matching files in a hierarchy:

find . -name '*.txt' |xargs perl -pi -e 's/find/replace/g'

Find the full name of a user
If you want to find out the full name for a user name you can use one of these one-liners to do the job:

ypmatch matkin passwd | cut -d: -f5 | cut -d, -f1
grep "^matkin:" /etc/passwd | cut -d: -f5 | cut -d, -f1


Remove processes matching some regular expression
If you have a number of processes that you want to kill, one of the following one-liners might be useful:

kill `ps xww | grep "sleep" | cut -c1-5` 2>/dev/null
ps xww | grep "sleep" | cut -c1-5 | xargs kill 2>/dev/null

This will kill any processes that has the word "sleep" in the calling command. If your kill does not handle multiple pids' you can either use the one-liner

ps xww | grep "sleep" | cut -c1-5 | xargs -i kill {} 2>/dev/null
or use a for-loop:
for x in `ps xww | grep "sleep" | cut -c1-5`
do
kill $x 2>/dev/null


To list all files in the `/usr/local' directory tree that are greater than 10,000 kilobytes in size, type:

$ find /usr/local -size +10000k [RET]

To list all files in your home directory tree less than 300 bytes in size, type:

$ find ~ -size -300b [RET]

To list all files on the system whose size is exactly 42 512-byte blocks, type:
$ find / -size 42 [RET]



Use the `-empty' option to find empty files -- files whose size is 0 bytes. This is useful for finding files that you might not need, and can remove.
To find all empty files in your home directory tree, type:
$ find ~ -empty [RET]


Apache important questions


 If you specify both deny from all and allow from all, what 
will be the default action of Apache?


It also depends on Order directive
order allow, deny # connection will be denied
order deny, allow # connection will be allowed

 what is  apachectl graceful

Note that you will either need to be running as root or use the "sudo" command in order to run this command.

If Apache is not already running it will be started. If it is already running then it will reload with the new changes but will not abort active connections, meaning that anyone who is in the middle of downloading something will continue to be able to download it.

Before restarting the Apache service a check will be done on the configuration files to ensure they are valid. If there is an error in them the error will be displayed and the Apache service will continue running using the old settings. You need to correct your settings before attempting to restart again.


How do you check for the httpd.conf consistency and any errors in it? -

apachectl configtest


When I do ps -aux, why do I have one copy of httpd running as root and the rest as nouser?


 You need to be a root to attach yourself to any Unix port below 1024, and we need 80.

running apache as a root is a security risk?
-

No, That one root process opens port 80, but never listens to it, so no user will actually enter the site with root rights. If you kill the root process, you will see the other kids disappear as well.

What is ServerType directive? -

 It defines whether Apache should spawn itself as a child process (standalone) or keep everything in a single process (inetd). Keeping it inetd conserves resources. This is deprecated, however.



perl script to check duplicate files

use strict;
use warnings;
use File::Find;
use Digest::MD5;
use Data::Dumper ;

my @dup = fnd_dup(@ARGV) ;
#print Dumper ([@dup]);
foreach my $cur_cup (@dup) {
foreach my $cur_fil (@$cur_cup) {

print "my duplicate file is $cur_fil\n";
}
}

sub fnd_dup (@){
my (@dir_list) = @_;
if ( $#dir_list < 0 ) {
return (undef) ;
}
my %file ;
find( sub { -f && push @{$file{(stat($_))[7]}}, $File::Find::name }, @dir_list );

#print Dumper ([\%file]);
#'2040' => [
# '/opt/dkim-milter-2.8.3/obj.Linux.2.6.18-92.el5.x86_64/libsm/fpurge.o',
# '/opt/dkim-milter-2.8.3/obj.Linux.2.6.18-128.el5.x86_64/libsm/fpurge.o'
# ]
my (@resulth);
my %md ;
foreach my $size ( keys %file ) {


if ( $#{$file{$size}} < 1 ) {
next ;
}

foreach my $curr_file ( @{$file{$size}}) {
open (FILE, $curr_file ) or next ;
binmode(FILE) ;
push @{$md{Digest::MD5->new->addfile(*FILE)->hexdigest}}, $curr_file ;
close(FILE);
}
foreach my $hash (keys %md ) {
if ($#{$md{$hash}} >= 1 ) {
push (@resulth, [@{$md{$hash}}]);

}
}
}
return (@resulth)


Basic Shell scripting

shell scripting

1. How do you find out what's your shell? - echo $SHELL
  1. What's the command to find out today's date? - date
  2. What's the command to find out users on the system? - who
  3. How do you find out the current directory you're in? - pwd
  4. How do you remove a file? - rm
  5. How do you remove a - rm -rf
  6. How do you find out your own username? - whoami
  7. How do you send a mail message to somebody? - mail somebody@techinterviews.com -s 'Your subject' -c 'cc@techinterviews.com'
  8. How do you count words, lines and characters in a file? - wc
  9. How do you search for a string inside a given file? - grep string filename
  10. How do you search for a string inside a directory? - grep string *
  11. How do you search for a string in a directory with the subdirectories recursed? - grep -r string *
  12. What are PIDs? - They are process IDs given to processes. A PID can vary from 0 to 65535.
  13. How do you list currently running process? - ps
  14. How do you stop a process? - kill pid
  15. How do you find out about all running processes? - ps -ag
  16. How do you stop all the processes, except the shell window? - kill 0
  17. How do you fire a process in the background? - ./process-name &
  18. How do you refer to the arguments passed to a shell script? - $1, $2 and so on. $0 is your script name.
  19. What's the conditional statement in shell scripting? - if {condition} then … fi
  20. How do you do number comparison in shell scripts? - -eq, -ne, -lt, -le, -gt, -ge
  21. How do you test for file properties in shell scripts? - -s filename tells you if the file is not empty, -f filename tells you whether the argument is a file, and not a directory, -d filename tests if the argument is a directory, and not a file, -w filename tests for writeability, -r filename tests for readability, -x filename tests for executability
  22. How do you do Boolean logic operators in shell scripting? - ! tests for logical not, -a tests for logical and, and -o tests for logical or.
  23. How do you find out the number of arguments passed to the shell script? - $#
  24. What's a way to do multilevel if-else's in shell scripting? - if {condition} then {statement} elif {condition} {statement} fi
  25. How do you write a for loop in shell? - for {variable name} in {list} do {statement} done
  26. How do you write a while loop in shell? - while {condition} do {statement} done
  27. How does a case statement look in shell scripts? - case {variable} in {possible-value-1}) {statement};; {possible-value-2}) {statement};; esac
  28. How do you read keyboard input in shell scripts? - read {variable-name}
  29. How do you define a function in a shell script? - function-name() { #some code here return }
  30. How does getopts command work? - The parameters to your script can be passed as -n 15 -x 20. Inside the script, you can iterate through the getopts array as while getopts n:x option, and the variable $option contains the value of the entered option.

clean up script -- difference between two files

#!/usr/bin/perl


my $totalfile = $ARGV[0];
my $suspectfile = $ARGV[1];
my $outfile = $ARGV[2];

if ($#ARGV != 2) {
print "user script.pl realfile suspectfiel goodfile\n" ;
exit;
}

local $/;
open (SUS,"<" ,$suspectfile) or die " cannot open file " ;
my @allsus = split /\n/, <SUS>;
close( SUS) ;


open (TOT,"<" ,$totalfile) or die " cannot open file " ;
my @total = split /\n/, <TOT>;
close (TOT) ;

my %allsus = map {$_ => 1 } @allsus;

my @allgood = grep {!defined $allsus{$_ } } @total ;

my @new = join "\n", @allgood;

open (OUT,">",$outfile ) or die " cannot open file ";
print OUT "@new";
close (OUT);


Other Articles

Enter your email address: