Ceci est une ancienne révision du document !
One of our NOC engineers is writing some Linux exam/test/whatever. We were having a chat and he confessed not knowing how test worked. I thought about it and realised I have never typed test at the command line either. He showed me what he was looking at: https://opensource.com/article/22/10/test-command-linux I had to read it twice, to figure out what they were talking about. The very last piece hit me, back in the day, I got caught with the c_c command, and it took me a few retries to grok it. It turns out it is something I have used often, never knowing that it was also a command (named test). Before I go on “my way” of doing things, let's look at this from the bottom up. So if you are a newbie and someone tells you to read the man page, you have my permission to spit in their coffee. (OK don’t) Seriously, the man page is about as much help to a newbie as a pin in a rainstorm if you don’t know what test does. It says nothing… I need you to think of it as a comparison command in bash that nets you a boolean. That’s it. To understand where this comes from, you need to know a bit about programming. Not much, just enough to hurt yourself. You see, when a C program exits cleanly, without error, your exit status is 0. If not, it is not 0. (make sense?) The same happens here, you evaluate or compare two things and you get 0 or 1. There is no hidden meaning or anything esoteric. It’s true or false. If you want to see that result, you type: echo $?
Go ahead, open a terminal and type it to see the default value. I’m not going to tell you what it is, you need to see it for yourself. OK, with that out of the way, let’s use it to see how it works. Type: test 1 eq 2 ; echo $? Yes, I know it is oversimplified, but you are going to have to look at it that (the simple) way. You should have got an error. Look at the output. Now let’s do it right and look at that output (proper term is exit status), type: test 1 -eq 2 ; echo $? This is where the man page comes in handy, helping you remember all the comparison operators. I’m not going to list them here, you have a terminal. If you are a native English speaker, you should know them. If you are not, now you have the opportunity to learn them. Now the part I did not know is that I have been using test all along, just not in that way. Open your terminal and type: [ 1 -eq 1 ] ; echo $? mmm… Oh and be aware that there is a space after “[“ and one before “]” as, with “[[“ it is then treated as a command. So what happened? Yes same pudding, different sauce. Go ahead and leave out the space, so you can see what the error looks like. No, really, this is important. Familiarity breeds contempt, if you are used to the error, you know how to fix it.
So the syntax is, we compare something in the “box” and we can then do something (or nothing) with the result (0 or 1). Let’s do a very simple example, type: [ 6 -gt 7 ] && echo “it is!” || echo “nope…” Now do it again, swapping the 6 and the 7 around. What happened? Do you see why I said it is a comparison that nets you a boolean? NOTE: if you are too lazy to type and copypasta, remember that this: “ is not this: “ That was integers, but you can also do it for strings. For strings we can use “=” and “!=” Type: [ “FCM” = “fcm”] ; echo $? Type: [ “FCM” != “fcm”] ; echo $? If you did not get those t-shirts before; != is funny ‘coz its true, you will now. We can also use the “zero” and “non-zero” operators, “-z” and “-n” Type: [ -n “fcm”] ; echo $? Type: [ -z “fcm”] ; echo $?
You are all smart people, so I’m not going to pull out the wax crayons here. Let’s take it one step further, we can use it on files and folders, why? Because we get a boolean back. Type: [ -e a.out ] ; echo $? This tests for a file named “a.out”. You can test for any file you like. (bottom left is a screen-shot to see it in action.) So I have a file named icon.svg, but not one named icons.svg in my folder. Now, the “-e” is for “exist”, but we can also check if it is a file or a directory, and yes, you guessed it they are “-f” or “-d”. As I said, this is not difficult. As you can see, once you get the basics, the rest just fall into place. Like, for instance, would you like to know if a file has write permissions?
Hah, see you are getting smarter already! Yes, type: [ -w a.out ] ; echo $? So now you know* how the test command works, you can go through the switches in the man page and it will start making sense to you. One of the other options you may come across is “-s” which checks if a file has meat on the bones. To see this in action, type: touch man.man && [ -s man.man ] ; echo $? Did you understand what you just typed? If not, misc@fullcirclemagazine.org