App-Cheats
view release on metacpan or search on metacpan
df -h /tmp
#############################################################
## Linux Commands - diff, sdiff, patch
#############################################################
# Do a file comparison while showing the context (copy)
diff -c file1 file2
# Do a file comparison while showing the context (unified)
diff -u file1 file2
# Do a difference on variables (not just files)
diff -u <(echo "a") <(echo "b")
# Create a patch file
diff -rupN src2 src > test.patch
# Compare folders
diff -ruN src_sean_latest/ src > diff
# Side by side difference
sdiff file1 file2
# Apply a patch file (-p1 ignore first later)
patch -p1 < ../test.patch
# Fix: "sh: -c: line 0: syntax error near unexpected token `('"
system qq(/bin/bash -c "diff -u <(echo a) <(echo b)");
system qq(/bin/bash -c "diff -u <(echo \\"a a2 a3\\") <(echo \\"b b2 b3\\")");
#############################################################
## Linux Commands - dos2unix
#############################################################
# Fix files endings of all files in folder (recursive)
find . -type f -print0 | xargs -0 dos2unix
# Convert newlines to unix format (trim off carriage returns, like dos2unix)
perl -lpe 'BEGIN{$n=chr 13; $r=qr/$n$/} s/$r//'
#############################################################
## Linux Commands - dpkg
#############################################################
# Print dpkg architecture (DES,machine)
# When installing packages.
dpkg --print-architecture
# Print allowed foreign architectures (DES,machine)
# When installing packages.
dpkg --print-foreign-architectures
# Error - Could not get lock /var/lib/dpkg/lock (admin)
sudo kill -9 $(sudo lsof | grep /var/lib/dpkg/lock | awk '{print $2}')
sudo dpkg --configure -a
# dpkg: error: dpkg status database is locked by another process (DES,bench)
sudo rm -f /var/lib/dpkg/lock
# Install a package from a file
sudo dpkg -i my.deb
# View packages installed on the system
dpkg -l
# Check if a package is installed on a system
dpkg -l | grep kate
# Check installion location of a package
dpkg -L <package>
# Add architecture to the list of architectures for which (DES)
# packages can be installed without using --force-architecture
dpkg --add-architecture i386
# Check if a package is on hold (no updates)
dpkg --get-selections apksigner
apt-mark showhold
# Remove packages marked with rc:
dpkg -l | \grep '^rc' | nth 1 | xargs sudo dpkg --purge
#############################################################
## Linux Commands - dmesg
#############################################################
# View kernel messages
dmesg
#############################################################
## Linux Commands - du
#############################################################
# Get top 20 people using too much disk space
du -ms /* | sort -nr | head -n 20
# Find how much space each locked user is using
a=`sudo passwd -Sa | AND -r "\bL$" | get_nth 0 | while read u; do ls -d1 /net/home*/$u; done 2>/dev/null | col_2_row`
b=`sudo du -scm `echo $a` | sort -nr`
echo "$b" | perl -ple '($u)=m{/([^/]+)$} or ($_=sprintf "%-30s %s", $_,"Password Status (L - Locked)") and next; ($s)=(split " ",`sudo passwd -S $u`)[-1]; chomp $s; $_ = sprintf "%-30s %s", $_, $s'
# Find how much space each locked user is using (compact)
sudo passwd -Sa | AND "\bL$" | perl -aple '$d=qx{sudo du -sm /net/home*/$F[0]}; $d = $? ? "NO HOME DIR" : (chomp $d,$d); $_ = "$d - $_"'
#############################################################
## Linux Commands - eval
#############################################################
# Variable name is stored in another variable
n1=abc
abc=blue
abc2=green
eval var=\$$n1
echo $var
n1=abc2
eval var=\$$n1
echo $var
# Expand $HOME variable (dollar,bash)
file='$HOME/my_path'
"`eval echo $file`"
# Expand uicfg $HOME variables
cat $HOME/uicfg | while read line; do echo `eval echo $line`; done
#############################################################
## Linux Commands - expect
#############################################################
# Run a command where user input is required (script,auto,password)
expect -c 'spawn ssh SOME_USER@irkdes "date"; expect "password:"; send "asdf1234\r"; interact'
# Expect quirks (scipt,auto,password)
# This will NOT work
echo "SOME_USER" | while read n; do expect ... ; done
# This DOES work
for n in "SOME_USER"; do expect ... ; done
# Suppress output (scipt,auto,password)
# Some output
expect -c 'spawn -noecho ... '
# All output
expect -c 'log_user 0; spawn ... ' # Restore with "log_user 1"
#############################################################
## Linux Commands - find
#############################################################
# Find every file where a name is used (starting from pwd location)
find . * | perl -ne 'print unless /^\./' | perl -lne 'print if -f' | xargs perl -lne 'print $ARGV if /start.txt/'
# Find all files but do not look in certain directories
find $source -not \( -path '*.svn' -prune -or -path '*.git' -prune \) > $all_files
# Find specific files but return directories
find . -name "*.h" -printf '%h\n'
find . -name "*.h" -exec dirname {} ';'
find . -name "*.h" -exec dirname {} \;
# Check all user accounts if:
# L - Locked
# NP - No Password
# P - Password is set (OK)
sudo passwd -Sa
# Remove user from a group (delete, passwd)
Log unto fs01
sudo gpasswd -d potapov controls
sudo make -C /var/yp
#############################################################
## Linux Commands - pidof
#############################################################
# Process ID of a program (PID)
pidof omsCommand
#############################################################
## Linux Commands - ps
#############################################################
# Kill the ui when things aren't working
ps -ef | grep ui
# See all users on the bench
ps -elf | grep ui
# Show which processes were started by the current user
ps -elf | grep $USER
# Kill unigraph process if it is running in the background
ps -elf | grep unigraph | perl -anle '`skill -KILL $F[3]`'
# Be more specific in process selection (ps) options
ps -o user,ucmd --no-heading -C ui
# Currently running process (use in a script such as getinfo.sh)
ps -elf | grep getinfo.sh | grep -v grep | grep -v $$
# Check if puppet is running on all the benches
run_on_all_benches.pl 'ps -elf | grep puppet | grep -v grep'
# Check if systemd or init is used
ps -p 1
#############################################################
## Linux Commands - rcs
#############################################################
# Unlock a file
co -u file
# RCS error: file is in use
rm -f RCS/,file_name
# RCS error: file locked by pwxxxx
rcs -U file
# RCS: check in a file initially without the prompt message
ci -t-msg tiny2_rcs.tst
#############################################################
## Linux Commands - read
#############################################################
# Read input from the keyword/user without showing the password (much easier in bash)
read -s pass; echo "got [$pass]"
read -s -p "Password:" pass; echo; echo "got [$pass]"
# Read input from the keyword/user without showing the password. Limit password length
read -s -n 5 -p "Password:" pass; echo; echo "got [$pass]"
#############################################################
## Linux Commands - reptyr
#############################################################
# Attach to a running process on the existing terminal
sudo apt-get install reptyr
#############################################################
## Linux Commands - rsnapshot
#############################################################
# Install rsnapshot on centos
yum install epel-release
yum install rsnapshot
# Check if rsnapshot configuration file is valid
rsnapshot configtest
# Show the command that would be executed
rsnapshot -t daily
rsnapshot -t weekly
#############################################################
## Linux Commands - rsync
#############################################################
# Copy/Sync entire directories
rsync -avh FROM TO
# Copy/sync a file while preserving the folder structure.
rsync -avz --relative FROM/file TO
# Show what rsync would do with actually doing it.
rsync -avh --dry-run FROM TO
rsync -avh -n FROM TO
# Copy/Sync entire directories (OnePlus Backup)
time rsync \
-ah \
--info="progress2" \
# Debug a regular expression
perl -Mre=debug -le 'print "abc:def-hij"=~/\w+/'
perl -Mre=debug -e 'print if "aaa:bbb" =~ /\w+/'
perl -Mre=Debug,PARSE -e 'print if "aaa:bbb" =~ /\w+/'
# Debug a regular expression (with some color)
perl -Mre=debugcolor -le 'print "abc:def-hij"=~/\w+/'
# Check if certain words are in order in a file
echo "line1 line2" | perl -0777nlE 'INIT{-t and die; $a=join".*?",map{/\S+/g}<STDIN>; $r=qr/$a/s} say "$ARGV - " . (/$r/?"PASS":"FAIL") ' f1 f2
echo "line1 line4" | perl -Mre=eval -0777ne 'INIT{-t and die; $a=join "",map qq[ (?{print"\\nTrying $_ - "}) (.*?(??{"$_"}) (?{print"pass"})) ],map{/\S+/g}<STDIN>; $r=qr/^$a/sx; print "\n\$r=qr$r\n\n"} print "\n# $ARGV"; print "\n".(/$r/?"PASS":"FAIL...
echo "line1 line4" | perl -Mre=eval -0777ne 'INIT{-t and die; $a=join "",map qq[ (?{print"\\nTrying $_ - "}) (.*?(??{"$_"}) (?{print"pass"})) ],map{/\S+/g}<STDIN>; $r=qr/^$a/sx} print "\n# $ARGV"; print "\n".(/$r/?"PASS":"FAIL")."\n\n"' f1 f2
# View optimizations done on a pattern.
perl -Mre=optimization -Mojo -E 'say r optimization qr/^abc/'
# A way to check if using a regular expression.
perl -Mre=is_regexp -E 'say is_regexp qr{}'
1
perl -Mre=is_regexp -E 'say is_regexp 123'
#############################################################
## Perl Modules - threads
#############################################################
# Error: This Perl not built to support threads
# Check if perl binary supports threads.
perl -V:useithreads
perl -MConfig -E 'say $Config{useithreads}'
# Simple thread example in perl
# Threads start running already with threads->create()
perl -Mthreads -le '@t=map threads->create(sub{print "Im #$_"}), 1..10; $_->join for @t'
# Simple thread example in perl
# Find the summation of 1 through 10
# Uses a shared variable between threads
perl -Mthreads -Mthreads::shared -le '$sum=0; share($sum); @t=map threads->create(sub{$sum += $_}), 1..10; print $_->join for @t; print "sum: $sum"'
# Aliases for threads->create.
perl -lMthreads -le '@t=map threads->new(sub{print $_}), 1..3; $_->join for @t'
perl -lMthreads -le '@t=map async(sub{print $_}), 1..3; $_->join for @t'
perl -lMthreads -le '@t=map threads->new(sub{print $_}), 1..3; $_->join for @t'
# If using a coderef, you must use threads->create.
perl -lMthreads -le '$sub = sub{ print "123" }; @t=map threads->new( $sub ), 1..3; $_->join for @t'
# If using a coderef, you must use threads->create (with arguments).
perl -lMthreads -le '$sub = sub{ print "@_" }; @t=map threads->new( $sub, $_ ), 1..3; $_->join for @t'
# Shared and non shared variables example
perl -lMthreads -Mthreads::shared -le '$a=$b=1; share($a); async(sub{$a++; $b++})->join; print "a=$a, b=$b"'
# Thread pitfall. $a can be either 2 or 3 (race condition)
perl -lMthreads -Mthreads::shared -le '$a=1; share($a); $_->join for map async(sub{my $foo=$a; $a=$foo+1}), 1..2; print "a=$a"'
# Allow only one thread to touch a variable at a time
# This will cause a "deadlock" where one thread requires a reourses
# locked by another thread and vice versa
perl -Mthreads -Mthreads::shared -le 'share $a; share $b; push @t, async(sub{lock $a; sleep 20; lock $b}); push @t, async(sub{lock $b; sleep 20; lock $a}); $_->join for @t'
#
# Alternate syntax 1
perl -Mthreads -le 'my $a :shared; my $b :shared; push @t, async(sub{lock $a; sleep 20; lock $b}); push @t, async(sub{lock $b; sleep 20; lock $a}); $_->join for @t'
#
# Alternate syntax 2
perl -Mthreads -le 'my $a :shared; my $b :shared; push @t, threads->create(sub{lock $a; sleep 20; lock $b}); push @t, threads->create(sub{lock $b; sleep 20; lock $a}); $_->join for @t'
# Thread safe queues. Passing data around
perl -Mthreads -MThread::Queue -le 'my $q=Thread::Queue->new; $t=async(sub{ print "Popped $d off the queue" while $d=$q->dequeue }); $q->enqueue(12); $q->enqueue(qw/A B C/); sleep 1; $q->enqueue(undef); $t->join'
# Thread safe queues. Passing complex data around
perl -Mthreads -MThread::Queue -le 'my $q=Thread::Queue->new; $t=async(sub{ print "Popped @$d off the queue" while $d=$q->dequeue }); $q->enqueue([1..3]); $q->enqueue([qw/A B C/]); sleep 1; $q->enqueue(undef); $t->join'
# Compute pi using parallel threading
time perl -Mthreads -Mthreads::shared -le 'share $sum; $M=100_000_000; $T=6; $h=1/$M; sub sum { my $s; my($from,$to)=@_; for($from..$to){ my $x=$h*($_-0.5); $s += 4/(1+$x**2)}; $s } sub split_by { my($max,$by)=@_; my $from=1; my @l; while($to<$max){ ...
#############################################################
## Perl Modules - CAM::PDF
#############################################################
# Example getting title fields from a pdf.
use CAM::PDF;
use e;
my $infile = shift or die "\nSyntax: perl fill:pdf.pl my.pdf\n";
(my $outfile = $infile) =~ s/(?=\.pdf)/_filled/i;
my $doc = CAM::PDF->new($infile) or die "$CAM::PDF::errstr\n";
say "Titles of the fields:";
p [$doc->getFormFieldList];
say "Adding new field values";
$doc->fillFormFields(
Start_Date => "Value 1",
Closed_Date => "Value 2",
Closed_By => "Value 3",
);
say "saving new file";
$doc->cleanoutput($outfile);
#############################################################
## Perl Modules - Carp
#############################################################
# Show a stack trace in perl.
perl -MCarp=longmess -E 'sub fun1{ fun2("TO FUN2") } sub fun2{ say longmess } fun1("TO FUN1")'
# Show a stack trace in perl. (Carp uses a similar approach).
# @DB::args is set (for a scope) when this command is run (some magic).
{
package DB;
my @caller = caller($scope);
() = caller($scope); # Same thing (to invoke LIST context).
}
perl -E 'sub fun1{ fun2("TO FUN2") } sub fun2{ my $scope = 0; while(my @caller = caller($scope)){ {package DB; () = caller($scope)} say "@caller[1,2,3,4] - (@DB::args)"; $scope++ }} fun1("TO FUN1")'
#############################################################
## Perl Modules - Carton
#############################################################
( run in 0.898 second using v1.01-cache-2.11-cpan-e1769b4cff6 )