2 shell脚本初步入门

本文详细介绍了Linux下shell脚本的基础知识,包括Bourne和Cshell的区别,查看系统shell支持,bash版本管理,shell脚本的执行方式,以及shell编程的良好习惯,如脚本解释器指定、编码规范和变量引用等。
摘要由CSDN通过智能技术生成

参考学习 书籍  跟老男孩 学liunx 运维 shell编程实战 

1 shell脚本语言 是弱类型语言 无须定义变量类型即可使用 

                                             一  Bourne  shell    二  C shell   

 1  Bourne  shell:sh  ksh  bash   
 2 C shell :csh  tcsh   

 3 查看系统支持的shell 支持情况  cat  /etc/shell  
 4 通过下面2种方法 查看 系统默认的shell 
 echo  $SHELL
 grep  root /etc/passwd 
 5   #! /bin/bash  第一行指出由哪个程序(解释器)或执行脚本中的内容
#! 幻数 必须在第一行 不是第一行则为脚本注释行 
6 sh为bash的软链接  
7 cat /etc/redhat-release 查看liunx的环境版本
[root@localhost init.d]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
8 查看bash 版本
[root@localhost init.d]# bash --version
GNU bash, 版本 4.2.46(2)-release (x86_64-redhat-linux-gnu)   bash 版本 
Copyright (C) 2011 Free Software Foundation, Inc.
许可证 GPLv3+: GNU GPL 许可证版本3或者更高 <http://gnu.org/licenses/gpl.html>

这是自由软件,您可以自由地更改和重新发布。
在法律允许的范围内没有担保.

9 升级bash   yum -y update  bash  
升级后查看 rpm -qa bash
[root@localhost /]# rpm -qa bash
bash-4.2.46-35.el7_9.x86_64  
10 shell脚本的执行   先查找系统环境变量ENV  
加载顺序通常是 /etc/profile  ~/.bash_profile  ~/.bashrc   /etc/bashrc  

11 shell脚本的执行 可用采用以下几种方式 

1 bash  script-name   或 sh  script-name  这是当脚本文件本身没有执行权限时  
2 path script-name    或 ./script-name 有执行权限时 用绝对和相对路径  
3 source script-name  或 . script-name
4 sh < script-name    或 cat script-name | sh  
12 创建模拟脚本  
cat > test.sh
echo 'I am jack'
键盘输入组合键 ctrl+d 结束编辑
第一种方法
[root@localhost ~]# cat > test2.sh 
echo ' I am jack '
[root@localhost ~]# cat test2.sh
echo ' I am jack '
[root@localhost ~]# sh test2.sh
 I am jack 
[root@localhost ~]# bash test2.sh
 I am jack 

方法2 和方法3
[root@localhost ~]# ls -l test2.sh
-rw-r--r--. 1 root root 19 4月  28 06:57 test2.sh
[root@localhost ~]# ./test2.sh         
 #方法2 这个地方无法自动补全 因为没有权限
-bash: ./test2.sh: 权限不够
# 方法3 可以 请注意开头的. 后面有空格 
#方法3 都是读入并执行脚本
[root@localhost ~]# . test2.sh
 I am jack 
[root@localhost ~]# source test2.sh
 I am jack 

#给脚本文件 添加执行权限后 方法2 就能执行了
 [root@localhost ~]# chmod +x test2.sh
[root@localhost ~]# ls -l test2.sh
-rwxr-xr-x. 1 root root 19 4月  28 06:57 test2.sh
[root@localhost ~]# ./test2.sh

使用方法3 实践时 会将'source' 或'.' 执行脚本的变量值 传递到当前的shell中 
#第一行的内容 通常用echo处理 更方便
echo 'userdir=`pwd`' >testsource.sh
[root@localhost ~]# cat testsource.sh
userdir=`pwd`                          #脚本内容定义了一个命令变量 打印当前路径 pwd使用反引号 也是大家说的飘号 ``
[root@localhost ~]# sh testsource.sh          #采用sh命令执行脚本 
[root@localhost ~]# echo $userdir
                                     #此处为空 没有当前路径的输出
[root@localhost ~]# source testsource.sh  #使用source 
[root@localhost ~]# echo $userdir   # 这时能看到 userdir变量的值
/root

系统NFS服务的脚本 通过.加载系统函数库 functions

第四种方法实践
[root@localhost ~]# ls -l test3.sh
-rw-r--r--. 1 root root 17 4月  28 07:18 test3.sh
[root@localhost ~]# cat test3.sh
echo 'I am tom '
[root@localhost ~]# sh < test3.sh        # 尽量不要使用这种方法
I am tom 
[root@localhost ~]# cat test3.sh | sh     # 这种方法在命令行 拼接字符串命令后 需要执行时就会用到 
I am tom 

让"父亲随儿子姓的办法" 
用source或. 在父亲shell脚本中事先加载 儿子shell脚本  

13 Shell 脚本开发的基本规范

1 shell脚本的第一行 指定脚本解释器 
# ! /bin/bash  

2 Shell 脚本开头会加 版本 版权 等信息 
#Date:    2024-04-28_07:26:51
#Author:  Created by jack
#Descript:      This scripts functions is ...
#Version:       1.1 
可修改~/.vimrc 配置文件vim编辑文件时 自动加载以上信息 (我下一篇文章写这个)

3 在shell脚本中尽量不要用中文
 用了的话 最好 在脚本中重新定义字符集设置 和系统保持一致
 参考:export LANG="zh_CN.UTF-8"

4 shell脚本的命名以.sh为扩展名
5 shell脚本因存放在固定的路径下    如:/server/scripts  

14 shell 脚本代码书写的良好习惯   

1 成对的符号 一次性写完  {} [] "" ''  `` 也可以配置.vimrc s实现自动添加  
2 中括号[] 的2端至少有一个空格 
3 对于流程控制语句 应一次性将格式写完 再添加内容 
  一次性写完if循环语句的格式
if   条件内容 
       then
  内容 
fi

一次性写完for循环语句的格式
for
do  
   内容 
done
4 通过缩进让代码更易读 如
if  条件语句 
        then
        内容
fi

5对于常量变量的字符串定义变量值应加双引号,并且等号后面不能有空格 
需要强引用的(即所见即所得的字符引用)则用单引号'' 
命令的引用 则用反引号`` 
JACK_FILE="test.txt"

6脚本中的单引号 双引号 及反引号 必须为英文状态下的符号 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值