Here I will first describe what a shell is, and then introduce my first shell program and summarize the experience from it.
First, what is the shell
Before talking about my shell program, I would like to talk to you about what is shell, I believe that the word shell you must have heard a lot, but what is it actually? In fact, the shell is a program, we enter some commands in the shell, it tells the operating system kernel what we want to do, so that the kernel can control the hardware to work correctly. The simple point is that the function of the shell is simply to provide an interface to a user's operating system, and the shell can invoke other programs to do so, as long as the interface that operates the application is called a shell. And now the shell of Linux in the default is/bin/bash this shell.
What is a shell program? In simple terms, write a file that you can run directly in the shell. To put it bluntly is to enter the command in the shell almost, but it is in accordance with a certain law written to a file, and then directly execute the file to execute the inside of the written command.
Second, my first shell program
#! /bin/bash
# filename:FileType.sh
read-p "Please input the filename:" filename
fpath= $filename
if [D $ Fpath];
Then
echo "$fpath is a direstory.";
elif [-e $fpath];
Then
echo "$fpath is a file.";
else
echo "$fpath is not a file or direstory.";
Fi
This is my first shell program, the function is very simple, is to enter a file name, to determine whether the file name is a directory or file, if both are not, give the corresponding hints.
Although this is a very simple program, I write this program, because I have not been learning C and C + +, no contact with any scripting language, so can be said to write a lot of mistakes, very hard to write it to be able to run. So I'm here to talk about my experience so that other people like me who have been learning static strongly typed languages like C + + and have not learned the scripting language are less likely to take detours.
Third, the procedure analysis
1, fpath= $filename
First of all take a look at this sentence, in the shell variable is not like C or C + +, you need to define and reuse, in the shell, directly write directly to the line, filename is the variable in the previous sentence, to save the file name we entered. We must pay attention to the wording of this sentence, common mistakes are as follows two points:
1 Fpath = $filename, note that this type of writing in the shell is another meaning, that is, to determine whether the value of filename is Fpath, attention is fpath, not fpath this variable corresponds to the value. Because the a=b in the shell is completely different from a = B, the former is the assignment, the latter is the comparison. This may be fatal to programmers who are accustomed to C/s + +, because in C + +, A=b is exactly the same as a = B, and a lot of people like the second way, because it looks good, but the shell has a big head.
2) Fpath=filename
For C/s + + programmers, since filename is a variable, Fpath is also a variable, then fpath=filename should be the value of the variable filename assigned to Fpath, should be no problem. But not in the shell, because the variables in the shell do not need to be defined, you want the shell to know that this is a variable, not a normal string, you should add a $ in front of it, so when you write Fpath=filename, It means fpath the value of this variable to filename, not the value of the variable named filename, because you didn't add the $ number, so the default filename is just a normal string. So when you want to assign the value of filename to Fpath, you should write the fpath= $filename, when the shell is to think that filename is a variable, it will take out the value of filename and assigned to Fpath.
2. If [-D $fpath];
The second noteworthy place is here, and note that all the blanks in this statement are required, especially if there must be a space between the if and the [. Because this is not a code or style problem in the shell, it's a grammatical problem.
3, echo "$fpath is a direstory.";
Note that in a shell, double quotes are different from single quotes, although you can print a string, but a variable in double quotes is replaced with its value, and the variable is not evaluated in single quotes. For example, in the program you enter:/root, the output is:/root is a direstory. The $fpath was replaced with/root. If you change to echo ' $fpath is a direstory. ', the directory will only output whatever you enter: $fpath is a direstory. Also, echo can be without double quotes and single quotes, but in that case you won't be able to use the command delimiter in the text you want to display.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/