Shell programming under Linux

Source: Internet
Author: User

Shell Programming for Linux


1. What is a shell?
When a user logs on to a Linux system, the system initialization program Init runs a program called a shell (shell) for each user.
The shell is a command-line interpreter that provides the user with an interface system-level program that sends a request to the Linux kernel to run the program, allowing the user to start, suspend, stop, or even write programs with the shell. Typical Linux systems use bash as the default shell.


2. Several popular shells
Currently the popular Shell has ash, bash, Ksh, csh, zsh, etc., you can use the following command to see the type of Shell:
Echo $SHELL
The shell is an environment variable that records the type of shell the user is using.


3. If you want to execute the script, you must also make it executable.
chmod +x filename
You can then execute your script by entering:./filename.


4. The first shell program

$vi firstscript.sh


echo "You is welcome to use base."
echo "Current work directory is $PWD."
echo "You are $LOGNAME."


Increase execution permissions
$chmod +x firstscript.sh
To start running the program:
$./firstscript.sh


5. Simple operation
$[expression]
For example:
var1=2
var2=$[$var 1*10+1]
Echo $var 2
The return value is 21.


-r file Name: True if the file exists and is readable
-W file name: True if the file exists and is writable
-X File Name: True if the file exists and is executable
-S file name: True if the file exists and has at least one character
-D file Name: True if file exists and is directory
-F file Name: True if the file exists and is a normal file
-e File name: True if the file exists
-C file Name: True if the file exists and is a character-specific file
-B file Name: True if the file exists and is a block special file


-A logic and
-O Logic or
! Logical No


The String_operator can be:
=: two strings equal
! =: two strings in different sizes
-Z: Empty string
-N: Non-empty string
-eq: Values Equal
-ne: values are not equal
-GT: Greater Than
-LT: Less than
-le: Less than or equal to
-ge: greater than or equal to


6. Command format: Read variable Name list
Function: input data from keyboard or file, assign value to variable. The shell uses spaces as delimiters between variables.
Press ENTER or Ctrl+d to end the input.
When read reads data from a file, it is read by line, which reads one line at a time. If the carriage return is not encountered, the end of the file is always read. The data is read in the same way as it was entered from the keyboard.
$read Name Sex Age
Yangailin Male 25
$echo $name
Yangailin
$echo $sex
Male
$echo $age
25


$read name Sex
Yangailin Male 25
$echo $name
Yangailin
$echo $sex
Male 25


$read name Sex
Yangailin Male 25
$echo $name
Yangailin
$echo $sex
Male 25


7. Piping pipe
You can pass the output of one command to another command as input through a pipeline. Pipe with a vertical bar | Indicates that it has the following syntax format:
Command 1 | Command 2


8. Cat commands
The cat command can be used to display the contents of a file, create a file, and use the control symbol (with the-v option).
The cat command displays the entire complete file at once.
If you want to display one page at a time, you can use the more command
$cat filename1 filename2 > Filename3
#该命令把两个文件连成一个文件filename3


9. Shell Advanced Programming
The shell allows the user to put special permutation characters between different commands. The user needs to use a semicolon ";" if they want to give multiple commands in a row.
Command1; Command2


Command1 && Command2
The above command indicates that Command2 will be executed only if Command1 is correctly running.


Command1 | | Command2
The above command indicates that Command2 will be executed only if Command1 fails to run.


Under the shell, a newly generated process can be executed by using the symbol ";" and "&" in the back of the command, respectively, in the foreground and background, with the following syntax:
command; Generate a foreground process, the next command must wait until the command is finished before it can be entered.
Command & generates a background process that, while running in the background, can enter additional commands.


There are two ways to group commands in the Shell: "()" and "{}", which should be placed in parentheses when the user does not want the command to run when a change to the State collection (such as positional parameters, environment variables, current working directory, etc.) affects the execution of the following statement.
{} The output of the command executed sequentially is used for input to another command (pipe mode)


The shell program is debugged primarily by using the Bash command to interpret the program's selections. The form of calling bash is:
Bash-Select the shell file name of the program
-E: Exit immediately if a command fails
-N: Read-in commands but do not execute them
-U: Not set variable as error when permutation
-V: Displays the lines when they are read into the shell input line
-X: Show commands and their arguments when executing commands


The primary way to debug a shell program is to use the shell command interpreter's "-V" or "-X" option to track program execution. The "-V" selection causes the shell to display every command line it reads into while executing the program, and the "-X" option causes the shell to display each command it executes during the execution of the program at the beginning of the row with a "+" plus a command name. and displays the value of each variable and the variable.

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.