Linux   Linux – Bash scripting useful commands and tips



Some examples of very simple commands and features that become very powerful when used together. In future posts we'll see real examples of bash scripts used to create custom nagios checks/plugins.

Concatenate files – cat

# cat FILE1
this is the file1

# cat FILE2
this is the file2

# cat FILE1 FILE2
this is the file1
this is the file2

 

Output redirection – >  2> &>

Redirect standard output (stdout)

# cat FILE1 FILE2 > CONCATENATEDFILE
# cat CONCATENATEDFILE
this is the file1
this is the file2

This one is not redirected because the msg is printed in the error ouput (stderr), not the stdout

# cat NONEXISTINGFILE
cat: NONEXISTINGFILE: No such file or directory

# cat NONEXISTINGFILE > ERROR
cat: NONEXISTINGFILE: No such file or directory

Redirect error output (stderr)

# cat NONEXISTINGFILE 2> ERROR
# cat ERROR
cat: NONEXISTINGFILE: No such file or directory

 

Count number of lines – wc

# cat CONCATENATEDFILE | wc -l
2

 

Assign command output to a variable – VAR=`command`

# NUMLINES=`cat CONCATENATEDFILE | wc -l`
# echo $NUMLINES
2

 

Parse data fields separated by character – cut

# cat USERDATA
user1;user1@mail.com;This is the user 1;COD1111

# cat USERDATA | cut -d";" -f1,4
user1;COD1111
# cat USERDATA2
user2,user2@mail.com,This is the user 2,COD2222

# USERCODE=`cat USERDATA2 | cut -d"," -f4`
# USEREMAIL=`cat USERDATA2 | cut -d"," -f2`

# echo "The email address of the user with ID $USERCODE is $USEREMAIL"
The email address of the user with ID COD2222 is user2@mail.com

 

Parse data fields separated by character – awk

# cat USERDATA | awk -F";" '{ print $1" "$4 }'
user1 COD1111

# cat USERDATA2 | awk -F"," '{ print "The email address of the user with ID " $4 " is " $2 }'
The email address of the user with ID COD2222 is user2@mail.com

 

Remove blank lines – sed

# cat LINES
LINE1

LINE2
LINE3


LINE4

LINE5
LINE6
# cat LINES | sed '/^$/d'
LINE1
LINE2
LINE3
LINE4
LINE5
LINE6

 

Remove blank characters – tr

# cat VALUES
VALUE1: 008709
VALUE2: 005209

# cat VALUES | cut -d":" -f2
 008709
 005209

# cat VALUES | cut -d":" -f2 | tr -d " "
008709
005209

 

Last command exit status – $?

Success – $?=0

# cat EXISTINGFILE
# echo $?
0

Error – $?=1

# cat NONEXISTINGFILE
cat: NONEXISTINGFILE: No such file or directory
# echo $?
1

 

Compare files – diff

FILE1 != FILE2

# cat FILE1
1
2
3
4

# cat FILE2
1

3
4

# diff FILE1 FILE2
2c2
< 2
---
>
# echo $? 
1

FILE1 = FILE3

# diff FILE1 FILE3
# echo $?
0

 

Sort lines – sort

# cat FILE1
3
1
2

# cat FILE2
2
3
1

# diff FILE1 FILE2
0a1
> 2
3d3
< 2
# cat FILE1 | sort
1
2
3

# cat FILE2 | sort
1
2
3
# cat FILE1 | sort > FILE1-SORT
# cat FILE2 | sort > FILE2-SORT
# diff FILE1-SORT FILE2-SORT
# echo $?
0

Leave a Reply

Your email address will not be published. Required fields are marked *