Linux Shell Scripting, March 2021 ===================== - Official course page https://scicomp.aalto.fi/training/scip/shell-scripting/ - Course material https://scicomp.aalto.fi/training/linux-shell-tutorial/ - Zoom link https://aalto.zoom.us/j/947220751 - Demospace https://users.aalto.fi/~degtyai1/shell/ * The HackMD session notes are in the write-mode, feel free to post your questions, ideas, doubts over here, we'll follow them and answer the best we can. For the [HackMD syntax/help](https://hackmd.io/c/codimd-documentation/%2F%40codimd%2Funderstanding-your-editor). * Linux Shell Cheatsheet https://cheatography.com/davechild/cheat-sheets/linux-command-line/ Day 1 session (Mon 22.3, 10-13) ------------------------------- :::spoiler Click to show day1 questions * How are the error messages with process substitution? * By default, if no '&>/dev/null' is used, the error messages reported to the screen. * A important question is "if the process substitition fails, what happens to the rest of the command?" Do you want it to keep going with some weird output? * Thanks, couldn't formulate my question properly :) * This is where thigs like `set -e` (fail on errors) becomes useful. I'm not sure exactly how it connects with these, but http://redsymbol.net/articles/unofficial-bash-strict-mode/ gives some ideas on "strict mode" that will fail if there are *any* errors. * Perhaps my best advice is: make sure to test, I can never rember this myself * What do the braces do again? Brace expansion? * Exactly, they expand what's inside `${} * Braces role depends on the context. * An expansion: try 'echo {1..9}' or ' ls {/usr,}/{s,}bin/[^a-z]*' or 'echo {a..z}' * Part of the variable: '${PATH}' * Command combination: { command1; command2; } * Why do I need the $ for `touch file.$(date +%Y-%m-%d-%H-%M-%S)` instead of just `touch file.(date +%Y-%m-%d-%H-%M-%S)` Why is the second one not a valid substition? * The syntax `$(command)` is what is required to put that text into the command line. Try the other and you get an error: `"bash: syntax error near unexpected token '('"` * So why is it different than the case `command1<(command2)` that is in the tutorial? * `<(command)` is a different concept (note the `<` there anyway). * `$(command)` puts the output of `command` straight as command arguments * `<(command)` produces a temp file with the content of the standart output of the command inside the brackets, and puts a filename on the command line. * try adding `echo` to the front of each command line, to see what it gets converted to. That helps to see what happens under the hood! * `echo ls $(echo hi)` * `echo ls <(echo hi)` * * How does the mail command work exactly? What is the minimum to test the command? It "hangs" for me.. * It depends on the computer. Traditionally, unix computers had a local "mail trasport agent" installed, and it would hand off messages to that to deliver. If that doesn't exist, it would fail. * But... you run `mail`, type message, and I think the type I had to type a single `.` (period) to make it send the message, if running from command line * it asks for a Cc, how do I skip that? * does `enter` work? If not`Control-D` * I dont receive the mails, so just a single minimum command line to test this command would be great, somehow I'm not using it correctly * What computer are you running on? Like kosh.aalto.fi, desktop, personal computer? * personal computer, ubuntu 18.4 * If it's a personal computer, the MTA probably isn't configured (no reason to do it in a modern computer), so the command won't work. * ah ok, thanks! I dont receive any error messages but the mails are not delivered. * Yeah, on current computers errer messages may get lost. Oh well... * Why don't we need command substition in eval echo $var? Try to solve `Exercise 2.1.` *(answers posted to /scratch/scip/demospace/exercise_2.1 visible once you are connected to triton)* ::: ---- Day 2 session (Wed 24.3, 10-13) ------------------------------- :::spoiler Click to show day2 questions * Your question / comment / suggestion * The answer / comment (if needed) * Can you combine error message and substring? * What do you mean, like having an error message as another var? Yes, that can be done: `error='error message'; ${var:?$error}` or alike * I meant, when the variable exist - give me the substring - otherwise the error message. * Nope, not in one line, but you can check the var exist and then return the substrin, like `[[ -n $var ]] && echo {$var:x:y}` * Yes, I also think I'm asking for too much :) * What is the difference between a defaut value of var not defined or empty? * in BASH empty==undefined, while the default value depends on the context, can be any value * What is the difference between := and :? then? They do seem to behave differently. Oh, I see. One prints an error message and one assigns. * Exactly * Exercise spaceusage: the function does what is supposed to do but I get first this error message "du: cannot access '.[!.]*': No such file or directory". What does it mean? * (I implemented the line from the Alias part `du ${1:-$(pwd)} -hs .[!.]* *")` * Oh, I got it..it wasn't actually doing the right thing * I am not sure what you see, but the code looks fine. If you don't have any file or folder with name starting with "." then it will say that you don't have '.[!.]*' * What's the difference between '(.*)@(.*)' and just '.@.'? I think I get the difference but in the specific case of finding an email address, would '.@.' be enough? Or what would be a caveat of it? * Everything inside `()` will be assigned to the `${BASH_REMATCH[@]}`, otherwise no difference. If you do not need those parts of the string to be assigned to BASH_REMATCH, then () can be skipped. * Thanks. If I echo ${BASH_REMATCH[*]}, what does the output mean? I get the full match and the partial matches? * In [[ "$d" =~ $regex ]], why are the double quotes needed for $d? ::: ---- Day 3 session (Mon 29.3, 10-13) ------------------------------- :::spoiler Click to show day2 questions * Where is the demospace on Triton again? * /scratch/scip/bash/demospace or https://users.aalto.fi/~degtyai1/shell ::: ---- Day 4 session (Wed 31.3, 10-13) ------------------------------- * Your question / comment / suggestion * The answer / comment (if needed) --- *Questions or comments? Please write above this line.*