Shell Scripting
Online tutorials:
- http://www.shellscript.sh/
- http://supportweb.cs.bham.ac.uk/docs/tutorials/docsystem/build/tutorials/unixscripting/unixscripting.html
Basics
In a nutshell, shell scripts encapsulate a series of commands that we'd normally run in the shell.
With shell scripts, a sequence of such commands can be written, saved in a text file, and ran whenever needed, sparing us from having to type long commands every time we want to perform that task.
There are different types of shells, with some differences between them.
The shell used in a script can be found in the first line (shebang #!
).
Different shells:
- sh
which sh
Bourne shell - bash
which bash
Bourne-again shell - csh
which csh
C shell
which
will be indicated its location on the machine you are running.
example
#!/bin/sh
# firts line is not comment
# "#!/bin/sh" indicate what shell to use: in this case sh or Bourn sh
date # date command
loggedusers=`users` # result from users is given to variable loggedusers
for i in $loggedusers # loop through each of the logged in users
do echo $i | figlet # echoing ech item to figlet
echo 'in da house' | cowsay # echo some corny line #cow say
sleep 1 # sleeping for 1 sec
done
cron-jobs
Shell scripts can be easily timed (run every hour, every Sunday 5am, every minute, etc) using a [1]
Each Linux/UNIX user can write cron-jobs by editing the cron file with crontab -e