Linux Shell: My first shell program

Source: Internet
Author: User
Here, I will first introduce what a shell is, then introduce my first shell program and the lessons learned from it. 

I. What is shell?

Before talking about my shell program, I 'd like to tell you what shell is. I believe you have heard a lot about shell, but what is it actually? In fact, shell is a program. If we enter some commands in shell, it tells the operating system kernel what we want to do, so that the kernel can control the hardware to work correctly. Simply put, the shell function only provides an interface for the user's operating system, and the shell can call other programs to complete these operations, all interfaces that can operate applications are called shell. In Linux, the shell is/bin/bash by default. What is a shell program? To put it simply, write a file that can be directly run in shell. To put it bluntly, it is similar to directly entering a command in shell, but writing it to a file according to certain rules, and then directly executing the file to execute the written command. 2. My first shell program [Plain]View
Plaincopyprint?

  1. #! /Bin/bash
  2. # Filename: filetype. Sh
  3. Read-P "Please input the filename:" filename
  4. Fpath = $ filename
  5. If [-d $ fpath];
  6. Then
  7. Echo "$ fpath is a direstory .";
  8. Elif [-e $ fpath];
  9. Then
  10. Echo "$ fpath is a file .";
  11. Else
  12. Echo "$ fpath is not a file or direstory .";
  13. Fi

This is my first shell program. The function is very simple. It is to input a file name and determine whether the file name is a directory or a file. If neither of them is the same, a prompt is given. Although this is a very simple program, when I was writing this program, because I had never been learning C and C ++ before, and had never touched on any scripting language, so it can be said that it is written with errors and errors, and it is very difficult to write and run. So here I will talk about my experience, so that others who have been learning static and strong languages like C ++ and have never learned scripting languages will face some detours. 3. Program Analysis 1. fpath = $ filename first, let's take a look at this sentence. In Shell, variables do not need to be defined and used like C or C ++. In shell, you just need to write the file directly. filename is the variable in the previous sentence to save the input file name. You must pay attention to the writing of this sentence. The common errors are as follows: 1) fpath = $ filename. Note that such writing is another meaning in shell, determine whether the filename value is fpath. Note that it is fpath, not the value corresponding to the fpath variable. In Shell, A = B is totally different from a = B. The former is a value assignment and the latter is a comparison. This may be fatal for programmers who are familiar with C/C ++, because in C/C ++, A = B is exactly the same as a = B, in addition, many people like the second method, because it looks nice to write code, but it is planted with a large root header in shell. 2) fpath = filename for C/C ++ programmers, since filename is a variable and fpath is also a variable, fpath = filename should assign the value of filename to fpath, it should be okay. But it is not in shell, because the variables in shell do not need to be defined, you want shell to know that this is a variable, not a common string, you should add a $ in front of it, so when you write fpath = filename, it means that the value of the fpath variable is assigned filename, instead of the value corresponding to the filename variable, because you have not added the $ sign, the default filename is just a normal string. Therefore, when you want to assign the value of filename to fpath, you should write it as fpath = $ filename. In this case, shell considers filename as a variable and then retrieves the value of filename and assigns it to fpath. 2. If [-d $ fpath]; the second one is worth noting. Note that all spaces in this statement are required, note that there must be a space between If and. In Shell, this is not a problem of encoding standards or styles, but a syntax problem. 3. Echo "$ fpath is a direstory. "; note: In Shell, double quotation marks are different from single quotation marks. Although you can print strings, variables in double quotation marks are replaced by their values, variables are not evaluated in single quotes. For example, if you enter:/root in the program, the output is/root is a direstory .. $ Fpath is replaced with/root. If you change to echo '$ fpath is a direstory.';, no matter which directory you enter, the output will only be: $ fpath is
A direstory .. In addition, ECHO can also be left with no double quotation marks or single quotation marks. However, in this case, you cannot use command delimiters in Chinese versions. The above is my summary based on the error experience. I hope to help you. I just got in touch with shell. If there is any error, please point it out!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.