Ceci est une ancienne révision du document !
This month I’d like to talk about something basic that a lot of people forget about. I was standing behind someone, shoulder surfing, as he showed me something. He was one of those super fast typers (100+ wpm) that could retype any mistake very quickly, but some of it was not mistakes and he would hit CTRL+c and retype. All I could think about was that it could have been done in one go or continued without issue. You see I’m lazy, I don’t like a lot of typing, (also I only type at 45wpm) so any “savings” earns bonus points in my book.
You all know about chaining update and upgrade, I mean you probably made an alias or tiny program to do it for you. You know, “sudo apt update && sudo apt upgrade -y”. Thing is, that I want to highlight that “&&” and expand a bit. The “&&” will run the second command, regardless if the first command had an error. To illustrate, disconnect your network, or turn off your WiFi and run that command. Let’s move on. The examples I will use will not be “real world” examples, but they will be examples that you can easily follow. Let's start simple, with the background operator. Open your terminal and type:
ping -c 3 www.google.co.uk &
and hit <enter>. What you will get back, is a process ID. (ping prints to the screen, so it won’t go quietly, but that is why I chose it) So if we were to add more than one command, they would happen at the same time, meaning instead of my typing out, say pwd, id and who, I could chain them, like so: pwd & id & who -and I should get three PID’s back and a “done” message when completed. (that ‘done’ part is important) However, sometimes we need the first command to complete before the second one runs, and we are all familiar with the first example above, but I’ll use another to illustrate my point. If I were to make a folder and then cd into that folder and make a file, you would agree, that I would get an error, if the folder was not created yet and I tried to cd into that folder, correct?
Good, you are all smart cookies, so I’m sure we are all on the same page. If I were to type:
mkdir fcm; cd fcm; touch fcm
I’d end up in a folder with a file named fcm. It would be as if I typed those commands in, in order, one-by-one. It would do the first command, once completed, it would do the second command and so forth.
Now I want you to make a mistake. Type:
mkdir fcm; cd fcn; touch fcm
and see what happens. Do you understand the difference between using “&&” and “;” then? (If not, replace the “;” with “&&” and run it again.)
So we know about the “and” operator “&&” , now for the “or” operator “||”, for instance, we could look for a file, if it exists, do nothing, if it does not, create it. Example: ls fcm23 || touch fcm23 or the opposite, if it exists, remove it, ls fcm23 || rm fcm23 -if you are a newbie, please do this in your terminal to remember it. You know, the muscle memory thing…
You can chain more of these commands together, like we saw in the Python tutorials in the magazine, lets say: ls fcm && echo “present” || “missing” & -and we bring it all together at once. Though it may look cryptic, you know what it does and if you followed along in the terminal and I now encourage you to try your own command chaining with commands that *you know. (everything I’m thinking of requires something installed that you may not have) If you are having trouble visualising any of it, use parenthesis. For example; (ls fcm23 && echo “searching…”) || (echo “not found”) This helps to break up long commands to help you get the logic right in your own mind, before committing to a shell script as these sorts of things can get very long and span multiple lines on the terminal. Which reminds me…
Just touching on another thing quickly – if you have bear paws like me and you have typed “\” with “enter” as the two keys are above each other, you will be frustrated, but also aware of the power of the “\”. (or not?)
Please leave your terminal in the default 80×25 and type:
echo “ This is a very long line of text that I'd rather \have across multiple lines of console to ease readability.”
and hit <enter>
This means that if you typed say, touch fcm\ -and hit <enter> and end up with a “>” on a new line, as a newbie, you should not panic and press CTRL+c like I saw this chappie do. Sticking with the above example, you can simply type: .txt -on the next line and hit enter. (I’ll add a screen-shot, as a picture may explain it a bit better.)
Hitting backslash before Enter, simply says “continue on a new line”, meaning you should go ahead like nothing happened.
So, when you are using all of the above in your daily terminal sessions, you know that you have arrived.
If you have any issue with what was said: misc@fullcirlemagazine.org