00. Shell programming

Create 00_OverviewShell directory. All code must reside here.

Script: executable text file

Tasks

  1. Simple script file named c1:

       1 #!/bin/sh
       2 read A
       3 echo $A
    
  2. A is variable
  3. $A is a substitution of A value

  4. echo command is executed after substitution, so

    • if called as
      • sh c1   ABC      def  123

      • it outputs
        • ABC def 123

        without extra spaces, because echo $A expands to echo ABC      def  123, which is command echo with three command line parameters

  5. (!) How to make echo display all spaces as well?

    • Spoiler:

    • fix c1 to do this

  6. Output substitution, c2 file

       1 #!/bin/sh
       2 read A
       3 Sys=`uname`
       4 echo "Hello, $A!
       5 Welcome to $Sys!"
    
    • The «`command`» (also «$(command)») construction runs command and substitute it with its' output

    • No spaces is allowed around «=» when assigning

    • All variables are strings
    • (!) Modify c2 not to read A, but to output user login name instead (use logname or whoami command)

  7. Multiline
    • echo $A squeezes all newlines as well

    • If you want to use control characters like in C, use echo -e key

    • (!) Write a c4 script that

      • ends with command echo -e "$Msg"

      • outputs
        Hello, your_login_name!
        Welcome to Linux
      • and contains only one two extra lines:
        • one shebang
        • and one like Msg=…something…

      • make this script executable
  8. Commandline parameters. This is c5 file:

       1 #!/bin/sh
       2 
       3 echo "$0"
       4 echo "$1"
       5 # ...
       6 echo "$*"
       7 echo "$#"
    
    • $1, $2 … — command line argument №1, №2 …

    • $0 — name of the script itself

    • $*all command line arguments

    • $# — number of command line arguments so

         1 [tmpuser@sugon 00_OverviewShell]$ sh c5 A bc  cde   fff
         2 c5
         3 A
         4 A bc cde fff
         5 4
         6 
      
  9. Conditional if operator.

    • Overview:
         1 if condition-conmands
         2 then
         3   true-commands
         4 else
         5   false-commands
         6 fi
      
      • if last of condition-conmands exit with success status (zero status code), true-commands are executed.

      • otherwise, if last of condition-conmands exit with non-zero exit status (an error), false-commands are executed.

      • Yes, 0 status is true, non-0 is false!

      • Yes, fi is if backwards

    • Example c6 file:

   1 #!/bin/sh
   2 if ls $*; then
   3         echo YES
   4 else
   5         echo NO $?
   6 fi

H/W

Research

What exit status the command read generates? Try it

  • Important: Please consult help read (e. g. here) command instead of man read, because read is shell built-in and you got manual page on all shell builtins

Research

How to suppress «\n» output after echo (see echo)?

Task

Write a c8 script that asks user for name, and prints a welcome message either if an user has entered a name or not:

   1 $ ./c8
   2 Enter your name: Spot
   3 Hello, Spot!
   4 $ ./c8
   5 Enter your name: 
   6 Hello, tmpuser?
   7 
  • Hint: In second example user had not pressed «Enter»

HSE/ArchitectureOS/Lab_00_OverviewShell (последним исправлял пользователь FrBrGeorge 2020-04-08 20:54:16)