Shell Scripting: Difference between revisions
Andre Castro (talk | contribs) No edit summary |
Andre Castro (talk | contribs) No edit summary |
||
Line 4: | Line 4: | ||
=Basics= | =Basics= | ||
In | 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. | 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. | ||
Line 16: | Line 16: | ||
<code>which</code> will be indicated its location on the machine you are running. | <code>which</code> will be indicated its location on the machine you are running. | ||
== example == | |||
<source lang="bash"> | <source lang="bash"> | ||
#!/bin/sh | #!/bin/sh | ||
Line 31: | Line 32: | ||
done | done | ||
</source> | </source> | ||
== cron-jobs == | |||
Shell scripts can be easily timed (run every hour, every Sunday 5am, every minute, etc) using a [https://en.wikipedia.org/wiki/Cron] | |||
Each Linux/UNIX user can write cron-jobs by editing the cron file with <code>crontab -e</code> | |||
[[Category:Cookbook]] | [[Category:Cookbook]] |
Latest revision as of 16:18, 25 October 2016
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