Document Details

StylishSpessartine

Uploaded by StylishSpessartine

University of Science and Technology

2024

Tags

shell programming operating systems computer science

Full Transcript

**University of Science and Technology** **Faculty of Computer Science and Information Technology** **Open Source Operating Systems** **Lab (6) Date 03-05-2024** **[The Shell]** **Description:** The students take from this exercise what is the shell program ,the main Shell features such as com...

**University of Science and Technology** **Faculty of Computer Science and Information Technology** **Open Source Operating Systems** **Lab (6) Date 03-05-2024** **[The Shell]** **Description:** The students take from this exercise what is the shell program ,the main Shell features such as combining commands, command line completion, command line edition, command history, Command Substitution, using Pipes, Aliases, Running commands in Background, input and output redirection and Shell functions. **Lab Learning Outcomes:** Applying The common shell features. **Lab Instructions:** The lab instructor has to follow the steps below to demonstrate to the students the main shell features. **\*The Shell:** The shell is a program which reads and executes commands for the user. It is an interface between the user and the kernel (operating system). **\-\--What is the shell does:** \- The shell prompts for input. \- The shell processes your command line to determine actions to takes and carries out the actions required. \- After the process is finished the shell prompts again for input. **\* The shell Features:** **1-** **Combining Commands on One Line:** You can write multiple commands on one line by separating them with ; **example**: show how you can use the shell features to run run the commands date, ls, pwd. and cat file1 in one command line. \$ date; ls ; pwd ; cat file1 **2- command line Completion:** The shell can making typing commands easier. Once an unambiguous prefix has been typed: 1. Write one ore more command letters 2. pressing Tab 3. the shell will automatically 'type' the rest. Example: \$ da tab key may be completed to date if no other commands start 'da' **3- Command line Edition:** Commands can also be edited before being run. Particularly useful for fixing a typo in the previous command. 1. Use the Left and Right cursor keys navigate across a command. 2. Extra characters can be typed at any point. Backspace deletes characters to the left of the cursor.Del and Ctrl+D delete characters to the right. **These are the basic editing commands by default:** Right --- move cursor to the right Left --- move cursor to the left Up --- previous history line Down --- next history line Ctrl+A --- move to start of line **4- Command History:** Often it is desired to repeat a previously-executed command. The shell keeps a **command history** for this purpose. 1. Use the Up and Down cursor keys to scroll through the list of previous commands 2. Press Enter to execute the displayed command Bash stores a **history** of old commands in memory. Use the built-in command history to display the lines remembered. History is stored between sessions in the file ˜/.bash\_history. When you start next session the shell automatically load ˜/.bash\_history file into the history list buffer. **History commands:** - To see the stored list use the command \$histoy 1. Run the command \$ history 2. The line into the history list are called events , and the numbers a long side each line are called event numbers. 3. To repeat execusion of specific event use the history substitution operator followed by the event number. For example: \$ ! 515 \$ ! ! \$ ! ls **5- Running commands in Background:** When a shell run a command it waits untill the command is completed. To run another commands before the first one is completed shells allows you to run the first command in the background by using the symbol (&). **Example**: Let the command find / -type \*.doc -print running in background. \$ find / -type \*.doc -print & The result is: \[1\] 1413 Job id pid **6- Command Substitution:** Command substitution allows the output of one command to be used as arguments to another. **Example1:** use the locate command to find all files called *manual.html* and print information about them with ls: \$ ls -l \$(locate manual.html) \$ ls -l \`locate manual.html\` The back quotes on the second form are opening single quote characters, called brackticks. The \$( ) form is usually preferred, but brackticks are widely used. **Example2:** Send mail for all user's mails that stored in the file names \$ mail \`cat names\` **7- Using Pipes:** \* A pipe channels the output of one program to the input of another. \* Allows programs to be chained together. Programs in the chain run concurrently. 1- Use the vertical bar: \| Sometimes known as the 'pipe' character. 2- pipe the output of the first command into the second one. **Example:** 1. pipe the output of echo into the program rev (which reverses each line of its input): \$ echo Happy Birthday! \| rev The result is: !yadhtriB yppaH 2. \$ cat file1 \| lp Print file1 3. \$ who \| wc --l Display the number of logged in users. 4. \$ cat \*.txt \| wc Display the number of words in all.txt files. **8- Redirecting input and output:** The output from programs is usually written to the screen, while their input usually comes from the keyboard (if no file arguments are given). In technical terms, we say that processes usually write to **standard output **(the screen(fd1)) and take their input from **standard input **(the keyboard(fd0)). There is in fact another output channel called **standard error (**fd2**)**, where processes write their error messages; by default error messages are also sent to the screen. **\--To redirect standard output to a file instead of the screen:** **1- Use the \> operator:**     \$ echo hello  \     hello \     \$ echo hello \> output  \     \$ cat output  \     hello 2- In this case, the contents of the file output will be destroyed if the file already exists. 3- If instead we want to append the output of the echo command to the file, we can use the \>\> operator:     \$ echo bye \>\> output  \     \$ cat output  \     hello \     bye **-4-To capture standard error, prefix the \> operator with a 2 e.g.:**     \$ cat nonexistent 2\>errors  \     \$ cat errors  \     cat: nonexistent: No such file or directory \     \$ 5. You can redirect standard error and standard output to two different files:     \$ find. -print 1\>errors 2\>files or to the same file:     \$ find. -print 1\>output 2\>output  \ or \     \$ find. -print \>& output **\-- Standard input can also be redirected** **1- Use the \ output will destroy the contents of the file output. This is because the first thing the shell does when it sees the \> operator is to create an empty file ready for the output. **9- Adding Aliases:** Converts complex commands into simpler ones. Setting aliases can save you even more typing than setting environment variables. *[With aliases, you can have a string of characters execute an entire command line]*. 1. You can add and list aliases with the **alias** command: **\$ alias *name* = *command***. **Examples**: 1.  **\$ alias p='pwd ; ls --CF'** \$ p **Out put:** run the command pwd and then to run ls --CF to print the current working directory and list its contents in column form. 2. **\$ alias rm='rm --i'** \$ rm **Output:** The second runs the rm command with the --i option each time you simply type **rm**. 2. While you are in the shell, you can check which aliases are set by typing the **alias** command. \$ alias 3. If you want to remove an alias, type **unalias**. (Remember that if the alias is set in a configuration file, it will be set again when you open another shell.) **\$ unalias p** **\$ unalias rm** **10- Shell functions:** - **What are functions?** Shell functions are a way to group commands for later execution, using a single name for this group, or routine. The name of the routine must be unique within the shell or script. All the commands that make up a function are executed like regular commands. When calling on a function as a simple command name, the list of commands associated with that function name is executed. A function is executed within the shell in which it has been declared: no new process is created to interpret the commands. **Function syntax:** Functions either use the syntax: **function FUNCTION { COMMANDS; }** Or: **FUNCTION () { COMMANDS; }** Both define a shell function **FUNCTION.** The commands listed between curly braces make up the body of the function. These commands are executed. whenever **FUNCTION** is specified as the name of a command. The exit status is the exit status of the last command executed in the body. **Common mistakes:** \* The curly braces must be separated from the body by spaces, otherwise they are interpreted in the wrong way. \* The body of a function should end in a semicolon or a newline. **Examples**: 1. \$u() { ls -l; } Here you can use u to execute ls --l. \$u 2. \$u() { ls --l \$\*; } The addition of \$\* indicates that any parameter or command line switches entered after the u command are to be insertedinto the ls --l command in place of the \$\* character. using the new difinition you can type: \$u --a - **Exercises (To be solved in Classroom):** - **Task** Open the file.bash\_profile with vi editor : \$ vi.bash\_profile To check your added aliases and functions End

Use Quizgecko on...
Browser
Browser