Tips for understanding LinuxShell and basic Shell scripting language

Source: Internet
Author: User
The image is better than the text. it outlines the work of Linux: Understanding LinuxShell: shell: a command line interpreter that helps users deal with the operating system by executing commands or scripts. Process: process. The task executed by the user is a process, but the process is not just a task. File

The image is better than the text, which outlines the work of Linux:

Understand Linux Shell:

Shell: a command line interpreter that helps users deal with the operating system by executing commands or scripts.

Process: process. The task executed by the user is a process, but the process is not just a task.

File: The file is stored on the hard disk and contains all the user data.

X-windows (also known as windows): A Linux mode that enables the screen to contain multiple windows, allowing users to monitor multiple tasks at the same time, so that they can easily move between tasks, it is graphical.

Text terminal: a window that can only display text or simple graphics.

Session: log in, establish a session, log out, and log out.

Shell types in standard Linux distributions:

Bourne Shell: It was a major shell in earlier versions and later became a standard. Written by Stephen Bourne from Bell Labs. Each Unix-like system has at least one shell compatible with it. The program of the Bourne Shell is named "sh", which is usually put in/bin/sh.

C shell: written by Bill Joy for the BSD version. Its syntax imitates the C language. It is mainly used for interactive operations and has many interactive commands, but is not often used for writing scripts.

Start playing!

There are thousands of commands. remember them, OK? Yes, it is obviously not. The real purpose of a computer is to simplify your work. you need process automation, so you need scripts.

Scripts are a collection of commands and saved in files. Shell can read these files and execute commands, just like executing commands sent from the keyboard. Shell also provides a variety of programming features, making the script really powerful.

Shell Programming basics:

Enable the terminal to obtain the shell

Let's see what shell you are using and run echo $ SHELL.

In Linux shell, $ represents a variable.

The "echo" command is to reply to anything you typed

When several commands need to be connected, the pipeline command "|" is useful.

Linux commands have their own syntax, and your errors will not be tolerated or accepted. If you write an nonstandard command, you certainly won't execute it as expected.

#! /Bin/sh -- this is called the environment. This statement is written in the first line of the shell script so that the command is sent to the/bin/sh program.

Shell script:

A shell script is a common text file with the extension of. sh and can be executed.

Compile and execute the script:

Open Terminal

"Cd" command to go to the place where you want to store the script

Cd, press enter to change your prompt to the home directory

Touch hello. sh (here our script is named hello, and. sh is required)

Vi hello. sh (nano hello. sh) with your favorite editor, I use nano

Chmod 744 hello. sh (make the script executable)

Sh hello. sh or./hello. sh (execute the script)

Write your first script:

#!/bin/bash# My first scriptecho "Hello World!"

As mentioned above, save these lines in a text file so that they can be executed and executed.

Output:

Hello World!

The above code is explained as follows.

#! /Bin/bash (runtime environment) # My first script (comments, anything located after # Is commented) echo "Hello World !" (Main part of the script)

Write your second script:

It's time to write the next one. This script will name your username and list the processes that are running.

#! /bin/bashecho "Hello $USER"echo "Hey i am" $USER "and will be telling you about the current processes"echo "Running processes List"ps

As mentioned above, save these lines in a text file. the file name can be used, but remember that the extension is. sh to make it executable and execute it.

Hello tecmintHey i am tecmint and will be telling you about the current processesRunning processes List  PID TTY          TIME CMD 1111 pts/0    00:00:00 bash 1287 pts/0    00:00:00 sh 1288 pts/0    00:00:00 ps

Cool? Writing scripts is as simple as converting an idea into a stream program command. With built-in filters and MPs queues, shell scripts are good at simple file system operations.

When your needs are more complex, such as functions, robustness, performance, and efficiency, you need an all-powerful language.

When you are familiar with C, PERL, PYTHON, and so on, it is not difficult to learn the script language.

Write your third script:

The third script is the last script in this article. This script is an interactive script. why don't you execute it yourself?

#! /bin/bashecho "Hey what's Your First Name?";read a;echo "welcome Mr./Mrs. $a, would you like to tell us, Your Last Name";read b;echo "Thanks Mr./Mrs. $a $b for telling us your name";echo "*******************"echo "Mr./Mrs. $b, it's time to say you good bye"

Output:

Hey what's Your First Name?Avishekwelcome Mr./Mrs. Avishek, would you like to tell us, Your Last NameKumarThanks Mr./Mrs. Avishek Kumar for telling us your name******************************************************Mr./Mrs. Kumar, it's time to say you good bye

In fact, this is not over yet. We just gave you a try. In the future, we will fully develop the "script" topic, which will continue without being the most detailed, but more detailed. Your comments are awesome! Please support us and help us publicize and keep in touch. stay tuned!

TecMint.com translation: Bole online-unblock

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.