Posts

Showing posts from October, 2015

2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1

Functions 2>&- The general form of this one is  M>&- , where  "M"  is a file descriptor number. This will close output for whichever file descriptor is referenced, i.e.  "M" . 2>/dev/null The general form of this one is  M>/dev/null , where  "M"  is a file descriptor number. This will redirect the file descriptor,  "M" , to  /dev/null . 2>&1 The general form of this one is  M>&N , where  "M"  &  "N"  are file descriptor numbers. It combines the output of file descriptors  "M"  and  "N"  into a single stream. |& This is just an abbreviation for  2>&1 | . It was added in Bash 4. &>/dev/null This is just an abbreviation for  >/dev/null 2>&1 . It redirects file descriptor 2 (STDERR) and descriptor 1 (STDOUT) to  /dev/null . >/dev/null This is just an abbreviation for  1>/dev/null . It redirects file descriptor 1 (S

cat -T | find | xargs -n 3 | uniq | chmod . -R | mkisofs | netstat -tnp | pkill

cat food 1> kitty "equal to>" cat food > kitty less fex #!/bin/bash FILE=$1 if [ -f $FILE ] then echo File exists else echo Does not exists fi ------ Remove blank lines cat fex | tr -s '\n' Show tabs cat -T fex List dirs find . -type d multiple criterions find . \( -name "*file" -o -type d \) Find all files access last day find . -atime -1 -type f Find files less than 2k find . -size -2k M – mb G - Gb --------- Delete files find . -name "er*" –delete Print files with permission 644 find . -perm 644 Cat all files find . -name "*file" -print -exec cat {} \; it’s not possible to use multiple commands with –exec parameter, but we can write multiple commands in shell scripts -exec ./commands.sh {} \; xargs $cat g | xargs 1 2 3 4 5 6 7 9 10 11 12 $ cat g | xargs -n 3 1 2 3 4 5 6 7 9 10 11 12 echo splitXsuplitXsplitXsplit | xargs -d X -n 2

readlink - determine actual location of the file JAVA_HOME

$readlink /usr/bin/javac /etc/alternatives/javac $ ls -lart /usr/bin/javac lrwxrwxrwx 1 root root 23 May  9 13:57 /usr/bin/javac -> /etc/alternatives/javac ls -lart /etc/alternatives/javac lrwxrwxrwx 1 root root 42 May  9 13:57 /etc/alternatives/javac -> /usr/lib/jvm/java-7-openjdk-i386/bin/javac $ readlink /etc/alternatives/javac /usr/lib/jvm/java-7-openjdk-i386/bin/javac for Maven: export JAVA_HOME = $ ( / usr / libexec / java_home )

ch10 processes

One of the main tasks of an operating system is to run processes on behalf of its users, services, and applications. These are tracked in a process table inside the kernel , which keeps track of the current state of each process in the system, and the system scheduler decides when each process will be assigned to a CPU , and when it is taken off the CPU again ubuntu2@ubuntu2:~/Documents$ ps -eaf UID        PID  PPID  C STIME TTY          TIME CMD root         1     0  0 Oct14 ?        00:00:04 /sbin/init root         2     0  0 Oct14 ?        00:00:00 [kthreadd] root         3     2  0 Oct14 ?        00:00:21 [ksoftirqd/0] ps -eaf - is a common command to list all current processes ps aux - is a common way to list all current processes on a BSD system GNU/ Linux straddles both traditions, and the GNU implementation of ps accepts System V and BSD options, as well as its own GNU options, such as --user. System V is probably the most widely used format

ch9 arrays

Arrays in the shell are only one-dimensional if you want 2 dimensional array - 64 , the alternative is to have 8 arrays of 8 ----------- New to bash version 4 are associative arrays . These arrays have text instead of a number as their index, so you can keep track of race results using ${points[Ferrari]} and ${points[McLaren]} rather than ${points[0]} and ${points[1]} and then having a lookup table mapping 0 to “Ferrari” and 1 to “McLaren.” ----------- index number goes in square brackets numberarray[0]=zero numberarray[1]=one ----------- $ cat studentarray.sh #!/bin/bash students=( Dave Jennifer Michael # year 1 Alistair Lucy Richard Elizabeth # year 2 Albert Roger Dennis James Roy # year 3 Rory Jim Andi Elaine Clive # year 4 ) for name in ${students[@]} do    echo -en “$name “ done echo $ ./studentarray.sh Dave Jennifer Michael Alistair Lucy Richard Elizabeth Albert Roger Dennis James Roy Rory Jim Andi Elaine Clive $ -------------- $ mp3s=( *.mp3 )

ch8 functions and libraries

functions allow to manage chunks of code and make your code modular functions hide implementation details and allow consistent reuse of code functions can be tested over and over again as a small piece of a larger script The standard Bourne shell syntax uses the function name followed immediately by a pair of parentheses () and curly brackets {} around the code itself. $ cat myfunction.sh #!/bin/bash myfunction() {     echo “This is the myfunction function.” } # Main code starts here echo “Calling myfunction...” myfunction echo “Done.” $ ./myfunction.sh Calling myfunction... This is the myfunction function. Done. $ ---------- There is a second syntax, which is not accepted by the Bourne shell , although bash and ksh both accept it. Instead of following the function name by a pair of parentheses, the function name is preceded by the keyword function: function myfunction ---------- Yet another syntax, accepted by bash alone , is to combine both elements.

ch7 variables continued

Variables are properly referenced as ${variable} , as this allows the shell to differentiate between ${var}iable (the variable $var followed by the text “iable”) and ${variable} (the variable $variable). This can be useful when applying suffixes to variables, such as “${kb}Kb is $bytes bytes, or approx ${mb}Mb an undefined variable is interpreted as an empty string. If it were being treated as a number, it would be interpreted as zero There are not many rules about variable names; they must start with a letter or underscore and can contain letters, numbers, and underscores. Periods, commas, spaces, and any other characters are not valid in variable names . Also, the first character of the variable name must be a letter or an underscore (not a digit). Traditionally, system variables are all uppercase,with words separated by the underscore character: BASH_EXECUTION_STRING when a numeric value is expected, non-numeric values are treated as zero. Bourne shell, which do no

ch6 flow control using loops

Unlike most loops, the for loop does not test the condition of a variable each time it goes around the loop. Instead, it starts with a list of items to iterate through and works its way through them until it has reached the end. #!/bin/bash for fruit in apple orange pear do echo “I really like ${fruit}s” done echo “Let’s make a salad!” -- FOR cat forin1 #!/bin/bash for fruit in $* do echo "I like $fruit" done bash forin1 a b c I like a I like b I like c ---------------- arguments to the function The for loop is best when you know that you want to do the same thing to a set of items, rather than wanting to repeat something until a certain state is achieved for loops are not so good if you expect that you will want to break out of the loop based on the outcome of some test or other. $* expands to all of the parameters passed on the command line $ cat fruit-read.sh #!/bin/bash echo -en “Please tell me some of your favorite f