Ceci est une ancienne révision du document !
Last issue, we looked at a simple script where we asked a question and manipulated the answer. The issue before that, we looked at variables, so how about we build on what we know so far and add another Lego-block? We will not make big jumps here as I don’t expect you to know anything about scripting, however, I do expect you to know simple things like operators, what type of bracket I am talking about, and how to look up a command in a man page (or bro pages, or yelp, or whatever).
How about we go script-ception? Scripts within scripts, that make scripts. Now the recipe for making scripts is; you create the file, edit the script and change the mode before running it. (Simplified). The main reason I write scripts is usually to automate something tedious. Imagine having to create say 44 users on a new server for scanners in the organization. Instead of having to create scanner1, then scanner2, and so on, wouldn’t it be nice to do it all in one go? This is where you would use scripts in real life. Have you deployed a bunch of containers and need to do something across a bunch of them? Script. So, what I am sharing with you is not useless. However, many times, people will try to tell you the terminal is dead, it really isn’t.
Join me by whipping out your favourite terminal emulator and do what I do. I am a firm believer that when you do something, you remember it better than just reading. I promise to keep these short, so you don’t get bored. Just one quick point before we have to come back, we looked at outputting text to the screen with echo, but we can also take input with “read”. Let me illustrate;
nano exts.sh - to start a new file in the nano editor.
#! /bin/bash
touch new
read -p “What extension would you like: “ ext
mv new new.${ext}
Now save that script and make it executable (you already know how to do this), then run your script. Please do not copy / paste as it will not work, type it in. Newer versions of nano support syntax highlighting, so make sure yours looks something like mine, or you will get errors.
On a side note, you can use the -s option for things like passwords, where the user’s input will not be displayed, if you did not know about it.
When you run your script, it should exit cleanly and nothing will have changed in your terminal, but if you ‘ls’ or open your file browser, you should see a new file with the extension you typed in. In my case I made it ‘.txt’, yes very original.
You may have noticed the colon and space before the closing inverted commas, that is because the read command does not care and takes your input from where you type it, but we work with humans and it needs to be legible. (In other words, window dressing.) You also should not put a question mark there either as this will be treated as an operator. Please do try this and see the outcome, so you know the error when you see it.
The ‘mv’ is simply the move command we are using to rename the file. The syntax is, mv <old filename> <new filename> - nothing difficult or funny.
So now we can get to the lazy part…erm inception-style script creation. *cough
So fire up a terminal emulator, and nano like before, and let’s get cracking!
Get the code below saved, set your script to executable, and run it.
Here (bottom right) is a quick view from my PC so you can see the ‘before’ and ‘after’ for running the script. (I named the file tt.sh).
Now run your file as it is already executable. What happened? Well, go ahead and try, I am not saying.
You can do this over and over and every time you will have an executable script ready to go. You can embed almost any command / commands in a script, or have the script create more than one script. Can you see where this is heading? I am sure someone out there has created a script to create more scripts to fill a drive as a prank.
#! /bin/bash
read -p “what should the name of our script be: ” fname
touch ${fname}
echo “ #! /bin/bash ” » ${fname}
echo “ w ” » ${fname}
chmod +x ${fname}
echo “File created”
Now the first part we have discussed; touch simply creates an empty file with the name you gave it. When we use echo with the double greater-than sign or chevron, it will append whatever is in brackets to the end of the file we create. Make sure it is not a single greater-than sign as that will overwrite your file contents.
The chmod you know by now, and the last line was just added so you get some feedback when the process completes.
As you can see, nothing funny bash-wise, but it opens up a whole can of worms for those who want to try.
As always, if you have questions, email misc@fullcirclemagazine.org