Shell Script Basics 1 Overview and variables

Source: Internet
Author: User
Tags aliases stdin

Shell Overview:
The interpreter program between the Linux kernel and the user
Usually refers to/bin/bash
Responsible for directing to the kernel to translate and communicate user/program instructions
Equivalent to the "shell" of the operating system

How the shell is used:
Interactive--Command line: manual intervention, high degree of intelligence, the implementation of an article by article, low efficiency
Non-interactive-script: need to design in advance, intelligent difficult, batch execution, high efficiency, easy to run quietly in the background

To switch the shell environment:
Chsh Change the login shell via Usermod
Manually execute the target shell program
[Email protected] ~]# Cat/etc/shells
/bin/sh (most Unix default shell)
/bin/bash (most Linux uses the default shell)
/sbin/nologin (non-landing shell)
/bin/dash
/bin/tcsh
/bin/csh
/bin/ksh

Bash Basic Features:
command-line Environment review
Shortcut key, TAB key
Command history
Command aliases
Standard input and output
redirect
Pipeline operation

/etc/profile (Environment variables and startup programs)
/ETC/BASHRC (functions and aliases)
/root/.bash_profile
/root/.bashrc


Writing and executing scripts:
#!/bin/bash
For i in {1..10}
Do
Useradd hydra$i 2>>uerror.log >/dev/null
echo "123" |passwd--stdin user$i 2>>perror.log >/dev/null
Done

To add Execute permissions:
[Email protected] ~]# chmod +x x.sh
[Email protected] ~]#./x.sh
HAIL HYDRA!!!!!

Execute when not authorized: (Because interpreter rights have permission)
[Email protected] ~]# chmod-x x.sh
[Email protected] ~]# bash x.sh
[Email protected] ~]# sh x.sh
(Above two open sub-processes)
[Email protected] ~]# source x.sh
[Email protected] ~]#. x.sh
(above two non-open sub-processes)

Scripting Spec Ideas:
#! Script declaration (which interpreter to use)
#注释信息 (steps, ideas, uses, variable meanings, etc.)
The executable statement

Script creation:
Create a new text file
Add an executable script statement (command line)
Add x permission
[[email protected] ~]# vim x.sh (create file)
#!/bin/bash (Declaration interpreter)
#步骤, ideas, uses, variable meanings
echo "Hail HYDRA!!!!!" (write script statement)
[[email protected] ~]# chmod +x x.sh (add execute permission)
[Email protected] ~]#./x.sh
HAIL HYDRA!!!!!

——————————————————————————————

The shell variable is a weakly typed variable

Variable:
Variable name = value
Variable name: letter, Number, _, but cannot start with a number

Define/Assign Variables
Variable name = variable Value
[[email protected] ~]# x=12 define variable x, assign value 12
[[email protected] ~]# var1=redhat define variable var1, assign value Redhat
Related precautions:
If the specified variable name already exists, it is equivalent to re-assigning it to this variable name.
Don't have spaces on either side of the equals sign
Variable names are made up of letters/numbers/underscores and are case-sensitive
Variable names cannot start with a number, do not use keywords and special characters

Prevent conflicts can be in curly braces (case-sensitive)
$ variable Name
${variable Name}
[Email protected] ~]# a=7

To view the value of a variable:
Name of the echo $ variable
[Email protected] ~]# echo $a
7

To cancel a variable:
Name of the unset variable
[Email protected] ~]# unset a
[Email protected] ~]# vim b.sh
#!/bin/bash

Useradd $ (Variable $ variable)
echo "$" |passwd--stdin $

[Email protected] ~]#/b.sh Hydra Anonymous
Change the password for the user Hydra.
PASSWD: All the authentication tokens have been successfully updated.


Categories of variables:
Environment variables: variable names are usually case-sensitive, oil system maintenance, used to set the working environment
Location variables: Bash built-in, storing parameters provided when executing scripts
Predefined variables: Bash built-in, a class of special-purpose variables that can be called directly, but cannot be directly assigned or modified
Custom variables: Set, modify and use by the user


Variable configuration file
/etc/profile ~/.bash_profile
Related actions
ENV: List all the environment variables
Set: List all variables
Common environment variables:
PWD PATH USER LOGNAME UID SHELL HOME PS1 PS2 ....

Pre-defined variables:
The execution information used to save the script
Directly using these variables, you cannot assign values directly to these variables
Variable name:
$: Current process or pin name
$$: PID number of the current running process
$ : Return status after command execution, 0 Normal, non 0 indicates failure
$#: The number of position variables that have been loaded
$*: Values for all positional variables

Positional variables:
To provide command-line arguments when executing a script
expressed as $n,n as ordinal
$1,$2, ... ${10},${11}, ...
[Email protected] ~]# vim c.sh
#!/bin/bash
echo $ ${10} (view 1th, 10 positional parameters)
[Email protected] ~]#./c.sh 1 2 3 4 5 6 7 8 9 10 11
1 10

Instance:
Quickly add users and set a login password.
Provide the user name as a parameter when executing the script
Set the login password to 123456
[Email protected] ~]# vim x.sh
!/bin/bash
Useradd $2>/tmp/err.log
echo 123456 | passwd--stdin $ &>/dev/null

——————————————————————————————————————

Extended assignment operation
Distinguish three kinds of delimiters
Double quotation mark "": Allow extension to $ reference other variable
Single quote ': Disable extension, even if $ is also treated as a normal character
Anti-apostrophe ': Put the command, execute the command, take out the result of the command
(double quotes, single quotes are whole, but single quotes mask special symbols)

[Email protected] run]# echo "$USER"
Root
[Email protected] run]# echo ' $USER '
$USER
[[email protected] run]# echo ' date '. txt
July 25, 2017 Tuesday 07:04:09 CST.txt

Back up/var/log in tar, back up to/opt
[Email protected] opt]# tar-czf log_ ' Date +%y%m%d '. tar.gz/var/log/

————————————————————————————————————

Shell Script Basics 1 Overview and 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.