Shell Programming FAQ - 1: Useful Shell commands and scripts

Saturday, September 6, 2008

This is the first of my Unix/Linux/Solaris Shell Programming FAQ series where I want to list some useful shell scripts/commands which at times challenged me, some may be already known to you and some may not be, but nonetheless I feel its my duty to share some not so trivial things to search and find that easily sometimes, also I needed a quick reference of these which may be useful for coders including me in future, so here we go.

1. Bitwise operators in Shell
$ a=10
$ let "a <<= 1"
$ echo $a
20
$ let "a >>= 1"
$ echo $a
10
The above example uses bit shifting operators in shell, the first is the left shift operator, which multiplies a given integer by 2, the second is the right shift operator which divides the number by 2, similarly one can also use addition, subtraction, increment operators with a similar syntax.

2. Using Arrays in Ksh and Bash

Here we will see a shell script which initializes and prints array elements in ksh and bash shell.

Ksh array:
# KSH_ARRAY is the array name followed by array elements
set -A KSH_ARRAY one two three four five

print "Length of KSH_ARRAY = ${KSH_ARRAY[@]}"
print "Printing array elements...."

for index in ${KSH_ARRAY[@]}
do
print $index
done
Bash array:
# BASH_ARRAY is the array name followed by array elements
BASH_ARRAY=( one two three four five)

echo "Length of BASH_ARRAY = ${BASH_ARRAY[@]}"
echo "Printing array elements...."

for index in ${BASH_ARRAY[@]}
do
echo $index
done

3. Search the first occurrence of a string or a pattern in a file and exit

When you want to search for the first occurence of a string in a file and exit, the below command may be useful, the file input.txt has the following contents, for more details in this visit here.

Input file: input.txt

Start
Line: 1
Line: 2
Line: 3
Line: 4
Line: 5
End

To search for the first occurence of the word "Line:" from the above file and exit, use the below grep and awk combination,
cat input.txt | grep "Line:" | awk '{ print $2; exit }' (or) awk '/Line:/ { print $2;exit }' input.txt

4. Send mails with text attachments
cat <TEXT_FILE> | mailx -s <SUBJECT> <EMAIL_ADDRESS>
Example:
cat attachment.txt | mailx -s "This is the subject of email"  name@domain.com
Note: If you want to send emails to multiple recipients, then one can add as many email IDs in a text file and use the below command.

$ cat > recipient.txt
name1@domain.com
name2@domain.com
name3@domain.com
name4@domain.com
cat attachment.txt | mailx -s "This is the subject of email"  `cat recipient.txt`

5. Adding lines before the start or after the end of the file: See here.

6. Printing newline characters in shell (or printing newline characters in a variable)

Although its discussed in (5), I just want to compare how printing newline characters works in different shells like bash, ksh and sh.

1. Printing newline characters in bash shell:

Use echo -e command.
bash-3.00$ var="This is the first line\nThis is the second line"
bash-3.00$ echo $var
This is the first line\nThis is the second line
bash-3.00$ echo -e $var
This is the first line
This is the second line
bash-3.00$
2. Printing newline characters in sh and ksh:

sh and ksh by default outputs newline without any specific options, therefore its a straightforward solution.
bash-3.00$ ksh
$ var="This is the first line\nThis is the second line"
$ echo $var
This is the first line
This is the second line
$
bash-3.00$ sh
$ var="This is the first line\nThis is the second line"
$ echo $var
This is the first line
This is the second line
$
Lets see more interesting/useful commands in my next Shell Programming FAQ, lots to come up for sure.

5 comments:

lee said...

,----
| To search for the first occurence of the word "Line:" from the above
| file and exit, use the below grep and awk combination,
|
| cat input.txt | grep "Line:" | awk '{ print $2; exit }' (or) awk '/Line:/ { print $2;exit }' input.txt
`----


actually, the grep man page is your friend:

cat input.txt | grep -m1 "Line:"

Prasanna Seshadri said...

Oh, Thanks for that, makes it simple, the only thing is grep -m1 doesn't work in Solaris, but its good for Linux.

Anonymous said...

Printing newlines:
$ echo -e Hello,\\nworld!
Hello,
world!

Anonymous said...

The "cat foo | grep" idiom is so common, and usually not necessary. It often actually limits the power of grep!

We should prefer "grep foo".

Prasanna Seshadri said...

Yes using grep only is the right way to go.


Copyright © 2008 Prasanna Seshadri, www.prasannatech.net, All Rights Reserved.
No part of the content or this site may be reproduced without prior written permission of the author.