Hackers Dot NL

Incredibly Simple Shell Programming

by Administrator on 21:12, under Tutorials

All to often the main complaint I have heard from people who have newly switched to Linux is the command line. Most complain that Linux MUST be an inferior OS because it relies much to heavily on the command line. You will also hear detractors of Linux say the same thing. They do not realize that in the command line lies true power, only once you have mastered the art of the command line, and the shell, can you truly begin to extract real power and flexibility from the machine. <br> If you are not familiar with the term ‘shell’ I will explain. The shell is your interface to the machine, it is how you interact and communicate with Linux. In DOS this is most often called a command interpreter, however the DOS command line and the Linux shell are only distant third cousins at best, the Linux shell blows away the DOS command line. In fact, the DOS command line under todays Windows OS is pretty much unusable except for very simple tasks (well its never been that great to begin with). Put simply, when you type in a command the shell interprets it and spits out a result. You can combine many of these commands into a file, <br>called a shell script, and execute them at once. This is called shell programming and is a powerful feature of Linux (and all Unix’s). You may also combine many of the commands on a single line in order to execute multiple statements at a time, <br> simply use plenty of ;’s to seperate the individual commands!

If you are coming from MS Windows, you know there is only one shell prompt available, and that is the DOS prompt. There are many shells available for Linux and which one you use depends on your personal preferences. The most common shell is use today is the BASH shell, which stands for Bourne Again Shell and is a free implementation of the Bourne shell. Here is a list of other common shells: <br> csh – The c shell, a shell with a c like language syntax. tcsh – a C shell with added features such as command line completion. ksh – the korn shell (no, not related to the band) zsh, pdksh – both based on the korn shell, with improvements This is not a complete list, there are other shells available for Linux, this tutorial will concentrate on only the BASH shell since it is the most common shell being used today. Make sure you are using the BASH shell before we begin, type this on the command line to make sure: <br> bash ENTER <br> <br> We will learn the very basics of shell programming, nothing fancy. If you already know shell programming you probably will want to skip this as it only scratches the surface. If you do not know shell programming the get ready to learn a little (I mean a little) about one of Linux’s most powerful features. <br> Ok, now we are ready to begin exploring shell programming a bit more. <br> Ok, we have the basics (and I mean very basics) out of the way, we can begin our adventure into shell programming now. As you already know, when you type a command on the BASH command line Linux interprets the command and executes it, or spits out an incredibly detailed and informative error message (sarcasm mode=1). However, did you know you can put a whole list of these commands into a file and execute the file. Linux will then execute all your commands just as you typed them? Which is exactly what we will do next <br> Let us say, for the sake of example, that when you initially log into your machine you always go through the exact same routine. That routine is this: <br> Mount the CD with this command: mount -t auto /dev/cdrom /cdrom List who is on the system with this command: who Start your mail program with this command: mymailprog It would get pretty tedious to type in those three commands each time you logged in, there is an easier way, called a shell script. We could create a text file with everyones favorite editor vi which looked something like this: <br> <b> #!/bin/sh mount -t auto /dev/cdrom /cdrom who mymailprog </b> <br> We could call the text file myscript.sh and now, when the login we could just type myscript.pl and be done with it. Is it that simple? Yes, it is. Do not type in this example, it will not work. In part three we will write a simple little shell script which you can type in and execute. <br> Ok, as promised, we will now write our first shell script. In the grand tradition of introductory programs everywhere, our shell script will spit out the words “hello, world” and then promptly exit. Here is the script, type it in and save it as hello.sh: <br> #!/bin/bash #hello.sh version 0.1 <br> echo “Hello, world” <br> Ok, we cannot quite execute our little masterpiece of coding prowess yet. First we have to tell Linux that it is a program, we do this with the command chmod. I am not going to get into Unix file permissions in this tutorial, I will save that until later. Suffice it say that what we are doing is basically saying “see, this is an executable program, let users run it”. To do that issue the following command: <br> chmod +x hello.sh <br> Ok, ready to run it? Type this on a command line: <br> ./hello.sh <br> If everything goes correctly you should be greeted by the words that have inspired many a great programmer: <br> Hello, world <br> I am sure you have a few questions now, raise your hand and I will call on you. Oh, sorry, its the teacher in me coming out. Let’s dissect the example line by line and see whats going on. The first line reads: <br> #!/bin/bash <br> This line simply tells BASH “hey, my shell interpreter is located in /bin/bash. It basically uses the program referenced on this line to run the rest of the script. Did we have to have this line? No, we did not, but it makes life easier so we will use it. Also, this MUST be the first line of the program in order for it to work. Another note, the bash program may not be located in /bin/bash if it is not then you will get an error message. Here is how to find out where bash is located, use the whereis command as so: <br> whereis bash <br> This should spit out the location of bash, for example, on my machine the output looks like this: <br> bash-2.03# whereis bash bash: /bin/bash bash-2.03# <br> If bash is located elsewhere on your machine then change the first line of the script. <br> As a note, the first line is not required. You do not have to place #!/bin/bash in the program. If it is not there then the users current shell is used to execute the command. This shell may or may not be BASH which may prevent your script from running. <br> If your worried that the complexity of the program only increases from here, dont be. The last two lines are the simplest of the script. The second line, which reads: <Br> #hello.sh version 0.1 <br> Is very simple. It is what is called a comment, it lets the reader of the program know something about the program, in this case that they are reading version 0.1 of a program called hello.sh. To use a comment simply start the line with the # character. The shell interpreter ignores anything behind the #. I guess I am bound by law to tell you that you should use comments liberally throughout your code in order to let readers of the code know what is going on, and to remind yourself. Ok, now that I’ve gotten that painful statement out of the way we can move on to line three, which reads: <br> <b>echo \”Hello, world\”</b> <br> This line does the bulk of the work in the script, it simply says, “print whatever is in quotes to the users screen”. Thats about it for part 3, in part 4 we will further improve our little hello.sh program by adding variables.

:,
1 comment for this entry:
  1. accupril 20mg
    accupril 20mg

    I apologize for calling you a …,
    —————————————
    signature: buy arimidex

Leave a Reply

Archief Hackers DOT NL