News

Shell scripting for system administrators: the basics

For system administrators shell scripting can be a very useful way to drastically improve workflow. Join Swayam Prakasha to find out how you can employ some fundemental tips and techniques to make your life easier…

SS Figure 1

In the previous example, we have used numbers as patterns, but patterns can be more complex, too. Please refer to the man page of sh for more details. The following example illustrates the do…while construct:
[sourcecode language=”bash”]#!/bin/sh

COUNT=10
# bash while loop
while [ $COUNT -gt 0 ]; do
echo Value of count is: $COUNT
let COUNT=COUNT-1
done
[/sourcecode]
Starting with an initial value of ten, we print its value till it becomes greater than zero.
When we execute the above script, the output looks like:
[sourcecode language=”bash”][root@localhost ~]# ./script_9.sh
Value of count is: 10
Value of count is: 9
Value of count is: 8
Value of count is: 7
Value of count is: 6
Value of count is: 5
Value of count is: 4
Value of count is: 3
Value of count is: 2
Value of count is: 1
[root@localhost ~]#
[/sourcecode]
One can also execute commands inside a shell script. The following example illustrates this:
[sourcecode language=”bash”]!#/bin/sh
echo `uname -o`
[/sourcecode]
When we execute this script, the following output will be displayed:
[sourcecode language=”bash”]GNU/Linux
[/sourcecode]

[twitter username=”linuxusermag”]

×