Getting Started with shell scripting--variables

Source: Internet
Author: User

First, the basic
View all shells
Cat/etc/shells or Chsh–l
1. Examples of scripts.
Linux does not differentiate files, for the convenience of memory, here with. Sh end.
(1) Vim first.sh
#!/bin/bash
Mkdir/root/shell
Ifconfig
(2) Add Execute permissions chmod +x first.sh The file will turn green at this point.
(3) Execution: 5 ways:
①./first.sh
② Absolute Path execution
③sh + script name (no execute permission required)
④source + script name (does not require EXECUTE permission) recommended use
⑤. + PIN Name

Tip: When you are finished viewing a path, use ESC +. , you can call Lu Jinglai to paste directly.

2. Variables
Common shell variables
Custom variables: Variables defined by the user according to their environment, for example: a=b
Environment variables, positional variables, pre-defined variables.

Call variable: Echo $+ variable name
(1) When 2 sets of variables need to be called: Echo $Linux $linux
(2) Variable name is easy to confuse with {} to enclose the variable name: Echo ${linux}system
(3) Variable values have some other special compliance: linux= "Rhel 6.5"
(4) Reference variable: system= "RHEL $linux"
(5) Single quotation mark: When assigning a special symbol case: a=1,b= ' $a ', echo $b
(6) Anti-apostrophe " “ ,esc下边那个按键。将命令输出结果赋给变量,反撇号括起来范围必须为可执行命令。例:rpm –qf which pwd"
3. Read command
Example: Read Dell HP
1 2
Reference variable: Echo $dell, outputs 1 echo $hp and outputs 2.
For interactive image display, add –p to display the prompt message.
Example: read–p "Input your password:" Password
4.
View all current variables by set
ENV View global Variables
Export setting global variables
Example: #export a=1, #bash, #echo $a, a value of 1 is returned.
5. Operation of numerical variables
There must be an empty space between the operator and the variable. The operation of integers is mainly done by the internal command expr command.
Example: Expr $A + $B (addition) expr $A * $B (multiplication) expr $A% $B (division)

Assigning the result of an operation to another variable

6. Position variable

Example: Vim weizhi.sh


7. Pre-defined variables
$#: Number of positional variables in the command line (the program executes several positional parameters)
$*: The contents of all positional variables (specific content such as/boot is a specific content)
$?: The state returned after the previous command was executed, when the return status value is 0 indicates normal execution, and a value other than 0 indicates an exception or error
Determine if an error occurs correctly for 0 exception error for non 0 value between 1-127
$: The currently executing process/program name (which is the name of the currently executing command or program)

Tip: Add Environment variables

Vim/etc/profile

At the end of the document, add:
Export path= "/opt/stm/stlinux-2.3/devkit/sh4/bin: $PATH"
(/opt/stm/stlinux-2.3/devkit/sh4/bin) is the path to add

Getting Started with shell scripting--variables

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.