加入收藏 | 设为首页 | 会员中心 | 我要投稿 广州站长网 (https://www.020zz.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Unix > 正文

UNIX 特殊变量 Linux shell编程

发布时间:2022-11-01 16:01:27 所属栏目:Unix 来源:互联网
导读: Shell概述

Linux 提供的 Shell 解析器有
[root@localhost ~]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh

Centos 默认的解析器是 bash
[root@loc

Shell概述

UNIX 特殊变量_中介变量和调节变量_asp提供两个内置对象存储会话变量和应用程序变量

Linux 提供的 Shell 解析器有

[root@localhost ~]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh

Centos 默认的解析器是 bash

[root@localhost sy]# echo $SHELL
/bin/bash

Shell 脚本入门 第一个shell脚本

创建一个 Shell 脚本,输出 helloworld

#!/bin/bash
echo "helloworld"

脚本的常用执行方式

开子 shell 与不开子 shell 的区别就在于,环境变量的继承关系,如在子 shell 中设置的

当前变量,父 shell 是不可见的

变量 系统预定义变量 常用系统变量

HOME、HOME、HOME、PWD、SHELL、SHELL、SHELL、USER 等

自定义变量 基本变量 变量定义规则

export 变量 可把变量定义为全局变量

特殊变量 运算符

“((运算式))”或“((运算式))” 或 “((运算式))”或“[运算式]”

计算(2+3)* 4 的值

(154) root@ localhost ~/sy] $ echo $[(2+3)*4]
20

条件判断

[ condition ](注意 condition 前后要有空格)

常用判断条件

注:如果是字符串之间的比较 ,用等号“=”判断相等;用“!=”判断不等。

实际操作 流程控制 if判断

(1)单分支

if [ 条件判断式 ];then
   程序
fi

或者

if [ 条件判断式 ]
then
	程序

UNIX 特殊变量_asp提供两个内置对象存储会话变量和应用程序变量_中介变量和调节变量

fi

if [ 条件判断式 ]
then
      程序
elif [ 条件判断式 ]
then
      程序
else
      程序
fi

注意事项:

case 语句

!/bin/bash
case $1 in
"1")
echo "banzhang"
;;
"2")
echo "cls"
;;
*)

for 循环

#!/bin/bash
sum=0
for((i=0;i<=100;i++))
do
sum=$[$sum+$i]
done
echo $sum

#!/bin/bash
#打印数字
for i in cls mly wls
do
echo "ban zhang love $i"
done

while 循环

#!/bin/bash
sum=0
i=1

while [ $i -le 100 ]
do
sum=$[$sum+$i]
i=$[$i+1]
done
echo $sum

read 读入

#!/bin/bash
read -t 7 -p "Enter your name in 7 seconds :" NN
echo $NN

shell 实验练习 练习echo给变量赋值 练习通过修改PS1变量来更改提示符

高亮显示

PS1="\[\e[1m\](\#) \[\e[4;31m\]\u@ \[\e[7m\]\h \w] \$ \e[m"

练习find命令,全盘查找passwd文件,然后用相应的命令确认每一个passwd文件的文件类型。

find / -name passwd -ls

第一个字母为文件类型

(147) root@ localhost ~/sy] $ find / -name passwd -ls
67113279    0 dr-xr-xr-x   3 root     root            0 Oct 19 23:56 /sys/fs/selinux/class/passwd
67109855    0 -r--r--r--   1 root     root            0 Oct 19 23:56 /sys/fs/selinux/class/passwd/perms/passwd
51041202    4 -rw-r--r--   1 root     root          188 Mar 31  2020 /etc/pam.d/passwd
18363676    4 -rw-r--r--   1 root     root         2308 Sep 26 05:23 /etc/passwd

练习管道的使用,完成以下要求:

ls -aR /etc|less

a表示显示所有隐藏文件UNIX 特殊变量,R递归显示文件夹当中的文件

编写一个shell小程序

对一个目录实现交互式的备份压缩。(根据屏幕的提示选择压缩的方式是gzip还是bzip2、输入备份目录的路径,备份压缩的文件名等)

#!/bin/bash
echo "欢迎使用文件压缩系统"
read -p "输入压缩路径:" path
echo "请选择压缩类型序号 1.gzip 2.bzip"
read choice
if   [ $choice -eq 1 ]
then
        tar -cvPf your.tar.gz $path
elif [ $choice -eq 2 ]
then
        tar -jcvPf your.tar.gz $path
else
        ehco "选择错误重新选择"
fi
echo "压缩结束"

不加p,绝对路径报错

(编辑:广州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章