Concise bash programming skills

  sonic0002        2012-11-06 10:38:42       10,393        2    

The following are some concise bash programming skills which we may need in our daily programming work.

1. Check status of command execution

The usual way:

echo abcdee | grep -q abcd
 
if [ $? -eq 0 ]; then
    echo "Found"
else
    echo "Not found"
fi

Concise way:

if echo abcdee | grep -q abc; then
    echo "Found"
else
    echo "Not found"
fi

Of course you can remove if...else with following code

[Sun Nov 04 05:58 AM] [kodango@devops] ~/workspace 
$ echo abcdee | grep -q abc && echo "Found" || echo "Not found"
Found

2. Redirect standard out and standard error to /dev/null

The usual way:

grep"abc" test.txt 1>/dev/null 2>&1

The wrong way:

grep"abc" test.txt 2>&11>/dev/null

The concise way:

grep"abc" test.txt &>/dev/null

3. Use of awk

Take an actual example. Get Xen DomU's ID.

The usual way:

sudo xm li |grep vm_name |awk'{print $2}'

The concise way:

sudo xm li |awk'/vm_name/{print $2}'

4. Concatenate all lines of a file with comma(,)

Assume file contents are

[Sat Nov 03 10:04 PM] [kodango@devops] ~/workspace 
$ cat /tmp/test.txt 
1
2
3

Using sed command:

[Sat Nov 03 10:14 PM] [kodango@devops] ~/workspace 
$ sed ':a;$!N;s/\n/,/;ta' /tmp/test.txt 
1,2,3

The concise way:

[Sat Nov 03 10:04 PM] [kodango@devops] ~/workspace 
$ paste -sd, /tmp/test.txt 
1,2,3

5. Filter duplicated line

Assume file contents are:

[Sat Nov 03 10:16 PM] [kodango@devops] ~/workspace 
$ sort /tmp/test.txt 
1
1
2
3

The usual way:

[Sat Nov 03 10:16 PM] [kodango@devops] ~/workspace 
$ sort /tmp/test.txt | uniq
1
2
3

The concise way:

[Sat Nov 03 10:16 PM] [kodango@devops] ~/workspace 
$ sort /tmp/test.txt -u
1
2
3

6. grep to search a word

Assume each line of a file contains an IP address. For example:

[Sat Nov 03 10:20 PM] [kodango@devops] ~/workspace 
$ cat /tmp/ip.list 
10.0.0.1
10.0.0.12
10.0.0.123

Using grep to find the IP 10.0.0.1, the usual way:

[Sat Nov 03 10:22 PM] [kodango@devops] ~/workspace 
$ grep '10.0.0.1\>' /tmp/ip.list 
10.0.0.1

The concise way(Actually this way is not necessary more concise, just want to explain that -w is useful)

[Sat Nov 03 10:23 PM] [kodango@devops] ~/workspace 
$ grep -w '10.0.0.1' /tmp/ip.list 
10.0.0.1

7. Use of $1,$2...

Assume we just want to use $2,$3, not $1, the usual way:

shift

echo"$@"

The concise way:

echo"${@:2}"

8. Default value of a parameter

If we don't provide a value for one one parameter, we may give it a default value, we can do it like:

arg=$1
 
if [ -z "$arg" ]; then
   arg=0
fi

The concise way:

arg=${1:-0}

9. Use of - in bash

If we want to search whether a string contains -i, we may use:

[Sat Nov 03 10:45 PM] [kodango@devops] ~/workspace 
$ echo 'abc-i' | grep "-i"
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
 
[Sat Nov 03 10:45 PM] [kodango@devops] ~/workspace 
$ echo 'abc-i' | grep "\-i"
abc-i

The concise way:

[Sat Nov 03 10:45 PM] [kodango@devops] ~/workspace 
$ echo 'abc-i' | grep -- -i
abc-i

10. Assign formatted output of printf to an variable

If we want to convert a decimal number to its hex format, the usual way:

[Sat Nov 03 10:55 PM] [kodango@devops] ~/workspace 
$ var=$(printf '%%%02x' 111)

The concise way:

[Sat Nov 03 10:54 PM] [kodango@devops] ~/workspace 
$ printf -v var '%%%02x' 111

Here is the help of printf

[Sat Nov 03 10:53 PM] [kodango@devops] ~/workspace 
$ help printf | grep -A 1 -B 1 -- -v
printf: printf [-v var] format [arguments]
    Formats and prints ARGUMENTS under control of the FORMAT.
--
    Options:
      -v var	assign the output to shell variable VAR rather than
    		display it on the standard output

11. Print some lines of a file

Print the first line:

head-1 test.txt

Print the second line:

sed-n'2p' test.txt

Print 2nd to 5th line

sed -n '2,5p' test.txt

Print the ;ast second line

$ tail-2 test.txt |head-1

$ tac test.txt |sed-n'2p'

12. Use let and (( )) to do arithmatic calculation

Do ++ operation,the usual way:

a=1

a=`expr a + 1`

The concise way:

a=1
let a++
let a+=2

13. Get real filename of a softlink

You may do it like :

[Sat Nov 03 11:12 PM] [kodango@devops] ~/workspace 
$ ls -l /usr/bin/python | awk -F'->' '{print $2}' | tr -d ' '
/usr/bin/python2

If you know a command named readlink. Then

[Sat Nov 03 11:13 PM] [kodango@devops] ~/workspace 
$ readlink /usr/bin/python
/usr/bin/python2

14. Get ASCII code of a character

[Sat Nov 03 11:14 PM] [kodango@devops] ~/workspace 
$ printf '%02x' "'+"
2b
[Sat Nov 03 11:30 PM] [kodango@devops] ~/workspace 
$ echo -n '+' | od -tx1 -An | tr -d ' '
2b

15. Clear contents of a file

The usual way:

echo""> test.txt

The concise way:

> test.txt

More to come...

Source : http://kodango.me/simple-bash-programming-skills

TIP  BASH  SKILL 

       

  RELATED


  2 COMMENTS


moncler jackets [Reply]@ 2012-11-12 13:28:22
Please let me know if you’re looking for a article author for your blog. You have some really good articles and I think I would be a good asset. If you ever want to take some of the load off, I’d really like to write some material for your blog in exchange for a link back to mine. Please shoot me an e-mail if interested. Thank you! moncler jackets http://monclerjackets2013.webnode.fr/
Anonymous [Reply]@ 2018-04-06 10:19:15

"12. Use let and (( )) to do arithmatic calculation" is missing (()) examples.



  RANDOM FUN

Levels of programmer