1 / 72

Some simple examples

Some simple examples. A B C Hi First, second, third(with errors) third2 EDIT fun3 func6 looping (w13). Floating Number in Unix. bc – or precisely, bc programming language . bc is " an arbitrary precision calculator language " with syntax similar to the C programming language.

barriosc
Télécharger la présentation

Some simple examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Some simple examples • A B C Hi • First, second, third(with errors) third2 • EDIT fun3 func6 looping • (w13)

  2. Floating Number in Unix • bc – or precisely, bc programming language. • bc is "an arbitrary precision calculator language" with syntax similar to the C programming language. • It is generally used by typing the command bc on a Unix command prompt and entering a mathematical expression, such as (1 + 3) * 2, whereupon 8 will be outputted.

  3. Example • $ bc • 2.34567 * 3.56789 • Calculating PI to 1000 places • $ bc –l –q • Scale = 1000; • A(1)*4

  4. Example • $ bc –l –q • scale = 12; • s(3.333) --- Calculate sin(x) to 12 digits • c(4.567)

  5. More about echo Command • Use echo command to display text or value of variable. • echo [options] [string, variables...]Displays text or variables value on screen.Options-n Do not output the trailing new line.-e Enable interpretation of the following backslash escaped characters in the strings:\a alert (bell)\b backspace\c suppress trailing new line\n new line\r carriage return\t horizontal tab\\ backslash

  6. Example • $ echo -e "An apple a day keeps away \a\t\tdoctor\n"

  7. More about echo • Display colorful messages • echo “Hello, world!” • echo -e "\033[34m   Hello Colorful  World." • (hello_color week12)

  8. That echo statement uses ANSI escape sequence (\033[34m), above entire string ( i.e.  "\033[34m   Hello Colorful  World." ) is process as follows • 1) First \033, is escape character, which causes to take some action. • 2) Here it set screen foreground color to Blue using [34m escape code. • 3) Then it prints our normal message Hello Colorful  World! in blue color.

  9. If we want to display that echo statement in red • echo -e "\033[31m   Hello Colorful  World.“ • In general: • Syntaxecho   -e  "\033[escape-codeyour-message" • More detail, see file cs315\Echo_sequence

  10. Shell Arithmetic • Use to perform arithmetic operations. • Syntax:expr op1 math-operator op2Examples: $ expr 1 + 3$ expr 2 - 1$ expr 10 / 2$ expr 20 % 3$ expr 10 \* 3$ echo `expr 6 + 3`

  11. Double quote, single quote, and back quote • echo “Today is `date`” • Double quote remove the meaning of the characters except “$” and “\”. • echo ‘Today is `date`’ • Enclosed in single quotes remains unchanged • echo `date` To execute command

  12. The read Statement • Use to get input (data from user) from keyboard and store (data) to variable.Syntax: read variable1, variable2,...variableN

  13. env command • Displays your UNIX environment variables. • env ENTER • Notice the difference between “env” and “set”. • “set” will display all variables while env only displays the environmental variables.

  14. Variable assignment • Variables are assigned in a script program as follows: • DONE=no • They are used in this manner: • while [ $DONE = no ] in your script

  15. #! /bin/bash • DONE=no • ENTRIES="hello bye ls 1" • while [ $DONE = no ] • do • echo Valid entries are: $ENTRIES • read ENTRY # Read the variable ENTRY from the user • case $ENTRY in • 1) • pwd • ;; • hello) • echo How are you? • ;; • bye) • echo exiting... • DONE=yes • ;; • ls) • ls -al |more • ;; • *) • echo $ENTRY is an unrecognized command. • ;; • esac • done (looping week11)

  16. /bin and Running Shell Script • To run a script, we can: • 1. ./script_Name • 2 . script_Name • 3. sh script_Name • In fact, we can run the script with some other ways.

  17. The difference between . Script_Name and ./script_Name • You can also run a script by using: • bash script_Name enter • . Script_Name

  18. Create a bin directory mkdir bin under your home directory. From your current directory, Type: cp script_name ../bin to copy the script into /bin directory. Or, cp * ../bin to copy all your files to the bin directory. Now, you can run them directly by type in the script_Name.

  19. Example • Run looping script under current directory • cp looping ../bin enter • Type looping enter • This time, you can type in the script name and run it directly.

  20. chsh –Change Shell • chsh enter • Type in your password enter • Type /bin/ksh will change your login shell to Korn shell

  21. What Is Korn Shell? • The Korn shell (/bin/ksh) is the most advanced 'standard' UNIX shell. It extends the Bourne shell with lots of nice features, and is a lot faster.

  22. Korn Shell • It was written by David Korn. • It is a powerful superset of the Bourne shell. • The improvements include : • 1. Job control • 2. Command line editing • 3. Programming features.

  23. Korn Shell Korn is superset of Bourne. zsh claims to be an enhanced Korn shell, while bash has added some parts of it

  24. Aliases in Korn shell • The corn shell allows you to create your own commands by using the alias command. • Example: • $ alias d= `ls –l` in standard Unix • Example in In our Linux • alias l=ls [Enter] • alias AA=“cal 2004”

  25. Exception in using alias in Korn • All built-in commands may be aliased except for: • case, do, done, elif, else, esac, fi, for, function, if, select, then, time, until, while, {,}

  26. Remove an alias-unalias • The unalias will remove all of the specified aliases. format: unalias aliasName [Enter]

  27. Functions in Korn shell • Korn shell allows one to define functions that may be invoked as shell commands. • Two basic format to define a function in Korn shell: • 1. Function name • { • list • }

  28. Function --- cont. • 2. Name ( ) • { • list • } • This format looks similar to C language format.

  29. Example • f ( ) • { • echo parameter l = $1 • echo parameter list = $* • } • # main program • f l • f cat dog goat • (week13 func6)

  30. Enhanced job control • Command : • jobs --- Display current jobs • bg %job# --- resumes the specified job as • a background process. • fg %job# --- resumes the specified job as • the foreground process. • kill

  31. TILDE “~” substitution • ~ --- $HOME • ~user --- home directory of user • Example echo ~ ENTER • ~/pathname --- $HOME pathname • ~+ --- $PWD (current working directory) • ~- --- $OLDPWD (previous working directory)

  32. More • let x=y+z written as ( ( x = y + z )) • select - allows use of simple menus • functions can be written with parameters • f( ) • { • echo $1 $2 $3 • } called by f cat dog cow

  33. While … Do… Done • #!/bin/ksh • message ( ) • { • echo hi • echo there • } • i=1 • while (( i <= 3)) • do • message #call the function • let i=i+1 • done (while week13)

  34. Function in General Form • function name • { • list of command • } # the keyword function may be omitted

  35. Function in General Form • name ( ) • { • list of command • }

  36. Using typeset to declare a local variable • Example: fact2 (week13)

  37. Typeset in Korn • The typeset command can also be used to assign values, but unless you are setting attributes, it's a lot more work for nothing. If a value is not given, the variable is set to null. Here, X is reassigned the null value: • $ X=

  38. Korn Shell • Variables and parameters are used by the Korn shell to store values. • Like other high-level programming languages, the Korn shell supports data types and arrays. • This is a major difference with the Bourne, C shell, and other scripting languages, which have no concept of data types.

  39. Korn Shell • The Korn shell supports four data types: string, integer, float, and array. • If a data type is not explicitly defined, the Korn shell will assume that the variable is a string.

  40. Integer (–i) Attribute • The integer attribute (–i) is used to explicitly declare integer variables. • Although it is not necessary to set this attribute when assigning integer values, there are some benefits to it. We'll cover this later in Chapter 6. In the mea • NUM is set to an integer-type variable and assigned a value: • $ typeset —i NUM=1 • $ print $NUM1

  41. Float (–E, –F) Attribute • The float attributes (–E, –F) are used to declare float variables. The –E is used to specify the number of significant digits, while –F is used to specify the precision. We'll cover this later in Chapter 6. In the following example, X is set to a float variable and assigned a value using both formats:

  42. Float Attribute • $ typeset —E5 X=123.456 • $ print $X123.46 • $ typeset —F5 X=123.456 • $ print $X123.45600

  43. Assigning Values to Variables • variable=declare variable and set it to null • typesetvariable=declare variable and set it to null • variable=valueassign value to variable • typesetvariable=valueassign value to variable

  44. factorial ( ) # one-parameter function • { • if (( $1 <= 1 )) • then • echo 1 • else • typeset tmp • typeset result • (( tmp = $1 - 1 )) • (( result = `factorial $tmp` * $1)) • echo $result • fi • } • x=number • echo input a number: • read $x • # • factorial $x • echo '$? is' $? • echo factorial 15 = `factorial 15` • echo '$? is ' $?

  45. Kermel • 1. The kernel itself is not a process but a • process manager. • 2. System calls are some specific program constructs that perform the kernel service required by processes. • 3. Each system call sets up the group of parameters that identifies the process request and then executes the hardware-dependent CPU instructions to switch from User Mode to Kernel Mode.

  46. Kernel thread---Privileged processes • Besides user processes, Unix systems include a few privileged processes • ---Kernel thread. • 1. They run Kernel Mode in the kernel address space. • 2. They do not interact with users, and thus do not require terminal devices. • 3. They usually created during startup and remain alive until the system is shut down.

  47. Transition between User Mode and Kernel Mode • . Process1 Process1 Process2 Process2 User Mode Kernel Mode Interrupt handler System call handler Scheduler System call Timer interrupt Device interrupt

  48. Process Implementation • To let the kernel manage processes, each process is represented by a process descriptor that includes information about the current state of the process. • When kernel stops the execution of a process, it saves the current contents of several processor registers in the process descriptor. These include: PC, SP, General-purpose registers, floating point registers, processor control registers, • and the memory management registers.

More Related