AnalyzingData/Primer_ShellScripting - Meg Wiki

You can't save spelling words.

Clear message
location: AnalyzingData / Primer_ShellScripting

A Quick Primer on Linux and Shell Scripting

You are assumed to know how to set-up and use a VNC viewer, and have some basic knowledge of Linux or Unix (see e.g. our unix survival guide). The following is just to get you on the way. Click here, for example, for more on shell scripting.

Some Practical Tricks...

  • You can get help on almost any Linux command typing "man command", e.g. "man rm".

  • You can use the cursor keys "Up" and "Down" to scroll through your recent Linux commands (and re-use or modify them).

  • typing "history" shows you the list of your recent commands

  • If you start typing (e.g. "matl") and then press the "tab" key, Linux will complete to the next uniquely identifyable command (i.e. "matlab" in the example).

  • The same holds for file names: type "ls myfilen" and then "tab" may complete to something like "ls myfilename.txt" (unless there are also other files with exactly the same beginning)

  • If you want the output of a command in a text file, you can use the ">" symbol. For example, "ls * > filelist.txt" will write the contents of your current directory to the file filelist.txt. ">" overwrites, ">>" appends.

  • You can copy&paste in Linux: Mark something by moving the cursor over some text while holding the left mouse button pressed. Then click the mouse wheel (or middle button). The marked text should appear where you last left the cursor (e.g. command window or text editor).

  • Sometimes you may have problems executing scripts that were created as text files in Windows - if in doubt, use "dos2unix" to convert these files to unix code (e.g. "dos2unix -o myfile.txt").

Basics of Linux

You probably already know commands such as ls, cd, pwd or mkdir which you usually execute in a command window:

FirstCommands.jpg

See below for some tricks that may be useful when you work with Linux a lot.

Basics of Shell Scripting

If you want to display text on the screen, you can use the "echo" command. If you want to display this text very often (don't ask for the reason), then you can write it in a script.

Open a new file (e.g. using the text editor "nedit"), put "#!/bin/sh" at the top (so the file knows it's a script and how to interpret the following commands), and add the command you would like execute.

Save the file. You have to make the file executable by changing the permissions ("chmod" command).

FirstScript.jpg

This is boring. You want to be more flexible. In many scripts, you want to execute the same command(s) for a number of different subjects. For this, there is the "for" loop:

LoopScript.jpg

Nothing can stop you now!

CbuMeg: AnalyzingData/Primer_ShellScripting (last edited 2013-03-08 10:02:29 by localhost)