Tuesday, February 23, 2016

Linux shell with effective way (ways to use it)

Here we are going to focus on linux / unix shell with specific symbols which we can use the shell in effective manner.

The symbols are,
|, \ , &, &&, [], [[]], (), {} 

These symbols are very usefull in bash.

Using pipe symbol in shell

| the pipe sign can use to pipe the output of a command. simply it's forwarding what have in stdout in to stdin.

ex:
ls /etc | grep gtk

here we get the output of the ls /etc and put it into the grep gtk  command as an input to that command.


Logical Operators.




Ampersand Operator (&)

The function of ‘&‘ is to make the command run in background. Just type the command followed with a white space and ‘&‘. You can execute more than one command in the background, in a single go.

Run one command in the background:

root@localhost:~$ ping ­www.google.com &

Run two command in background, simultaneously:
root@localhost:~$ apt-get update & apt-get upgrade &


2. semi-colon Operator (;)

The semi-colon operator makes it possible to run, several commands in a single go and the execution of command occurs sequentially.
root@localhost:~$apt-get update ; apt-get upgrade ; mkdir test

The above command combination will first execute update instruction, then upgrade instruction and finally will create a ‘test‘ directory under the current working directory.

3. AND Operator (&&)

The AND Operator (&&) would execute the second command only, if the execution of first command SUCCEEDS, i.e., the exit status of the first command is 0. This command is very useful in checking the execution status of last command.
For example, I want to visit website google.com using link command, in terminal but before that I need to check if the host is live or not.
root@localhost:~$ ping -c3 www.google.com && links www.google.com

4. OR Operator (||)

The OR Operator (||) is much like an ‘else‘ statement in programming. The above operator allow you to execute second command only if the execution of first command fails, i.e., the exit status of first command is ‘1‘.

For example, I want to execute ‘apt-get update‘ from non-root account and if the first command fails, then the second ‘links www.google.com‘ command will execute.

tecmint@localhost:~$ apt-get update || links www.google.com

In the above command, since the user was not allowed to update system, it means that the exit status of first command is ‘1’ and hence the last command ‘links www.google.com‘ gets executed.

What if the first command is executed successfully, with an exit status ‘0‘? Obviously! Second command won’t execute.

tecmint@localhost:~$ mkdir test || links www.google.com

Here, the user creates a folder ‘test‘ in his home directory, for which user is permitted. The command executed successfully giving an exit status ‘0‘ and hence the last part of the command is not executed.


5. NOT Operator (!)


The NOT Operator (!) is much like an ‘except‘ statement. This command will execute all except the condition provided. Let's assume a directory which exists some files with several .html files. We need to delete all files without deleting .html files. So we can use the following command for this.
 
root@localhost:~$ rm -r !(*.html)

do ls for confirm the files are exists.

6. AND – OR operator (&& – ||)


The above operator is actually a combination of ‘AND‘ and ‘OR‘ Operator. It is much like an ‘if-else‘ statement.

For example, let’s do ping to google.com, if success echo ‘Verified‘ else echo ‘Host Down‘.
tecmint@localhost:~/tecmint$ ping -c3 www.tecmint.com && echo "Verified" || echo "Host Down"

Sample Output

PING www.google.com (212.71.234.61) 56(84) bytes of data. 64 bytes from www.google.com (212.71.234.61): icmp_req=1 ttl=55 time=216 ms 64 bytes from www.google.com (212.71.234.61): icmp_req=2 ttl=55 time=224 ms 64 bytes from www.google.com (212.71.234.61): icmp_req=3 ttl=55 time=226 ms --- www.google.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2001ms rtt min/avg/max/mdev = 216.960/222.789/226.423/4.199 ms Verified

Now, disconnect your internet connection, and try same command again.

root@localhost:~/tecmint$ ping -c3 www.google.com && echo "verified" || echo "Host Down"

Sample Output

ping: unknown host www.google.com Host Down


\ Back slash

this operator will use when we writing long statement in the shell and if we want to break it in to several lines.


Test and [ ]

Test function is used in many language for check the condition of a expression (evaluate a expression). [ ] is similar to test.

[ "abc"="abc" ]

This tells check whether abc equals abc. if they are equals it returns 0(expression true). else 1(expression result False).

ex :

root@localhost:~$if [ "abc"="abc" ];then echo "equals";fi
root@localhost:~$equals

[[ ]] is same as [ ], but using it we can include number of commands with other operators.

We can use [ ] this for check whether if a file exists, check whether it's a file or a directory and so on. Here is a list of them.

some common file test

OperatorCharacteristic
-dDirectory
-eExists (also -a)
-fRegular file
-hSymbolic link (also -L)
-pNamed pipe
-rReadable by you
-sNot empty
-SSocket
-wWritable by you
-NHas been modified since last being read

Testing pair of files

Operator    True if
-nt                Test if file1 is newer than file 2. The modification date is used for this and         the next comparison.

-ot                Test if file1 is older than file 2.

-ef                Test if file1 is a hard link to file2.

 



You can get the actual result of test function using $?

ex:
root@localhost:~$[ -e /home ]; echo $?
root@localhost:~$0

Using parentheses in Shell

Parentheses can use for certain no. of instances.

1) Executing group of commands in a different shell(subshell) we can use ()

ex:
root@localhost:~$pwd
/home/user
root@localhost:~$(cd /tmp; pwd)
/tmp
root@localhost:~$pwd
/home/user

2) Defining an array,

array=(a b c d)

To iterate all the values in array use the following comand

echo ${array[@]}

arrayname[@] will result all values in the array.

3) Command substitution

Reassigns the output of a command. Even multiple command.

ex ;
var=$(ls /home/user/Documents)
echo $var
Output reassigned to var variable and it can be printed
similar to ' '

We can rewrite above command as follow

var=$'ls /home/user/Documents'
echo $var


Double parantheses

1) Integer arithmatic

((i=23))

2) Integer arithmatic with variable

v=$((45+34))
echo $v
 79

3) Variable increment or  decrement

var=1
((var++))
echo $var
2

4) In functions

for ((i=0; i<10; i++))



Curly braces

This is doing big part in bash.

1) Truncate the contents of a variable

$ var="abcde"; echo ${var%d*}
abc


This will remove all characters after "d" letter with the "d". It looks from right to left. That mean if d has number of occurrences, from the last "d" characters will removed.





2) Make substitutions similar to sed

$ var="abcde"; echo ${var/de/12}
abc12


3) Use a default value

$ default="hello"; unset var; echo ${var:-$default}
hello


4) And also can use for iterate strings as loops

$ echo f{oo,ee,a}d
food feed fad


$ mv error.log{,.OLD}
(error.log is renamed to error.log.OLD because the brace expression
expands to "mv error.log error.log.OLD")


$ for num in {000..2}; do echo "$num"; done
000
001
002


$ echo {00..8..2}
00 02 04 06 08



$ echo {D..T..4}
D H L P T

$Variable="abcdef"
$ echo Variable: ${VARIABLE}123456
    Variable: abcdef123456


5) Run block of code in the same shell

$ { date; top -b -n1 | head ; } >logfile
    # 'date' and 'top' output are concatenated,
    # could be useful sometimes to hunt for a top loader )

Deleting certain files in a directory

Assume a directory with three files named a.txt, b.txt and c.txt
You need to delete only a.txt and b.txt

rm /home/user/Documents/{a.txt,b.txt}

this will remove only those files.


Summary of all

  •  Brackets

    if [ condition ] # test construct
    if [[ condition ]] # extended test construct, similar to single, but is bash builtin
    Array[index]=element1 # array initialization
    [a-z] # range of characters in a regular expression

  • Curly Brackets

    ${variable} # parameter substitution
    ${!variable} # indirect variable reference
    { cmd1; cmd2; …; cmdN; } # block of code
    {string1,string2,…,stringN} # brace expansion
    {a..z} # extended brack expansion
    {} # text replacement, after find and xargs

  • Parentheses

    ( cmd1; cmd2; …; cmdN ) # command group executed within a subshell
    Array=(element1 element2 … elementN) # array initialization
    result=$(cmd) # command substitution like “
    >(cmd) # process substitution
    <(cmd) # process substitution

  • Double Parentheses

    (( var = 78)) # integer arithmetic
    var=$(( 20 + 5 )) # integer arithmetic with viriable assignment
    (( var++ )) (( var– )) # c-style variable increment/decrement
    (( var0 = var1<98?9:21 )) # c-style trinary operation

Hope this is very useful while using bash.


Referenced links ;
http://unix.stackexchange.com/questions/32210/using-single-or-double-bracket-bash
https://shunchaowang.wordpress.com/2015/03/06/bash-parentheses/
http://www.linuxjournal.com/content/bash-brace-expansion
http://stackoverflow.com/questions/8748831/bash-when-do-we-need-curly-braces-in-variables
http://www.tecmint.com/chaining-operators-in-linux-with-practical-examples/
http://mywiki.wooledge.org/BashFAQ/031
http://stackoverflow.com/questions/2188199/how-to-use-double-or-single-bracket-parentheses-curly-braces
https://bash.cyberciti.biz/guide/Logical_OR

No comments:

Post a Comment