Posts

Showing posts from July, 2015

backtick (`)

 backtick (`)

for loop

$ cat for1 for fruit do  echo I really like $fruit done echo “Let’s make a salad!” $ bash for1 a b c d I really like a I really like b I really like c I really like d “Let’s make a salad!”

case

$ cat casescr read -p "Enter your fruit"  fruit case $fruit in banana) echo "$fruit is yellow";; apple) echo "$fruit is green";; esac $ bash casescr Enter your fruitbanana banana is yellow

zero length check while

[ -z "$input" ] - returns true if the string has zero length [ -n "$input" ] -returns true if the string has nonzero length [ -z $input ] is not syntatically valid, only [ -z "$input" ] cat while1 input="" while [ -z "$input" ]; do   read -p "Please give your input" input done echo "Thank you for saying $input" $ bash while1 Please give your input Please give your input Please give your inputsome text Thank you for saying some text ----------------

conditions elif | sed -n q;d awk

cat rd if [ -r "$1" ]; then   echo "$1 is readable" else   echo "Error to read $1" fi $ bash rd file file is readable $ bash rd file1 Error to read file1 -------------- cat el OS=`uname -s` if [ "$OS" = "Linux" ]; then   echo "This is Linux" elif [ "$OS" = "Solaris" ]; then   echo "This is Solaris" else   echo "This is Unix" fi $ bash el This is Linux -------------- if [ -e /etc/resolv.conf ]; then -e stands for exists -L stands for symbolic link -S stands for socket -p stands for pipe -d stands for directory   -------------- #test symbolic link $ cat sy file=$1 if [ -L $file ]; then   echo "this is symbolic link" else   echo "not identified file" fi $ bash sy filesym1 this is symbolic link -------------- Replace particular line 3: sed 3s/echo/read/g el > el3 Prin...

variables

$BASH_VERSION $RANDOM $SECONDS ------------- cat pid.sh echo "Process $$ Starting up with arguments $@ for parent $PPID" $$ special variable which provides the process ID of this shell itself PPID is set to the process ID of the process that called this shell or shell script ./pid.sh 1 Process 26249 Starting up with arguments 1 for parent 19260 SECONDS – returns a count of the number of seconds the shell has been running ------------- echo $HOME /home/ok74010 PATH is a colon-separated list of directories used to find program files. It’s searches left-to-right You can read a list of hashed paths by running hash –l, to remove hash –r In PATH, a period (.) is used to represent the current directory of the calling program. Less well known is that a double colon (: :) will do the same, and a single colon at the beginning or end of the PATH variable will also expand to the current working directory. From security perspectiv...

conditions

if/then then and fi statements are required, the space around the [ and ] symbols are also required: if [ condition ]; then statements(s); fi  --------------- cat ifthen if [ 2 -eq 2 ]; then echo "ok" else echo "not ok"; fi $ bash ifthen ok  ------------------ $ if [ 2 -eq 2 ]; then echo "ok" else echo "not ok"; fi ok else echo not ok ------------------ -r return true onlly if file exists and is readable cat ift2 if [ ! -r "$1" ]; then echo "error reading file" else echo "ok" fi $ bash ift2 ifthen ok $ bash ift2 ifthen3 error reading file  

backslash

within double quotes $ ` ' * and <new line> are treated as normal; a backslash followed by anything else is not as special.  $ echo "$HOME\&" /home/ubuntu2\& $ echo $HOME\& /home/ubuntu2&

rm -rf . | wildcards | m5sum loop

rf -rf .  - will remove everything from the current directory downward rf "-rf ." - will remove file named -rf . ubuntu2@ubuntu2:~/temp/newdir1/dir2/dir3$ ll total 8 drwxrwxr-x 2 ubuntu2 ubuntu2 4096 Jul 20 11:06 ./ drwxrwxr-x 3 ubuntu2 ubuntu2 4096 Jul 20 11:00 ../ -rw-rw-r-- 1 ubuntu2 ubuntu2    0 Jul 20 11:05 file-1 -rw-rw-r-- 1 ubuntu2 ubuntu2    0 Jul 20 11:06 file-2 -rw-rw-r-- 1 ubuntu2 ubuntu2    0 Jul 20 11:06 file-3 ubuntu2@ubuntu2:~/temp/newdir1/dir2/dir3$ ubuntu2@ubuntu2:~/temp/newdir1/dir2/dir3$ ll *[-12] -rw-rw-r-- 1 ubuntu2 ubuntu2 0 Jul 20 11:05 file-1 -rw-rw-r-- 1 ubuntu2 ubuntu2 0 Jul 20 11:06 file-2 ubuntu2@ubuntu2:~/temp/newdir1/dir2/dir3$ to find - or [ put it into [] ------------ touch *f*.dot rm \*f\*.dot ------------ rm -rf * ubuntu2@ubuntu2:~/temp/newdir1/dir2/dir3$ ll total 8 drwxrwxr-x 2 ubuntu2 ubuntu2 4096 Jul 20 11:50 ./ drwxrwxr-x 3 ubuntu2 ubuntu2 4096 Jul 20 11:00 ../ -rw-rw-r-- 1 ubuntu2 ubunt...

mkdir -p

mkdir -p newdir1/dir2/dir3

linux file types

How many types of file are there in Linux/Unix? By default Unix have only 3 types of files. They are.. Regular files Directory files Special files(This category is having 5 sub types in it.) So in practical we have total 7 types(1+1+5) of files in Linux/Unix. And in Solaris we have 8 types. And you can see the file type indication at leftmost part of “ls -l” command. Here are those files type. Regular file(-) Directory files(d) Special files Block file(b) Character d evice file(c) Named pipe file or just a pipe file(p) Symbolic link file(l) Socket file(s) For your information there is one more file type called door file(D) which is present in Sun Solaris as mention earlier. A door is a special file for inter-process communication between a client and server (so total 8 types in Unix machines). We will learn about different types of files as below sequence for every file type. Definition and information of the file type How to create particular file typ...

return codes

return code 1 - no match for grep 2 .. - error 0 - success

positional parameters | if

$0 is the name of the command itself $1 first parameter, $2 second, etc $# variable tells how many parameters it was called with $ cat n3params echo "my name is $0" echo "I was called as $0 with $# parameters" if [ "$#" -eq "2" ]; then echo "my first parameter is $1" echo "my second parameter is $2" else echo "you provided $# par'ameters but 2 required" fi --- $ bash n3params f1 r2 my name is n3params I was called as n3params with 2 parameters my first parameter is f1 my second parameter is r2 $ bash n3params f2 my name is n3params I was called as n3params with 1 parameters you provided 1 par'ameters but 2 required -----------------------  cat n4manaparams echo "I was called with $# params" count=1 while [ "$#" -ge "1" ]; do echo "parameter $count is $1" let count=$count+1 shift done -- bash n4manaparams qwe rty uio I was called with 3 param...

while read from file

$ cat n2while while read mess do echo $mess date done < /etc/group | grep ubuntu $ bash n2while adm:x:4:syslog,ubuntu2 cdrom:x:24:ubuntu2 sudo:x:27:ubuntu2 dip:x:30:ubuntu2 plugdev:x:46:ubuntu2 lpadmin:x:108:ubuntu2 ubuntu2:x:1000: sambashare:x:124:ubuntu2

variable read and set grep

$ cat n1 echo -n "Enter your surname and name: " read surname name echo "Hi $name, how is your $surname family" set | grep name $ bash n1 Enter your surname and name: Corbat Mike Hi Mike, how is your Corbat family name=Mike surname=Corbat $

sed | read var | echo -n | echo $! |

Commands to show hidden characters sed -n 'l' f  #l - L ----- sed commands usually include backslashes sed s/text/replace/g file > newfile ------------------------------ ----- There are three primary ways of assigning a value to a variable: .. Explicit definition: VAR=value .. Read: read VAR .. Command substitution: VAR=`date`, VAR=$(date) cat f #!/bin/sh echo -n please enter your name:\ read var echo your name is $var ------------------------------ ----- cat f #!/bin/sh echo -n please enter your name:\ read var echo your name is $var Please enter your first name and last name: AAA VVV Hello, AAA. How is the VVV family? {{PAGE 36}} ------------------------------ ----- reading from files $ while read message > do > echo $message > done < /etc/motd ------------------------------ ----- date +"%A" Tuesday date +"%k"' '"%A" 11 Tuesday -----------------...

variables | binary ASCII | loop parallel running | source

source is a bash shell built-in command that executes the content of the file passed as argument, in the current shell. It has a synonym in '.' (period). ------------------ Show file content with tabs (without line breaks and non-printing characters) cat -T /tmp/testing.txt Show file content with line breaks (without tabs and non-printing characters) cat -E /tmp/testing.txt Show file content with tabs, line breaks and non-printing characters cat -A testfile name^Isurname^Iage$ Bill^ISimson^I22$ Alex^IFerguson^I33$ ------------------ $ mtr --report google.com HOST: example                  Loss%   Snt   Last   Avg  Best  Wrst StDev   1. inner-cake                    0.0%    10    2.8   2.1   1.9   2.8   0.3   2. outer-cake            ...

awk -F, '$6==10' | awk | available shells /etc/shells

Show rows in the CSV file which column =6 have value = 10 gzcat filename.csv.gz | awk -F, '$6==10' ------------------------------------------------ The following program searches the system password file and prints the entries for users whose full name is not indicated: awk -F: '$5 == ""' /etc/passwd ------------------------ ------------------------ awk -F : '{print $4}' awk  - this is the interpreter for the AWK Programming Language. The AWK language is useful for manipulation of data files, text retrieval and processing -F <value>  - tells  awk  what field separator to use. In your case,  -F:  means that the separator is  :  (colon). '{print $4}'  means print the fourth field (the fields being separated by  : ). ------------------------------------------------ cat ds | awk '{print$1,$2}' ---------------------- type cat /etc/shells to see a list of available shells

shell scripting book

 The following code will read the line into a variable called message, looping around until there is no more input (read returns non-zero if an end-of-file was read, so the while loop ends — this will be looked at in greater detail in Chapter 6). $ while read message > do > echo $message > done < /etc/motd

64/32 bit arch -- lscpu arch /proc/cpuinfo | isainfo -b

#isainfo -b 64 #arch i686 #lscpu Architecture:          i686 CPU op-mode(s):        32-bit, 64-bit Byte Order:            Little Endian CPU(s):                1 On-line CPU(s) list:   0 Thread(s) per core:    1 Core(s) per socket:    1 Socket(s):             1 Vendor ID:             GenuineIntel CPU family:            6 Model:                 60 Stepping:              3 CPU MHz:    ...

is not in the sudoers file

Open a Root Terminal and type visudo (to access and edit the list). Navigate to the bottom of the sudoers file that is now displayed in the terminal. Just under the line that looks like the following: root ALL=(ALL) ALL Add the following (replacing user with your actual username): user ALL=(ALL) ALL

how to find installed jdk and set JAVA_HOME evinronment

$locate jdk /usr/lib/jvm/java-7-openjdk-i386/bin/java You can set your JAVA_HOME in /etc/profile as Petronilla Escarabajo suggests. But the preferred location for JAVA_HOME or any system variable is /etc/environment . Open /etc/environment in any text editor like nano or gedit and add the following JAVA_HOME="/usr/lib/jvm/open-jdk" (java path could be different) Use source to load the variables, by running this command: source / etc / environment Then check the variable, by running this command: echo $JAVA_HOME cat environment PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games" JAVA_HOME="/usr/lib/jvm/java-7-openjdk-i386/"

du -h --max-depth=1 | du -skh

du -h --max-depth=1 11M    ./mysql-connector-java-5.1.35 597M    ./wls12130 61M    ./SoapUI-5.1.3.old 48K    ./gedit-tm-autocomplete-1.0.5 41M    ./axis2-1.6.2 572K    ./5665OS_Codebundle 1.1G    . du -skh * - shows sum of folders (Works on SOlaris) gzip -l archive.csv.gz - shows the ratio% and size of uncompressed file gunzip archive.csv.gz - to unzip