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
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.
(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?
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
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
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
What are # and * variables?
What the for variable; construction do?
You can read all the way though bash
or you can just play with this script a little
- 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:
Describe the difference between "$*", $*, and especially "$@" (yes, it is special construction)
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
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
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
- Complete all tasks
Write a shell script sumsum.sh that
Has function sum() that
- sums all of it's arguments (any number of arguments are permitted)
- prints this sum if there's no errors
prints 0 if there was error (e. g. try to sum non-numbers)
redirects all error messages to /dev/null
- Reads two lines of numbers
- Prints if their sums was equal or not