issue177:c_c
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue177:c_c [2022/01/29 14:15] – créée auntiee | issue177:c_c [2022/02/01 16:03] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | 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. | + | **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. | + | 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: | + | La plupart d' |
+ | |||
+ | J' | ||
+ | |||
+ | |||
+ | **Open those terminals and let's have some silly fun. Type: | ||
nano gotmoney.sh | nano gotmoney.sh | ||
Ligne 13: | Ligne 18: | ||
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. | 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. | + | 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.” | + | Ouvrez ces terminaux et amusons-nous bêtement. Tapez : |
+ | |||
+ | nano gotmoney.sh | ||
+ | |||
+ | et appuyez sur Entrée. | ||
+ | |||
+ | Comme vous le savez, tous les scripts shell nécessitent un chemin vers le shell. Nous commençons donc par ajouter : # | ||
+ | |||
+ | Voici donc notre schéma, nous devons maintenant formuler la question. C'est ici que vous devez réfléchir à ce que vous voulez. Rappelez-vous ; mal ficelé au début, mal ficelé la fin (« GIGO, garbage in, garbage out »). | ||
+ | |||
+ | Ouvrons un autre terminal ou onglet et testons notre « question ». C'est important, si votre commande ne fonctionne pas dans bash, elle ne fonctionnera pas dans votre 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. | 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. | ||
Ligne 25: | Ligne 43: | ||
What happened? Well unless today was the 27th , you would get no output. Remember, if the condition is FALSE, nothing happens. | What happened? Well unless today was the 27th , you would get no output. Remember, if the condition is FALSE, nothing happens. | ||
- | Let us dissect that query: IF [ TRUE ]; then blah... | + | 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’. | + | Tout d' |
+ | |||
+ | Après tout, je ne suis intéressé que par le jour, car je suis payé le 27 de chaque mois et donc le mois, les heures et les secondes ne me sont d' | ||
+ | |||
+ | Selon la page du manuel, « %d » est ce que je recherche car il représente le jour. Testons cela, tapez : date %d et appuyez sur entrée. Que s' | ||
+ | |||
+ | Effacez cet onglet ou le second terminal et tapez : if [ $(date +%d) -eq 27 ] ; then echo "Vite, dépensez-en avant que votre femme ne le voie !!"; fi puis appuyez sur Entrée. | ||
+ | |||
+ | Que s' | ||
+ | |||
+ | Décortiquons cette requête : 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’. | ||
https:// | https:// | ||
Ligne 42: | Ligne 73: | ||
Cron is beyond the scope of this article, but we will visit it in the future. | Cron is beyond the scope of this article, but we will visit it in the future. | ||
- | Did I make a mistake? misc@fulcirclemagazine.org | + | Did I make a mistake? misc@fulcirclemagazine.org** |
+ | |||
+ | La partie [ TRUE ] est ensuite décomposée en une question pour déterminer si elle est vraie ou fausse. Si notre variable « date +%d » (elle est entre parenthèses car il ne s'agit pas d'un mot) -eq (est égale à) 27. Nous utilisons -eq comme opérateur de comparaison, | ||
+ | https:// | ||
+ | |||
+ | N' | ||
+ | |||
+ | Avant que quelqu' | ||
+ | |||
+ | Sauvegardez-le et rendez le script exécutable. (Vous devriez savoir comment faire maintenant.) | ||
+ | |||
+ | Juste pour tester sa validité, essayez de changer le 27 en date d' | ||
+ | |||
+ | Félicitations ! Vous avez écrit un script ! Vous pouvez le mettre dans cron et le faire déclencher une fois par jour. Ainsi, lorsque le jour de la paie arrive, vous recevrez un rappel amical ! | ||
+ | |||
+ | L' | ||
+ | |||
+ | Ai-je fait une erreur |
issue177/c_c.1643462117.txt.gz · Dernière modification : 2022/01/29 14:15 de auntiee