Getting Started with Linux shell script programming (i)

Source: Internet
Author: User
Tags logical operators readable

Environment variables


Some commands

Various environment variables often appear in shell scripts, so to write a script, you must first understand the environment variables. Common commands for handling environment variables are listed below

    • Set is used to display local variables
    • env to display environment variables
    • Export to display and set environment variables
    • Source followed by a file, you can directly execute the script command in the file and update
Common environment variables
    • Position parameters

Equivalent to the C language, the main function of the argv array, which is represented here in $num, where Num refers to a number. See the program example below

# Foot.sh#!/bin/shecho "#0" echo "#1" echo "#2 $" echo "#3 $" echo "#4 $4" echo "#5 $" echo "#10 ${10}" # Note the curly braces here

    • List some variables

List some common variables that can be viewed directly through ECHO

PWD current path, bash's pwd command is the output of the path oldpwd the previous directory Pathshell the command path home user's home directory Shell current shelluser username UID User ID can refer to/etc/ PASSWDPPID Create the process number of the current process, which is the parent process number PS1 prompt variable

Reference

General strings can be used without quotes, with quotation marks (single or double quotes, to block special characters, such as asterisks)

"1 is $" Double quotes A string that references a backslash (\) In addition to the dollar sign ($) backslash (\) can output single quotation marks
' 1 is $ ' Single quotation marks The strings inside the single quotation mark are output as is, including double quotes, but cannot output single quotes
Vara= ' pwd ' Anti-Quote The contents of the inverted quotation mark are interpreted as shell commands.
Note: ${pwd} and ' pwd ' mean the same, the returned result is a string, and the newline character is deleted


Comparison

Place the expression in brackets, such as ["$num 1"-eq 20] and return 0 if the condition satisfies.

Comparison operators

-eq Euqal ==
-ge Greater or equal >=
-gt Greater >
-le Less or equal <=
-lt Less than <
-ne Not equal !=


File operators

-D File Directory is a directory
-E File Exist Whether there is
-F File File is a normal file
-R File Readable Whether it is readable
-S file Whether the file length is not zero
-W File Writable Whether it can be written
-X File Executive is executable
-L File Linkable Whether it is a symbolic link


logical operators

!expression Logical Non-
Expression1-a expression2 Logic and
Expression1-o expression2 Logical OR OR


Getting Started with Linux shell script programming (i)

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.