Answer each of the following questions completely and briefl

Answer each of the following questions completely and briefly:

1. Using the layered approach in systems programming design, explain the relationship among the following concepts: user, shell, commands, shell scripts, application programs, C standard library functions, system calls, operating system kernel, interrupt, kernel mode and user mode. You can explain them by using both words and diagrams.


2. Suppose there is a file named filename in your current directory, write a C program to print out the user’s (owner’s) read, write, execut permissions for this file, and the type of this file.

Solution

Shell Programming

We are now familiar with the shell and a large number of commands.

In this lecture, we will discuss shell programming using bash. The main goal is to be able to write your own scripts. But what are scripts? And what are they useful for? We will answer these questions.

Goals

We plan to learn the following from today’s lecture:

OK. Let’s get started.

Our top 20 commands - ok top 44 then

Linux has the ability to let you put commands together in a single file known as a shell script that it can execute. Shell scripts can include any of the shell commands; for example, the set we discussed in class. These top twenty commands (well, I lied, top 44 commands - I used wc word count with the “-w” switch to get the actual number of commands we have discussed in class) are sorted (I used the sort command) in alphabetical order below:


alias, awk

bg

cat, cd, chmod, chsh, cp, cut

echo, expr

fg, file, find

grep

history

jobs

kill

less, locate, logout, lpq, lpr, lprm, ls

man, mkdir, more, mv

ps, pwd

rm, rmdir

scp, sleep, sort, ssh

tar, touch, tty

uniq

whereis, which, who

Important note: If you know our top 44 you are all set for the reminder of the course. You may only use half of them most of the time and you are likely to learn others, but this is a good set to know.

We also used a number of programs and tools:

as, emacs, vim, gcc, objdump

We will learn more tools for sure but emacs and gcc will become familiar friends.

In addition to calling Linux commands (e.g., grep, cd, rm) shell scripts can also call compiled programs (e.g., C programs) and other shell scripts. Shell programming also includes control flow commands to test conditional code (if..then) or to do a task repeatedly (for..in). These control structure commands found in many other languages (such as C, or other scripting languages like perl) allow the programmer to quickly write fairly sophisticated shell programs to do a number of different tasks.

Importantly, shell scripts are not compiled, rather, they are interpreted and executed by the shell itself.

Shell scripts are used for a variety of reasons ranging from building and configuring systems or environments, prototyping code, or in support of an array of repetitive tasks that programmers do. Shell programming is mainly built on the Linux shell commands and utilities and therefore reuse of existing programs enables programmers to simply build new programs to tackle fairly complex jobs.

Interactive mode and shell scripts

The shell can be used in two different ways:

The interactive mode is fine for entering a handful of commands but it becomes cumbersome for the user to keep re-entering these commands interactively. It is better to store the commands in a text file called a shell script, or script for short, and execute the script when needed. In this way, the script is preserved and other users can also use it - code reuse, again. In fact, scripts can invoke others scripts and programs so scripting makes good sense.

Separating groups of commands using ;

Let’s start to build up our knowledge of how scripts work by first looking at some basic operations of the shell. The Linux shell allows for the unconditional execution of commands and allows for related commands to be kept adjacent as a command sequence using the semicolon character as shown below:

[campbell@moose ~]$ date; who; tty
Wed Jan  2 12:13:51 EST 2008
jbrody   :0           Dec 20 11:34
jbrody   pts/0        Dec 21 21:34 (:0.0)
jbrody   pts/1        Dec 21 18:32 (:0.0)
campbell pts/2        Jan  2 11:51 (c-75-69-130-98.hsd1.nh.comcast.net)
/dev/pts/2

The tty command prints the file name of the terminal connected to standard input.

Exit Status [$?] - Why is it important?

When using the shell interactively it is often clear when we have made a mistake - the shell warns about correct syntax, and complains about invalid switches or missing files. Here is a seperation of interests between the parser (the shell) and the program (the command, /bin/ls for example).

Error messages provide visual clues that something is wrong allowing us to adjust the command to get it right.

Commands also inform the shell explicitly whether the command has terminated successfully or not due to an error. Commands do this by returning an exit status, which represents an integer value that is made available to the shell and other commands, programs, scripts.

By convention an exit status of (0) indicates the successful execution and any other value (always positive) indicates failure of some sort.

The shell environment value $? is updated each time a command exits. What do we mean by that?

[campbell@moose ~]$ echo $?
0
[campbell@moose ~]$ ls badfilename
ls: badfilename: No such file or directory

[campbell@moose ~]$ echo $?
1

The special parameter $? holds the exit status. There are a number of other special parameters, most like $? can be only read from and not written to.

Conditional sequences - basic constructs

Why do we need to use the exit status?

Often we only want to execute a command based on the success or failure of an earlier command; for example, we may only wish to remove files if we are in the correct directory, or we can only append info to a file if we know it exists.

The shell provides both conjunction (“and-ing”) and disjunction (“or-ing”) based on previous commands. These are very useful constructs for writing decision-making scripts - take the example below:

[campbell@moose cs50]$ cd tempdir && rm *.c

[campbell@moose cs50]$ (who | grep -q campbell) || echo no the lazy boy is playing squash
no the lazy boy is playing squash

In the first example, && (without any spaces) requests that the second command is only executed if the first succeeds (with an exit status of 0) - i.e., we only delete the files if we have been able to change to the required directory. The w or who command shows who is logged on and what they are doing.

In the second example, (||) (without any spaces) requests that the second command is only executed if the first command failed (with an exit status > 0) - i.e., if campbell is not logged on. The grep command used with the “-q” switch for quiet suppresses the error message when campbell does not exist in this example.

The shell syntax borrows heavily from C (and now C++) . Since Unix is written in C it is not surprising that the shell syntax is similar

Answer each of the following questions completely and briefly: 1. Using the layered approach in systems programming design, explain the relationship among the f
Answer each of the following questions completely and briefly: 1. Using the layered approach in systems programming design, explain the relationship among the f
Answer each of the following questions completely and briefly: 1. Using the layered approach in systems programming design, explain the relationship among the f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site