Outils pour utilisateurs

Outils du site


issue177:c_c

Ceci est une ancienne révision du document !


Most of us don’t think twice when typing at a terminal, it’s just there. What is actually happening is that you are typing commands into a shell that is interpreting your commands. By default, Ubuntu ships with bash, but you can install another if you like. I will focus on bash. The reason being, that if you ever get to use servers or containers on the internet, chances are good that it is bash. A lot of time I hear, it is difficult to learn shell scripting. Sure, bash is not the friendliest of syntaxes, but sometimes you just need a question answered and a small shell script will do. The question may be something arbitrary like: Do I have any money? If you are a wage serf like me, the only time you have money is when you have been paid. So to simplify the question, I might ask, have I been paid? Now that is a yes or no question, the kind the computer likes. Yes or no questions translate easily into true or false. There is a dilemma in this question, as the computer has no access to my bank account and therefore has no idea if I have been paid. However, I can do the next best thing, check if it is ‘payday’. The computer has no idea what ‘payday’ is, but we do, it is on the 27th of each month. We have a target and a yes or no question, so we can write a simple script.

Open those terminals and let's have some silly fun. Type: nano gotmoney.sh and press enter. As you know, all shell scripts require a path to the shell. So we start off by adding: #!/bin/bash -to the header of our script. We can use bash’s if-statement to ask the question; “If today is the 27th … TRUE or FALSE” then we got paid or did not get paid” The if statement in bash does this nifty trick of opening with the word “if” and closing with “fi” (the reverse). The way it works is, IF TRUE, then do something, else do nothing. So this is our outline, we now need to formulate the question. Here is where you need to think about what you want. Remember, garbage in, garbage out. Let’s open another terminal or tab and test out our ‘question’. This is important, if your command does not work in bash, it will not work in your script.

First, let’s see what the switches are, type: man date and press enter. Going down a bit you will see - “FORMAT controls the output.” After all, I am only interested in the day, as I get paid on the 27th , every month and therefore the month and the hours and seconds are of no use to me. If you don’t understand what I am talking about, simply type: date and press enter. According to the man page, “%d” is what I am looking for as it represents the day. Let’s test that out, type: date %d and press enter. What happened? You did not pay attention to the Synopsis part; it says, “+FORMAT”. Now try: date +%d and press enter. Great, now we have something that works. Last issue we learned about the $ variable. Now it’s time to use it. So… if some condition is true -do something. (The technical description of an if statement, hehe) Clear this tab or second terminal and type: if [ $(date +%d) -eq 27 ] ; then echo “Quick, spend some before the wife sees it!!”; fi and press enter What happened? Well unless today was the 27th , you would get no output. Remember, if the condition is FALSE, nothing happens. (Else is not part of this discussion) Let us dissect that query: IF [ TRUE ]; then blah…

The [ TRUE ] part is then broken into a question to determine true or false. If our variable “date +%d” (it is in brackets as it is not one word) -eq (equal to) 27. We have -eq as our comparison operator like -gt for ‘greater than’ and -lt for ‘less than’. You learned these in school, so you should know them by now. If you are unsure, see: https://tldp.org/LDP/abs/html/comparison-ops.html Also remember the spaces with the square brackets as they are commands. “[ “ and “[[ “ (we will get to these much further down the line when we get to test) Before anyone comments, I am not a code monkey, this reads easier for me than all the unnecessary white space. Save it and set the script executable. (You should know that much by now) Just to test the validity, try changing the 27 to today’s date and see if the echo fires. Congratulations! You wrote a script! You can put this into cron and have it fire once a day. That way when it is ‘payday’, you will get a friendly reminder! Cron is beyond the scope of this article, but we will visit it in the future. Did I make a mistake? misc@fulcirclemagazine.org

issue177/c_c.1643583105.txt.gz · Dernière modification : 2022/01/30 23:51 de d52fr