Shell script programming and variables of (plus real items)

Shell script programming and variables of (plus real items)

The purpose of this chapter: learn programming specifications, operation and knowledge-related script variables

Overview of a script .Shell

Shell script programming and variables of (plus real items)

The role of two .Shell

Shell script programming and variables of (plus real items)

III. Be the first Shell Script

1. Write a script (BOOT directory to view the current location, display VML beginning of a long and friendly format for all files)

[root@localhost ~]# vim aaa.sh "随意编一个空文件以.sh为结尾"

/bin/bash  "开头声明,固定格式”
一个脚本  "描述性信息可加可不加"
cd /boot/        

pwd
ls -lh vml*

~                                                                                       
~                       

2. add to our script execute permissions, look at the results

[root@localhost ~]# chmod +x aaa.sh

[root@localhost ~]# ./aaa.sh   //执行脚本,这是我们最常用的方式
/boot
-rwxr-xr-x. 1 root root 5.7M 8月  10 00:26 vmlinuz-0-rescue-cc65aecf945d4dde800fe3e29ee6edbc
-rwxr-xr-x. 1 root root 5.7M 8月  23 2017 vmlinuz-3.10.0-693.el7.x86_64

3. redirection symbol ">" pipe symbol "|"

" > " :把左侧的命令结果重定向另一个文件或目录

[root@localhost opt]# touch abc.tt abd.txt
[root@localhost opt]# ls
abc.tt  abd.txt  rh
[root@localhost opt]# tar czvf test.tar.gz *.txt > test2.txt
[root@localhost opt]# ls
abc.tt  abd.txt  rh  test2.txt  test.tar.gz
[root@localhost opt]# cat test2.txt
abd.txt
[root@localhost opt]# 

"|": The results of the command on the left side as the right side of the command to be processed

统计所有磁盘分区的占用率

[root@localhost opt]# df -hT
文件系统       类型      容量  已用  可用 已用% 挂载点
/dev/sda2      xfs        10G  3.8G  6.3G   38% /
devtmpfs       devtmpfs  898M     0  898M    0% /dev
tmpfs          tmpfs     912M     0  912M    0% /dev/shm
tmpfs          tmpfs     912M  9.1M  903M    1% /run
tmpfs          tmpfs     912M     0  912M    0% /sys/fs/cgroup
/dev/sdb1      xfs        20G   33M   20G    1% /mnt/sdb
/dev/sda5      xfs        10G   37M   10G    1% /home
/dev/sda1      xfs       6.0G  174M  5.9G    3% /boot
tmpfs          tmpfs     183M   12K  183M    1% /run/user/42
tmpfs          tmpfs     183M     0  183M    0% /run/user/0

磁盘在第一列  占用率在第5列

[root@localhost opt]# df -h | grep "dev/sd*" | awk '{print $1,$5 }'
/dev/sda2 38%                                          
tmpfs 0%
/dev/sdb1 1%
/dev/sda5 1%
/dev/sda1 3%
[root@localhost opt]# 

4. The effect of variable type and

Shell script programming and variables of (plus real items)

预定义变量:
变量名
作用
$0
当前脚本的名字
$n
传递给脚本或者函数的参数,n表示第几个参数
$#
传递给脚本或函数的参数个数
$*
传递给脚本或函数的所有参数
$@
传递给脚本或者函数的所有参数
$$
当前shell脚本进程的PID
$?
函数返回值,或者上个命令的退出状态

5. Define a new variable, the value of the variable view

Variable name = variable value
Variable names begin with a letter or an underscore, case sensitive


[root@localhost opt]# Producht=Python
[root@localhost opt]# Version=2.7.13
[root@localhost opt]# echo $Producht
Python
[root@localhost opt]# echo $Producht $Version
Python 2.7.13
[root@localhost opt]# 

6. Assignment using a variety of quotes, keyboard input content is variable assignment

Shell script programming and variables of (plus real items)

[root@localhost ~]# vim c.sh

#!/bin/bash
read -p "请输入一个整数" num1
read -p "请输入第二个整数" num2
~                               
[root@localhost ~]# chmod +x c.sh
[root@localhost ~]# ./c.sh
请输入第一个整数21
请输入的二个整数232

7. arithmetic integer variables

Shell script programming and variables of (plus real items)

8. position variable

Shell script programming and variables of (plus real items)

[root@localhost ~]# vim .sss.sh

#!/bin/bash
read -p "第一个位置变量$1"
read -p "第二个位置变量$2"
sum=`expr $1 + $2`
echo "整数之和为$sum"
~                        
[root@localhost ~]# ./.sss.sh 23 45
第一个位置变量23
第二个位置变量45
整数之和为68
[root@localhost ~]# ./.sss.sh 64 120
第一个位置变量64
第二个位置变量120
整数之和为18

9. Environment Variables

Shell script programming and variables of (plus real items)

[root@localhost ~]# echo $PWD
/root
[root@localhost ~]# echo $HOME
/root
[root@localhost ~]# echo $USER
root
[root@localhost ~]# echo $SHELL
/bin/bash

10. predefined variables

Shell script programming and variables of (plus real items)

#!/bin/bash
read -p "第一个位置变量$1"
read -p "第二个位置变量$2"
sum=`expr $1 + $2`
echo "整数之和为$sum"
echo "脚本名称$0"
echo "详细数字$*"
echo "参数的个数$#"
~                                                                                       
~                       
[root@localhost ~]# ./.sss.sh 64 120
第一个位置变量64
第二个位置变量120
整数之和为184
脚本名称./.sss.sh
详细数字64 120
参数的个数2

These are all our content up

Guess you like

Origin blog.51cto.com/14449524/2440089