Homokozó:Programozás/1

A Wikiforrásból
Ugrás a navigációhoz Ugrás a kereséshez
Programozás
szerző: Szervác Attila
1. lecke
2. lecke →
Ch1: Your first program, commands, standard output, loops, input and arithmetics


.1 Your first program, commands, standard output, loops, input and arithmetics


The standard way to invoke our interactive shell implementation is shown in the example below. Let’s type it after the prompt string (usually '*$ ') then let’s execute it by press the [Enter]:

sh -c 'echo "Hello, World!"'

All expressions (‘words’) in the standard shell language are separated by the actual Internal Field Separators (IFS) defined by the IFS environment variable and other special characters, first of all enclosed within the double " and literal single ' quote characters. With the "sh" command in the standard command line interface (cli) / environment we are invoking our interactive shell implementation. With the -c option we tell our invoked shell not to read the list of our commands from a given filename – as sh myprogramfile – instead this way immediately from the command line: as 'our first command; our second command…' argument.

We are using only one command on our first program, which is the echo command.

Syntax:

echo [-n] args…

The echo prints the arguments on the standard output (stdout), separated by spaces. Unless the -n option is present, the echo command also prints a newline character (\n) in the end.

The ; and the newline character are control operators between the commands. We can use smarter printf command to print more special outputs:

sh -c 'mytext="Hello, World!! :)"; printf "\t$mytext\n" # our first program :)'

In the first command we defined a sympathetic variable named 'mytext', so its parameter will be very reusable and portable; then, in the second step we used the clever printf command where with the special escape character ('rep', \) we first drop a tab, then printed the value of our variable (: $mytext), finally a newline. Other special characters especially outside of literal ' quoting has also special meanings.

For example the * character in the command string outside of quoting can match to file entries depending on the executed command. ? means 1 character. To match to a character, the standard shell can use so-called character classes, range of ASCII characters, for example [a-z], thus [a-z]* can match to one or more file entries starting with a minuscula, try similar than sh -c 'echo *' or sh -c 'echo .*' commands to list your file entries on the current working directory.

The concept behind for loop is the following: when a variable gets values we can do the execution of the list of commands, here is a trick with strings:

sh -c 'for l in a b c; do printf "${l}d\n"; done;'

Encapsulating the variable name in to {} also can make other funny tricks called parameter expansion.

Let’s list our files in the root (/) directory:

sh -c 'for fn in /*; do printf "$fn "; done; printf "\n";'

The read command can read words and rest of line into variables from the stdin (keyboard) or by redirection from file:

sh -c 'read -p "Type & press Enter: " mystring; printf "$mystring\n";'

sh -c 'read firstline</etc/passwd; printf "$firstline\n";'

The while loop acts while the result is true (0, ':')

sh -c 'while read line; do printf "$line\n"; done</etc/passwd;'

The other most popular programming tool is the arithmetic expansion, let’ see:

sh -c 'for i in 1 2 3 4 5 6 7 8 9 10 11; do printf "$((i*i))\n"; done;'

Finally let’s see the test expression command or for short [ expression ], for example:

sh -c 'a=1; test $a -le 10; printf "$?\n"'

that gives true if the expression is true.

So we can use it effectively in short form in a while [ expression ]; do ... ; done; loop. Of course you can edit your program in a file, e.g. 'myprogramfile' with a simple text editor, then run with the appropriate script language interpreter. For this: simply use the

sh myprogramfile

form, so this simple way you can always save and run your program reading Your file from now.

Test #1

1: Write and construct a while loop that prints a funny text 9876 times to the stdout. 2: Write an infinite while loop that prints something to the stdout. Don’t forget with ^C you can send a SIGINT signal to Your Infinite Process[tm] :))) Use the 1-character long true expression for the while if you can! :) 3: Learn the trap and break commands to find how you can trap the ^C SIGINT – or ^\ SIGTSTP – signal to skip after your given infinite loop.

URL-s:

The Dash manpage