Shell script (i) Shell scripting basics using shell variables

Source: Internet
Author: User

Tags: dir single quote information script python 2 operator sequence boot overwrite

The role of the shell: between the system kernel and the user, is responsible for interpreting the command line.
·
Shell programs that are used by default after logging in, typically/bin/bash
Different shell internal instructions, operating environment, etc. will differ
[Email protected] ~]# Cat/etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
·
################### #编写脚本实例 ####################
1, using VI text editor, each line of a Linux command, according to the order of execution to write
[Email protected] ~]# VI frist.sh
cd/boot/
Pwd
LS-LH VML
·
2. Give executable permission
[Email protected] ~]# chmod +x frist.sh
·
3. There are three ways to execute a script file
Method One: Script file path
[Email protected]/]# sh/frist.sh
/boot
-rwxr-xr-x. 1 root root 5.2M July 09:18 vmlinuz-0-rescue-78ce74241c884ba1861483f7ce9ce624
-rwxr-xr-x. 1 root root 5.2M November vmlinuz-3.10.0-514.el7.x86_64
·
Method Two: Sh script file path
[Email protected] ~]#./frist.sh
/boot
-rwxr-xr-x. 1 root root 5.2M July 09:18 vmlinuz-0-rescue-78ce74241c884ba1861483f7ce9ce624
-rwxr-xr-x. 1 root root 5.2M November vmlinuz-3.10.0-514.el7.x86_64
·
Method Three: The source script file path switches directly to the script's working environment
[Email protected]/]# source/frist.sh
/boot
-rwxr-xr-x. 1 root root 5.2M July 09:18 vmlinuz-0-rescue-78ce74241c884ba1861483f7ce9ce624
-rwxr-xr-x. 1 root root 5.2M November vmlinuz-3.10.0-514.el7.x86_64
[Email protected] boot]# pwd
/boot
·
4. Perfect script structure
Script Declaration, comment information, executable statement
[Email protected] boot]# vi/frist.sh
#!/bin/bash
#This is my first shell-script.
Cd/boot
echo "The current directory is located in:"
Pwd
echo "The files that begin with VML include:"
LS-LH VML

·
[Email protected] boot]#/first.sh
The current directory is located at:
/boot
Files that begin with VML include the following:
-rwxr-xr-x. 1 root root 5.2M July 09:18 vmlinuz-0-rescue-78ce74241c884ba1861483f7ce9ce624
-rwxr-xr-x. 1 root root 5.2M November vmlinuz-3.10.0-514.el7.x86_64
·
·
################### #重定向与管道操作 ####################
1. Interactive hardware devices
Type file description number default device
Standard Input 0 Keyboard
Standard Output 1 screen
Standard Error 2 Screen
·
2. REDIRECT Operation
Type operation symbol function
REDIRECT Input < read data from the specified file instead of typing from the keyboard
REDIRECT Output > Save output to the specified file (overwrite the original content)
Standard error Output 2> saving error information to the specified file (overwrite the original content)
------------------2>> Append the error message to the specified file
Mixed output &> save standard output, standard error content to the same file
·
3 pipe operation symbol "|"
Outputs the command on the left as the processing object for the right command
[[email protected] ~]# grep "bash"/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Yuan:x:1021:1021::/home/yuan:/bin/bash
[[email protected] ~]# grep "Bash"/etc/passwd | Wc-l
2
·
################### #Shell变量的作用, type ####################
Role
Provides specific parameters for flexible management of Linux systems with two layers of meaning
Variable name: Using a fixed name, either by a system preset or by a user-defined
Variable Value: Ability to change depending on user settings and system environment
·
Type
Custom variables: defined, modified, and used by the user
Environment variables: maintained by the system for setting up the working environment
Positional variables: Passing parameters to the script through the command line
Predefined variables: A class of variables built into bash that cannot be modified directly
·
Custom variables
Variable name = variable Value
·
################### #查看变量的值 echo $ variable name ####################
[Email protected]/]# Product=python
[Email protected]/]# version=2.7.13
[Email protected]/]# echo $Product
Python
[Email protected]/]# echo $Product $Version
Python 2.7.13
·
################### #赋值时使用引号 ####################
Double quotation marks: Allow referencing of other variable values through the $ symbol
Single quotation marks: Prohibit reference to other variable values, $ as normal characters
Reverse apostrophe: Command substitution, extracting output from command execution
·
# # #使用bash内置的read命令来给变量赋值, the read command prompts the user to enter user information for a simple interactive process # # #
Read [-P ' hint message '] variable name
[email protected]/]# Read ToDir2
/opt
[Email protected]/]# echo $ToDir 2
/opt
·
################### #设置变量的作用范围 ####################
[Email protected]/]# echo "$Product $Version"
Python 2.7.13
[Email protected]/]# export Product Version # # # #设置为全局变量
[[email protected]/]# Bash # # # # #进入shell环境
[Email protected]/]# echo "$Product $Version" # # # #子程序引用全局变量
Python 2.7.13
[[Email protected]/]# exit
Exit
·
################### #整数变量的运算 ####################
Addition Operation: +
Subtraction Operations:-
Multiplication Operation: *
Division Operation:/
Modulus (take rest) operation:%
[Email protected]/]# x=35
[Email protected]/]# y=16
[Email protected]/]# expr $X + $Y
51
·
[Email protected]/]# expr $X-$Y
19
[Email protected]/]# expr $X * $Y
560
[Email protected]/]# expr $X/$Y
2
[Email protected]/]# expr $X% $Y
3
·
[Email protected] ~]# ycube= expr $Y \* $Y \* $Y
[Email protected] ~]# echo $Ycube
4096
·
################### #位置变量 ####################
represented as $n, n is a number between 1~9
·
[Email protected]/]# VI add.sh
#!/bin/bash
sum= expr $1 + $2
echo "$ + $ = $SUM"
·
[Email protected] ~]# chmod +x add.sh
·
[Email protected]/]#./add.sh 12 34
12 + 34 = 46
·
################### #预定义变量 ####################
$#: Number of positional variables in the command line
$ : The contents of all positional variables
$?: 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
$: The currently executing process/program name
[Email protected] ~]# VI mybak.sh
#!/bin/bash
tarfile=beifen- date +%s . tgz
Tar zcf $TARFILE $
&>/dev/null
echo "Executed $ script"
echo "Complete a backup of $# objects altogether"
echo "Specific content includes: $*"
·
[Email protected] ~]# chmod +x mybak.sh # # #给这个脚本执行权限
[Email protected] ~]#./mybak.sh/boot/grub # # #备份一个
The./mybak.sh script has been executed
A total of 1 object backups completed
Specific content includes:/boot/grub
[Email protected] ~]#./mybak.sh/etc/passwd/etc/shadow # # #备份二个
The./mybak.sh script has been executed
A total of 2 object backups completed
Specific content includes:/etc/passwd/etc/shadow

Shell script (i) Shell scripting basics using shell 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.

Tags Index: