Hacking with the quote command on Bash shell

ABOUT Quoting

Quoting is used to remove the special meaning of certain characters or words to the shell.  Quoting can
be used to disable special treatment for special characters, to prevent reserved words from being
recognized as such, and to prevent parameter expansion.

Each of the shell metacharacters (*note Definitions::) has special meaning to the shell and must be
quoted if it is to represent itself. When the command history expansion facilities are being used 
(*note History Interaction::), the HISTORY EXPANSION character, usually '!', must be quoted to prevent
history expansion.  *Note Bash History Facilities::, for more details concerning history expansion.

There are three quoting mechanisms: the ESCAPE CHARACTER, single quotes, and double quotes.

Escape Character

A non-quoted backslash '\' is the Bash escape character.  It preserves the literal value of the next
character that follows, with the exception of 'newline'.  If a '\newline' pair appears, and the backslash
itself is not quoted, the '\newline' is treated as a line continuation (that is, it is removed from the
input stream and effectively ignored).


Single Quotes

Enclosing characters in single quotes (''') preserves the literal value of each character within the
quotes.  A single quote may not occur between single quotes, even when preceded by a backslash.

Double Quotes


Enclosing characters in double quotes ('"') preserves the literal value of all characters within the
quotes, with the exception of '$', '`', '\', and, when history expansion is enabled, '!'.  
The characters '$' and '`' retain their special meaning within double quotes (*note Shell
Expansions::).  The backslash retains its special meaning only when followed by one of the following
characters: '$', '`', '"', '\', or 'newline'.  Within double quotes, backslashes that are followed by
one of these characters are removed.  Backslashes preceding characters without a special meaning are left
unmodified.  A double quote may be quoted within double quotes by preceding it with a backslash.  If
enabled, history expansion will be performed unless an '!' appearing in double quotes is escaped using a
backslash.  The backslash preceding the '!' is not removed.

The special parameters '*' and '@' have special meaning when in double quotes (*note Shell Parameter
Expansion::).

TYPICAL SHELL SESSION RELATED
[bash]
$echo quote ls
quote ls
$echo ‘quote ls’
quote ls
$echo ‘quote ls`
> ;
> ^C
$echo `quote ls`
‘ls’
$echo `ls`
animal.png animal.xcf icon.png
$ls
animal.png animal.xcf icon.png
$ls `quote ls`
ls: cannot access ‘ls’: No such file or directory
$ls ‘quote `ls`’
ls: cannot access quote `ls`: No such file or directory
$echo ‘quote `ls`’
quote `ls`
$echo `quote `ls“
”ls
$echo "quote `ls`"
quote animal.png
animal.xcf
icon.png
$echo ‘quote `ls`’
quote `ls`
$echo ‘ls `ls`’
ls `ls`
$quote "ls pwd"
‘ls pwd’$
$quote "ls pwd \n"
‘ls pwd \n’$
$ls `quote ls`
ls: cannot access ‘ls’: No such file or directory
$ls `ls`
animal.png animal.xcf icon.png
$

[/bash]
LINK
https://www.gnu.org/software/bash/manual/bash.txt

Hacking with walking a process tree with pstree

pstree is a small, command line (i.e., all-text mode) program
that displays the processes (i.e., executing instances of programs)
on the system in the form of a tree diagram. It differs from the
much more commonly used (and more complex) ps program in a number
of respects, including that the latter shows the processes in a list
rather than a tree diagram but provides more detailed information
about them.

source : http://www.linfo.org/pstree.html

$pstree -a -p 2658
chromium,2658
  ├─{Chrome_ChildIOT},2661
  ├─{CompositorRaste},2665
  ├─{Compositor},2664
  ├─{HTMLParserThrea},2666
  ├─{OptimizingCompi},2662
  └─{v8:SweeperThrea},2663
$pstree -a -p 1585
gnome-terminal-,1585
  ├─bash,1590
  │   └─pstree,2884 -a -p 1585
  ├─bash,2847
  │   └─top,2855
  ├─gnome-pty-helpe,1589
  ├─{dconf worker},1587
  ├─{gdbus},1586
  └─{gmain},1591
$pstree -a -p 28
(kworker/0:1,28)
$pstree -a -p 27
(kworker/1:1,27)
$pstree -a -p 2855
top,2855

Hacking with a command to look into cpu related hardware information

lscpu  gathers  CPU architecture information like number of CPUs,
threads, cores, sockets,NUMA nodes, information about CPU caches,
CPU family,  model,  bogoMIPS,  byte  order  and stepping  from
sysfs and /proc/cpuinfo, and prints it in a human-readable format.
It supports both online and offline CPUs.  It can also print out
in a parsable format, including how different caches are shared by
different CPUs, which can be fed to other programs.

source : http://www.unix.com/man-page/linux/1/lscpu/
$lscpu 
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                2
On-line CPU(s) list:   0,1
Thread(s) per core:    2
Core(s) per socket:    1
Socket(s):             1
NUMA node(s):          1
Vendor ID:             AuthenticAMD
CPU family:            21
Model:                 19
Stepping:              1
CPU MHz:               1400.000
BogoMIPS:              6029.64
Virtualization:        AMD-V
L1d cache:             16K
L1i cache:             64K
L2 cache:              1024K
NUMA node0 CPU(s):     0,1
$


How to write a program to solve project euler problem 10 ?

// Louis Casillas, oxaric@gmail.com 
// Further Edits: Fahad Uddin
// Modified a little by Jeffrin Jose T 
// Euler Problem #10
// Find the sum of all the primes below two million.

#include 
#include 
char isPrime( int num )
{
   if ((num % 3) == 0 )
   {
      return 0;
   }

   int i = 0;
   int r = floor ( sqrt( num ) );

   int f = 5;
   while (f <= r)
   {
      if ((num % f) == 0)
      {
         return 0;
      }
      if ((num % (f + 2)) == 0)
      {
         return 0;
      }
      f += 6;
   }
   return 1;
}
#define UPTO 2000000
int main(void)
{
   int i = 5;
   double sum_of_primes = 5;
   while (i < UPTO)
   {
      if ( isPrime( i ) )
      {     
         sum_of_primes += i;
      }
      i += 2;
   }
   printf( "The sum of the primes is: %f \n", sum_of_primes );
   return 0;
}

How to write a program to solve project euler problem 9 ?

# There exists exactly one Pythagorean triplet for which a + b + c = 1000.                                                    
# Find the product abc.                                                                                                       
# This program is copied from http://code.jasonbhill.com/python/project-euler-problem-9/                                      


import time

def prod_triplet_w_sum(n):
    for i in range(1,n,1):
        for j in range(1,n-i,1):
            k = n-i-j
            if i**2+j**2==k**2:
                return i*j*k
    return 0

start = time.time()
product = prod_triplet_w_sum(1000)
elapsed = (time.time() - start)

print "found %s in %s seconds" % (product,elapsed)

How to write a program to solve related to euler problem 8 ?

# This code related is taken from http://code.jasonbhill.com/python/project-euler-problem-8/
import time

start = time.time()

L = []
L.append("73167176531330624919225119674426574742355349194934")
L.append("96983520312774506326239578318016984801869478851843")
L.append("85861560789112949495459501737958331952853208805511")
L.append("12540698747158523863050715693290963295227443043557")
L.append("66896648950445244523161731856403098711121722383113")
L.append("62229893423380308135336276614282806444486645238749")
L.append("30358907296290491560440772390713810515859307960866")
L.append("70172427121883998797908792274921901699720888093776")
L.append("65727333001053367881220235421809751254540594752243")
L.append("52584907711670556013604839586446706324415722155397")
L.append("53697817977846174064955149290862569321978468622482")
L.append("83972241375657056057490261407972968652414535100474")
L.append("82166370484403199890008895243450658541227588666881")
L.append("16427171479924442928230863465674813919123162824586")
L.append("17866458359124566529476545682848912883142607690042")
L.append("24219022671055626321111109370544217506941658960408")
L.append("07198403850962455444362981230987879927244284909188")
L.append("84580156166097919133875499200524063689912560717606")
L.append("05886116467109405077541002256983155200055935729725")
L.append("71636269561882670428252483600823257530420752963450")
M = []
for s in L:
for t in list(s): M.append(t)
prod = 0
for i in range(len(M)-4):
p = int(M[i])*int(M[i+1])*int(M[i+2])*int(M[i+3])*int(M[i+4])
if p > prod: prod = p
print prod

elapsed = (time.time() - start)
print "time: %s seconds" % elapsed

A program to find 10001st prime number ?

ABOUT PRIME NUMBER

A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1, involve 5 itself. However, 6 is composite because it is the product of two numbers (2 × 3) that are both smaller than 6. Primes are central in number theory because of the fundamental theorem of arithmetic: every natural number greater than 1 is either a prime itself or can be factorized as a product of primes that is unique up to their order.

TYPICAL SOURCE CODE RELATED
[c]
/*
This code is worked on from http://notmyfaultsblog.blogspot.in/2010/06/project-euler-problem-7-in-c.html
Modified a little for standards by Jeffrin Jose T ahiliation@yahoo.co.in

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?

*/

#include <stdio.h>
#include <math.h>

int isPrime(int test)
{
int i;
int calculateTo = (int) sqrt(test);

for(i = 3; i<=calculateTo; i+=2)
{
if(test%i==0)
return 0;
}

return 1;
}

int main()
{
int howHigh=10001;
int counter=1;
int i;

while (1)
{
for(i=3 ; ; i+=2)
{
if( isPrime(i) )
counter++;

if(counter==howHigh)
{
printf("%d\n", i);
return 0;
}
}

}

return 1;
}
[/c]
TYPICAL OUTPUT SESSION RELATED
[bash]
$gcc -lm tpn.c -o tpn
$./tpn
104743
$

[/bash]
LINKS
https://en.wikipedia.org/wiki/Prime_number
https://en.wikipedia.org/wiki/Factorization

How to write a program to solve project euler problem 6 ?

/*
* Project Name: Problem Six
* Solution Name: Problem Six
* Original creation date: 06/07/2011
* Edit date: 18/01/2013
* Programmer name: Jamie Taylor (aka "GaProgMan")
* File name: ProblemSix.c
*
* Purpose of this project:
* Problem Six, from Project Euler.
* URL: http://projecteuler.net/index.php?section=problems&id=6
* The sum of the squares of the first ten natural numbers is,
* 1^2 + 2^2 + ... + 10^2 = 385
* The square of the sum of the first ten natural numbers is,
* (1 + 2 + ... + 10)^2 = 55^2 = 3025
* Hence the difference between the sum of the squares of the first
* ten natural numbers and the square of the sum is 3025 - 385 = 2640.
* Find the difference between the sum of the squares of the first
* one hundred natural numbers and the square of the sum.
*
* GNU Copyright information
* Copyright 2011 Jamie Taylor <jamie@taylorj.org.uk>
*
* This program is free software; you can redistribute
* it and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
*Foundation; either version 2 of the License, or (at
*your option) any later version.
*
*This program is distributed in the hope that it will
*be useful, but WITHOUT ANY WARRANTY; without even the
*implied warranty of MERCHANTABILITY or FITNESS FOR A
*PARTICULAR PURPOSE. See the GNU General Public
*License for more details.
*
*You should have received a copy of the GNU General
*Public License along with this program; if not, write
*to the Free Software Foundation, Inc., 51 Franklin
*Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include <iostream>
#include <math.h>

using namespace std;

int main () {
unsigned long long sumOfSquares = 0;
unsigned long long squareOfSums = 0;
long long difference = 0;
int i = 1;
do {
sumOfSquares += pow((double)i, 2);
squareOfSums += i;
i++;
} while ( i < 101 );

squareOfSums = pow ((double)squareOfSums, 2);
difference = squareOfSums - sumOfSquares;

cout << "Sum of all squares: " << sumOfSquares << endl;
cout << "Square of all sums: " << squareOfSums << endl;

cout << "The difference between the sum of all squares and the square "
<< "of all sums is:\n" << difference << endl;
char ch;
/* cin >> ch; */
return 0;
}

/* Small changes to this file by <ahiliation@yahoo.co.in> Jeffrin Jose T. */
/* The pow() function returns the value of x raised to the power of y. */

How to write a program to solve project euler problem 5 ?

/* This file is worked ( mostly copy and paste ) from from http://www.programminglogic.com/solution-to-project-euler-5/
Notice that if the number is evenly divisible by all numbers from 11 to 20 it's also divisible by all numbers
from 2 to 10.
*/

#include <stdio.h>

int main(){
int i,j,counter;
for (i=10;i<1000000000;i++){
counter=0;
for (j=11;j<21;j++){
if (i%j==0)
counter++;
}
if (counter==10){
printf("%d \n",i);
break;
}
}
return 0;
}

gdb backtrace for a chromium hangup

(gdb) bt
#0  pthread_cond_timedwait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_timedwait.S:238
#1  0x00007f9d69721343 in base::ConditionVariable::TimedWait (this=0x7fffdb12b308, max_time=...)
    at ../../base/synchronization/condition_variable_posix.cc:102
#2  0x00007f9d696fde2c in base::WaitableEvent::TimedWait (this=0x7f9d705c0540, max_time=...)
    at ../../base/synchronization/waitable_event_posix.cc:214
#3  0x00007f9d696dcc16 in base::MessagePumpDefault::Run (this=0x7f9d705c0530, delegate=0x7fffdb12b570)
    at ../../base/message_loop/message_pump_default.cc:56
#4  0x00007f9d696edc51 in base::RunLoop::Run (this=0x7fffdb12b3e0) at ../../base/run_loop.cc:49
#5  0x00007f9d696da157 in base::MessageLoop::Run (this=) at ../../base/message_loop/message_loop.cc:308
#6  0x00007f9d6c310ffc in content::RendererMain (parameters=...) at ../../content/renderer/renderer_main.cc:227
#7  0x00007f9d696a31de in content::RunZygote (main_function_params=..., delegate=)
    at ../../content/app/content_main_runner.cc:339
#8  0x00007f9d696a4071 in content::ContentMainRunnerImpl::Run (this=0x7f9d7059a270)
    at ../../content/app/content_main_runner.cc:764
#9  0x00007f9d696a2db0 in content::ContentMain (params=...) at ../../content/app/content_main.cc:19
#10 0x00007f9d69101ec8 in ChromeMain (argc=4, argv=0x7fffdb12bbd8) at ../../chrome/app/chrome_main.cc:57
#11 0x00007f9d60e91b45 in __libc_start_main (main=0x7f9d69101e70 , argc=4, argv=0x7fffdb12bbd8, 
    init=, fini=, rtld_fini=, stack_end=0x7fffdb12bbc8) at libc-start.c:287
#12 0x00007f9d69101d6f in _start ()
(gdb)