03. Shell Programming (part 2)

Make 03_ShellProgramming subdirectory and store all the code there.

(!) As always, part of this tasks is about reading [[man1:man|manual pages

  1. while.sh

       1  #!/bin/sh
       2 
       3  A=.
       4  while echo "Run"; ls $A 2> /dev/null; do
       5          read A
       6  done
       7  echo "Exit status: $?"
    
    • Like if, while executes a command chain (echo "Run"; ls $A in example) and check exit status of last command executed (ls)

      • if it's 0 (successful execution) this means true
      • if it is non-zero, this means false (when ls got an error

    • exit status on any last command executed is stored to «?» variable

    • (optionally) you can use && and || instead of ; to force all exit statuses into account, e. g. cmd1 || cmd2 is true if either of cmd1 or cmd2 was successful, and cmd && cmd2 && cmd3 is true only if all commands was successful.

  2. (!) (was in homework, but can get it from help read). Write a program while2.sh that simulates cat: reads from standard input (one read per line):

       1 $ fortune > file
       2 $ cat < file
       3 Isn't it ironic that many men spend a great part of their lives
       4 avoiding marriage while single-mindedly pursuing those things that
       5 would make them better prospects?
       6 $ sh while2.sh < file
       7 Isn't it ironic that many men spend a great part of their lives
       8 avoiding marriage while single-mindedly pursuing those things that
       9 would make them better prospects?
      10 
    
    • (!) Also: what we ought to do to make ./whil2.sh run?

  3. test. Can compare strings, numbers, check is string is a real file name etc.

    • (!) Research: read the manual page. What these commands do:

    • try test $A = $B ; echo $? for various A and B

      • (!) why we got an error if either A or B is empty?

    • $A -lt $B

    • -f $A

    • -z "$A"

  4. (!) Write a while3.sh script that print all the lines from standard input, but stops if QUIT is entered

    • <!> (optional) it must also stop when standard input is closed

  5. for.sh

       1 #!/bin/sh
       2 
       3 for n in 1 2 3 10 20 30 qwe asd xcv; do
       4         echo "Next value $n"
       5 done
       6 
       7 echo "$#" / "$*"
       8 for n ; do
       9         echo "What is this: $n?"
      10 done
    
    • The for cycle is like python's (strictly speaking, python's for is like shell's one :) ). It assigns to a variable (n in example) all words from word sequence

    • (!) Research:

      • What are # and * variables?

      • What the for variable; construction do?

        1. You can read all the way though bash

        2. or you can just play with this script a little :)

  6. Functions:
    • Function in shell is like a (temporally defined) command
    • Arguments are passing to the function just like command line arguments does
    • inside a function all the command-line aware constructions (like $0, $1, $#, $* etc) deals with function arguments instead

    • See funct.sh file:

       1 #!/bin/sh
       2 
       3 fun() {
       4   echo "$0: $# $*"
       5   for arg; do
       6         echo "> $arg"
       7   done
       8 }
       9 
      10 fun 1   QWE  "3 4"
      11 fun "$*"
      12 fun  $*
      13 fun "$@"
    
    • (!) Research:

      • Describe the difference between "$*", $*, and especially "$@" (yes, it is special construction)

  7. Reminder. Command output substitution, `command` and $(command) forms (equivalent)

    •    1 $ date
         2 Чт апр 23 20:13:35 MSK 2020
         3 $ A=`date`
         4 $ echo $A
         5 Чт апр 23 20:13:44 MSK 2020
         6 $ B=$(cal)
         7 $ echo $B
         8 Апрель 2020 Пн Вт Ср Чт Пт Сб Вс 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
         9 $ echo "$B"
        10      Апрель 2020
        11 Пн Вт Ср Чт Пт Сб Вс
        12        1  2  3  4  5
        13  6  7  8  9 10 11 12
        14 13 14 15 16 17 18 19
        15 20 21 22 23 24 25 26
        16 27 28 29 30
        17 
      
      • Why echo $B looks like this?

    • (!) Create funct2.sh program

       1 #!/bin/sh
       2 
       3 sum() {
       4   # insert your code here
       5 }
       6 
       7 while read a b; do
       8         sum $a $b
       9 done
    
    • Note how read var1 var2 … varN works

    • (!) read expr and improve the program so it will print the sum of $a and $b. Assume input is always correct

  8. (!) Copy funct2.sh to funct3.sh and modify it so it shall also stop if $A+$B>100.

    • Hint: use substitution here.
    • You can use break to quit from cycle

    • Hint: debug shell script with sh -x shellscript.sh

H/W

HSE/ArchitectureOS/Lab_03_ShellProgramming (последним исправлял пользователь FrBrGeorge 2020-06-22 12:38:21)