Shell Scripting: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 18: Line 18:
<source lang="bash">
<source lang="bash">
#!/bin/sh
#!/bin/sh
# firts line is not comment
# firts line is not comment
# "#!/bin/sh" indicate what shell to use: in this case sh or Bourn sh
# "#!/bin/sh" indicate what shell to use: in this case sh or Bourn sh

Revision as of 16:13, 25 October 2016

Online tutorials:

Basics

In essence 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.

#!/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