Titel:

A free Basic UNIX-training with muLinux

Startseite
english
  
ISBN: 3937514872   ISBN: 3937514872   ISBN: 3937514872   ISBN: 3937514872 
 
  Wir empfehlen:       
 
[Back] [Home] [Forward]       http://rowa.giso.de  

A free Basic UNIX-training with muLinux

To save this lesson onto a floppy-disc, we insert a floppy into the floppy-drive, mount the device to /a.
/# mount /dev/fd0 /a
/# _

Easy Shell Scripts

The standard commands offered by a Unix such as muLinux are much more comprehensive than the commands offered in DOS. Many tasks that need different DOS programs to do can be executed without any problems in a few Unix commands. And above that, individual commands can be combined in script-files, which are comparable with Batch-files in DOS. Because of that much more comprehensive language, and the flexibility offered by shell scripts, it is a much more comfortable environment to program in. In a way Unix commands can be used as modules for more extended tasks, so many things don't need to be programmed from the beginning (like in DOS). And finally, under X-Windows, these scripts can be used to adding some graphical skin to the bones of the shell (X-Windows is a graphical environment for Unix, and is on one of the muLinux add-on disks).

In our first dive into shell scripting, we will create some small programs, to learn some of the possibilities and advantages of shell-scripts.

A script is a text, which contains a list of commands, which are going to be interpreted by the shell. In other words, in every line of the textfile, we can write a Unix-command like ls or date. Then these will (mostly) be executed one after the other.

To save our scripts permanently, our floppy-disc needs to be mounted to /a. We create a directory with the name bin for our script-files.

/# cd /a
/a# mkdir bin
/a# cd bin
/a/bin# _

Steps to create and finish shell-scripts

  1. Write the commands with a text-editor (like  vi filename ) in the Script-file.
  2. The script file we gonna made into an executable file with the command  chmod u+x filename.
  3. Executing the script-file is done by:  ./filename 

A small example will help review this. Firstly the scriptfile should clear the screen with: clear and then the already known command df should be executed. The script-file should be called test. By the way, you can call script-files anything you want.

/a/bin# vi test

clear
df
~
~
~
~
~
~
~
~
~
"test" [New File] 2 lines, 9 chars

We save our work and leave vi:

[Esc] :wq

Now comes the second step. We need to make our file executable, so that users can run it. By using ls -l we can see that the file isn't yet an executable. If we go ...

/a/bin# chmod u+x test

The file is now executable. We can check this with ls -l:

/a/bin# ls -l test
1 -rwxr--r--  1  root root  9 Nov 17 17:59 test

The x shows us that we as a user have the permissions to execute this file. And this is what we are gonna do:

/a/bin# ./test
The output of df: all mounted devices and the
space-information on it.

The dot defines the parent working directory. We also can execute the command like this:

/a/bin# /a/bin/test

Modification of our script

We want to modify our script. We don't want to run vi every time, that's why we use a second console

[Alt] [F2]

Also there we log in as root and fire up the text editor vi:

login: root
/# cd /a/bin
/a/bin# vi test

So, now we can edit on the second console our script, save it ([Esc] :w) and change to console 1 ([Alt] [F1]) to execute it. For that we use again the repeat-command feature of Unix (Cursor-up). To continuing editing we return to console 2 ([Alt] [F2]).

Comments

Comment lines should be built into a script to help you or someone else review the code. They are introduced by a hash # or colon:. But be careful with shell-scripts with comments in the first line. Sometimes these will be executed as ordinary commands. The best thing is to leave the first line empty.

      Leave this blank
# This is a comment-line!
clear
df
~
~
~
~
~
~
~
~

Outputting text

With the command echo we can output text.

# This is a comment-line!
clear
echo " info about the mounted devices:"
df
~
~
~
~
~
~
~

echo Without parameters this outputs a empty line. But we can format our script to get our output a bit fancier.

# This is a comment-line!
clear
echo
echo "info about the mounted devices:"
df
echo
~
~
~
~
~

Parameters

Parameters can also be given to scripts. For example, we want to give parameters to the command df. To get all of our parameters from the command-line we need the signs $*

# This is a comment-line
clear
echo
echo "info about the mounted devices:"
df $*
echo
~
~
~
~
~

Now we can execute our script with parameters. The parameter /a shows us the info about the mounted device mounted on mountpoint /a

/a/bin# ./test /a

The parameters /a and /tmp show us the info about the devices mounted on these mountpoints.

/a/bin# ./test /a /tmp

There can also be given special devices as parameters.

/a/bin# ./test /dev/ram0 /dev/ram1

Exercise

Create a script with the name formata. This script should make formatting floppy disks easier. For this you need the commands fdformat and mkfs.ext2.

Hint: the floppy can't be mounted while it is being formatted! And please don't format either your work floppy or your muLinux bootdisc. And finally a copy of the error-free script should be put onto your work floppy.

So, this is it for now with shell-scripts.

To finish this lesson we unmount our work floppy.
/a# cd /
/# umount /a
/# _

[Back] [Home] [Forward]      
Robert.Warnke@giso.de (copyleft) Robert Warnke, Berlin (Germany) - You can write me in English. | http://rowa.giso.de | translated by mek, correctd by Ian Whitchurch
  
Ubuntu & Kubuntu Linux 9.04 "Jaunty Jackalope". Doppel-DVD mit Ubuntu und Kubuntu 9.04 als Live- und Vollversionen, inkl. Einsteiger-Handbuch
von Open Source Press
Siehe auch:
Ubuntu 9.04 Jaunty Jackalope: Das Einsteigerbuch
Ubuntu GNU / Linux: Das umfassende Handbuch, a...
Linux lernen mit Ubuntu
Ubuntu & Kubuntu Linux 9.10 "Karmic Koala" (Doppel-DVD inkl. Han...
Ubuntu 9.10 Karmic Koala: Das Einsteigerbuch
Einstieg in Linux: Eine distributionsunabhän...
 
   
 
     

Zurück zur Themenseite:
StudyPaper.com/Startseite/Computer/Informatik

Das Setzen von Verweisen (Links) auf diese Seite ist gestattet und bedarf keine vorherige Absprache.
   
  Startseite  |  english  |  Bookmark setzen  |  Webseite weiterempfehlen  |  Copyright ©  |  Impressum