Shell Programming (vi) script preliminary

Source: Internet
Author: User
Tags define local line editor shebang


Shell script Programming:

Classification of programming languages: depending on how they are run

Compile-and-run: Source code--compiler (compile)---Program files;

Explanation Run: Source code-and run-time start interpreter, run by interpreter side explanation side;


Depending on whether the implementation of the functionality in the programming process is calling the library or calling the external program file:

Shell script Programming:

Programming with commands and programming components on the system;

Full programming:

Programming using a library or programming component;


Programming Models: Procedural programming languages, object-oriented programming languages

program = instruction + data

Program: To organize the code with the instruction as the center, the data is to serve the code;

Sequential execution

Select Execute

Loop execution

Representative: C,bash

Object type: Organize the code with data as the center, organize the instruction around the data;

Class: Instantiate object, method;

Representative: Java, C + +, Python


Shell scripting: Procedural programming, interpretation of running, dependent on external program file running;


How to write a shell script:

The first line of the script file, shelf: Gives the shebang, the interpreter path, indicating the interpreter program file that interprets the execution of the current script

Common interpreters:

#!/bin/bash

#!/usr/bin/python

#!/usr/bin/perl


Text Programmer: Nano

Line Editor: SED

Full Screen programmer: Nano, VI, VIM


What is a shell script?

Stacking of commands;

However, many commands do not have idempotent, and it is necessary to use program logic to determine whether the running conditions are satisfied, so as to avoid errors in their operation.


To run the script:

(1) Give execution permission and run the program file directly;

chmod +x/path/to/script_file

/path/to/script_file

(2) Run the interpreter directly and pass the script to the Interpreter program with command line parameters;

Bash/path/to/script_file


Note: Blank lines in the script are ignored by the interpreter;

In the script, except for Shebang, all the remaining lines beginning with # will be ignored as comment lines;

The execution of a shell script is accomplished by running a child shell process;


Exercise 1: Write a script that implements the following functions;

(1) Display all files or directories in the/etc directory that begin with uppercase p or lowercase p;

(2) Displays all the files or directories in the/var directory itself, and converts the lowercase letters in the displayed results to uppercase and then displays;

(3) Create temporary file/tmp/myfile. XXXX;


Bash configuration file:

Two categories:

Profile class: Provides configuration for the interactive logon shell process

BASHRC class: Provides configuration for non-interactive logon shell processes


Login Type:

Interactive logon Shell process:

Login to open shell process after entering account and password directly through a terminal;

Use the SU command: su-username, or login switching performed with su-l USERNAME;


Non-interactive logon shell process:

The login switch performed by SU username;

A terminal opened under a graphical interface;

Run the script


Profile class:

Global: Effective for all users;

/etc/profile

/etc/profile.d/*.sh


User personal: Valid only for the current user;

~/.bash_profile


Function:

1, for defining environment variables;

2, run the command or script;


BASHRC class:

Global:

/etc/bashrc

User personal:

~/.bashrc


Function:

1, define local variables;

2, define the command alias;


Note: Only the administrator can modify the global configuration file;


Interactive logon Shell process:

/etc/profile--/etc/profile.d/*--and ~/.bash_profile--~/.BASHRC--/ETC/BASHRC


Non-interactive logon shell process:

~/.BASHRC--/ETC/BASHRC-/etc/profile.d/*


attributes defined in the command line, such as variables and alias scopes, are the life cycle of the current shell process;

configuration file-defined attributes that are only valid for subsequent shell processes that are newly started;


Let the attributes defined by the configuration file take effect immediately:

(1) Repeated by the command line, defined once;

(2) Let the shell process reread the configuration file;

~]# Source/path/from/conf_file

~]# . /path/from/conf_file


Issue 1: Define command aliases that are valid for all users, such as lftps= ' Lftp 172.16.0.1/pub '?

Question 2: When a CentOS user logs in, it is prompted to log in and displays the current system time?


Shell Programming (vi) script preliminary

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.